/ Published in: Other
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# since define_method is private, we will abstract it into a public method named create_method # so that we can just use Object#create_method when we want to dynamically create methods # Now you can do something like # ["one","two","three"].each { |n| Model.create_method("number_#{n}") { puts n }} # Now you have : # Model.number_one # => one # Model.number_two # => two # Model.number_three # => three def create_method(name,&block) self.class.send(:define_method,name,&block) end