How to access a method when method-name is in a variable

Hi All

I have a method name stored in a variable. How do I now invoke the
method ?

my_var = ‘some_method’
my_obj = Some_Obj.new

my_obj.my_var()

doesn’t work

Any suggestion

thnx a lot
LuCa

Alle Sunday 27 January 2008, Luca S. ha scritto:

doesn’t work

Any suggestion

thnx a lot
LuCa

my_object.send( my_var)

Stefano

thats it, thnx!!

An other question, is it possible to assign a value to an attribute this
way

For example, this doesn’t work

my_object.send(my_var) = 2000

thnx in advance
LuCa

Stefano C. wrote:

Alle Sunday 27 January 2008, Luca S. ha scritto:

doesn’t work

Any suggestion

thnx a lot
LuCa

my_object.send( my_var)

Stefano

Alle Sunday 03 February 2008, Luca S. ha scritto:

Stefano C. wrote:

Stefano
Yes, you can. You must understand that, when you do:

my_object.my_var= 2000

you’re actually calling the my_var= method of my_object with argument
2000.
Therefore, to do this with send, you need to do:

my_object.send(:my_var=, 2000)

Stefano

that makes sense, now I understand what send() is actually doing!

thnx!!!
LuCa

Luca S. wrote:

For example, this doesn’t work

my_object.send(my_var) = 2000

If you use attr_writer :foo, it creates the method foo= which takes one
argument. So you’d call it like this:
send(:foo=, 2000)
or
my_var = “foo”
send("#{my_var}=", 2000)

HTH,
Sebastian