Download this source
# Create new methods on a class
# One way is to use Eval
method_name = 'my_method'
eval <<-EOC
def #{method_name}
'You called #{method_name}'
end
EOC
puts my_method
# Another way is to use define_method
class Clazz
define_method(:ameth) do |i|
# 10.times { yield self }
i.call(self)
end
end
# Have to use lambda (or proc), can't pass a &block
# argument to another block.
Clazz.new.ameth(lambda { |me| puts me })
Running this outputs:
You called my_method
#<Clazz:0xb7f3d890>

