4

In Obj-C, the method to invoke can be decided at runtime using selector mechanism.

Is there anything similar in Ruby so that I can convert a method string into a method symbol at runtime and invoke it?

Chuankai
  • 151
  • 1
  • 8

2 Answers2

3

You want the send method:

obj.send(method_name), where method_name can be either a string or a symbol, will invoke the method with the given name on obj.

Any arguments you want to pass to the method, can be given as additional arguments to send, i.e. obj.send(method_name, argument1, argument2).

sepp2k
  • 363,768
  • 54
  • 674
  • 675
0

Sure. Checkout Ruby Object Class send method.

Wizz
  • 1,267
  • 1
  • 13
  • 20