Return to Snippet

Revision: 3761
at September 12, 2007 10:27 by vanne


Initial Code
# quick method on how to create dynamic methods or methods that share the same codebase
method_names = [:dog, :cat, :man, :women]

method_names.each do |attr_name|
  eval <<-END
    def #{attr_name}
      class << self ; attr_reader :#{attr_name} ; end
      # here is where you would write what you want your methods to do
      # simply reference the method with an @ sign
      @#{attr_name} = "#{attr_name}"
    end
  END
end

# the above code simply creates 4 methods named dog, cat, man, and women and outputs them as
# strings


Initial URL

                                

Initial Description

                                

Initial Title
dynamic methods

Initial Tags
ruby

Initial Language
Other