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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -