More methods at runtime

More methods at runtime

class Test
  def self.makemethod(methodname)
    (class << self; self; end).module_eval do
      define_method(methodname) do |parameter|
        print("#{methodname} says #{parameter}")
      end
    end
  end
end

# Try it
Test.makemethod("hello")
Test.hello("world")
> hello says world

Tags: , ,

1 Comment Leave yours

1 Trackbacks

Leave a Reply