/lib/meta.rb
Ruby | 32 lines | 21 code | 4 blank | 7 comment | 0 complexity | 2f8a3796214e7598f9e14c6d1ed95103 MD5 | raw file
Possible License(s): GPL-2.0
1# A set of methods to help create meta-programming gizmos. 2class Object 3 # The metaclass is the singleton behind every object. 4 def metaclass 5 class << self 6 self 7 end 8 end 9 10 # Evaluates the block in the context of the metaclass 11 def meta_eval &blk 12 metaclass.instance_eval &blk 13 end 14 15 # Acts like an include except it adds the module's methods 16 # to the metaclass so they act like class methods. 17 def meta_include mod 18 meta_eval do 19 include mod 20 end 21 end 22 23 # Adds methods to a metaclass 24 def meta_def name, &blk 25 meta_eval { define_method name, &blk } 26 end 27 28 # Defines an instance method within a class 29 def class_def name, &blk 30 class_eval { define_method name, &blk } 31 end 32end