Define_method enables one to add a method to a class or object at runtime. It takes a symbol (for the name of the method) and a block (for the body of the method). define_method is private so if used outside of the class then use Object.class_eval.
=begin
define_method enables one to add a method to a class or object
at runtime. It takes a symbol (for the name of the method) and a block
(for the body of the method). define_method is private so if used outside of
the class then use Object.class_eval.
=end
class MyClass
def self.new_method(name, &block)
define_method(name, &block)
end
end
MyClass.new_method(:mymeth) {
n,i=5,0
while i < n do
puts Time.now
i+=1
sleep 1
end
}
x = MyClass.new
x.mymeth #Gives the mymeth method to MyClass at runtime."