I want to create a class that can add methods dynamically and allow multiple parameters.
For example:
r = Robot.new
r.learn_maneuvering('turn') { |degree| puts "turning #{degree} degrees" }
r.turn 50 # => turning 50 degrees
r.turn 50, 60 # => turning 50 degrees # => turning 60 degrees
My first attempt was this:
def learn_maneuvering(name, &block)
define_singleton_method(name, &block)
end
However, it only accounts for one parameter..
I then started with:
def learn_maneuvering(name, &block)
define_singleton_method(name) do |*args|
# to do
end
end
I believe this will loop until all arguments are used right? Anywho, I am not sure how to pass these arguments to the given block.
You’re close:
prints:
But is this really what you want? It seems like just doing
makes more sense to me.