Created
August 5, 2012 18:37
-
-
Save mtunjic/3266599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author : why the lucky stiff | |
# include in any project in which metaprogramming is needed | |
class Object | |
# The hidden singleton lurks behind everyone | |
def metaclass; class << self; self; end; end | |
# The equivalent of class_eval for singleton classes. | |
# Evaluates the given block in | |
# the context of the receiver’s singleton class. | |
def meta_eval &blk; metaclass.instance_eval &blk; end | |
# Adds methods to a metaclass | |
# Defines a method within the receiver’s singleton class. | |
# If the receiver is a class or module, this will create | |
# a class method (instance method of the receiver’s singleton | |
# class). | |
def meta_def name, &blk | |
meta_eval { define_method name, &blk } | |
end | |
# Defines an instance method within a class | |
# Defines an instance method in the receiver | |
# (which must be a class or module). | |
def class_def name, &blk | |
class_eval { define_method name, &blk } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment