[ruby 1.8]
Assume I have:
dummy \"string\" do
puts \"thing\"
end
Now, this is a call to a method which has as
Yes, there are a few options.
The first is method_missing
. Its first argument is a symbol which is the method that was called, and the remaining arguments are the arguments that were used.
class MyClass
def method_missing(meth, *args, &block)
# handle the method dispatch as you want;
# call super if you cannot resolve it
end
end
The other option is dynamically creating the instance methods at runtime, if you know in advance which methods will be needed. This should be done in the class, and one example is like this:
class MyClass
1.upto(1000) do |n|
define_method :"method_#{n}" do
puts "I am method #{n}!"
end
end
end
It is a common pattern to have define_method
called in a class method which needs to create new instance methods at runtime.