ruby - How to understand dynamic method creation -
i saw code:
class myclass define_method :my_method |my_arg| my_arg*3 end end obj = myclass.new obj.my_method(2) # => 6 when create obj, have not yet called define_method, my_method should not have been created. then, why can call obj.my_method(2) directly? in other words, define_method executed @ time when myclass instantiated?
any method executed when appears directly in context read. there no exception method define_method, defines method. method define_method called (and hence method my_method defined) in line 2, before obj created in line 7. , defined method not object.
Comments
Post a Comment