Wednesday, August 29, 2007

Dynamically adding methods

This is divine, ruby is too great to be true. no explanation needed.


  1. class Object  
  2.   def def(method_name, &block)  
  3.     (class << selfself end).send(:define_method, method_name, block)  
  4.   end  
  5. end  
  6.   
  7. x = Object.new  
  8.   
  9. string = "This is a test"  
  10. x.def(:elad_says) {puts "Elad says: " + string}  
  11.   
  12. x.elad_says  

No comments:

Post a Comment

Tell me what you think