1

I have to dynamically call a list of methods on an object obj. I am trying to instantiate a method object and then call it.

method_name (a string) is a name of a method which can be called on object obj.

meth=obj.method(method_name) #method_name is a string
meth.call = mod

I am getting the following error:

undefined method `call=' for # (NoMethodError)

I am using Sequel ORM and have to save model associations dynamically. If I directly call method_name (when method_name is not a string) the following is working

obj.method_name = mod #working

However, when method_name is a string, the following is giving a syntax error:

obj.send(method_name) = mod #not working

syntax error, unexpected '=', expecting keyword_end

So I am not able to call the methods from their name in string form using any of the above ways.

Kumar Akarsh
  • 4,954
  • 2
  • 20
  • 31
  • possible duplicate with http://stackoverflow.com/questions/621176/how-to-dynamically-call-accessor-methods-in-ruby – Baldrick Feb 27 '12 at 08:49

2 Answers2

3

If the method named method_name takes the mod parameter try:

obj.send(method_name, mod)

If you are assigning something, then method_name should end with =.

Candide
  • 30,469
  • 8
  • 53
  • 60
0

The correct syntax for call is

call(args, ...)

[ref]

(Don't put = after call, just list the arguemnts separated by commas)

Also Ingenu's method, is also a good way (probably better) of doing similar thing.

Community
  • 1
  • 1
abhishek
  • 998
  • 6
  • 9