Send an unknown method call

From Rosetta Code
Revision as of 22:12, 27 August 2011 by rosettacode>DavidMcCabe (New task, Ruby implementation given.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Send an unknown method call
You are encouraged to solve this task according to the task description, using any language you may know.

Send a message to an object without knowing the name of the message. See also Respond to an unknown method call.

Ruby

class Example
  def foo
    42
  end
  def bar(arg1, arg2, &block)
    block.call arg1, arg2
  end
end

symbol = :foo
Example.new.send symbol       # => 42
Example.new.send( :bar, 1, 2 ) { |x,y| x+y }          # => 3