/lib/meta.rb

http://github.com/publicdisplay/icalendar · Ruby · 32 lines · 21 code · 4 blank · 7 comment · 0 complexity · 2f8a3796214e7598f9e14c6d1ed95103 MD5 · raw file

  1. # A set of methods to help create meta-programming gizmos.
  2. class Object
  3. # The metaclass is the singleton behind every object.
  4. def metaclass
  5. class << self
  6. self
  7. end
  8. end
  9. # Evaluates the block in the context of the metaclass
  10. def meta_eval &blk
  11. metaclass.instance_eval &blk
  12. end
  13. # Acts like an include except it adds the module's methods
  14. # to the metaclass so they act like class methods.
  15. def meta_include mod
  16. meta_eval do
  17. include mod
  18. end
  19. end
  20. # Adds methods to a metaclass
  21. def meta_def name, &blk
  22. meta_eval { define_method name, &blk }
  23. end
  24. # Defines an instance method within a class
  25. def class_def name, &blk
  26. class_eval { define_method name, &blk }
  27. end
  28. end