Return to Snippet

Revision: 3915
at October 3, 2007 15:39 by vanne


Initial Code
# 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

Initial URL

                                

Initial Description

                                

Initial Title
Dynamic Method Creation

Initial Tags
ruby

Initial Language
Other