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

L

Luca Scaljery

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
 
S

Stefano Crocco

Alle Sunday 27 January 2008, Luca Scaljery ha scritto:
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

my_object.send( my_var)

Stefano
 
L

Luca Scaljery

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
 
S

Stefano Crocco

Alle Sunday 03 February 2008, Luca Scaljery ha scritto:
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

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
 
S

Sebastian Hungerecker

Luca said:
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
 
L

Luca Scaljery

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

thnx!!!!
LuCa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top