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

···

--
Posted via http://www.ruby-forum.com/.

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

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

thnx!!!!
LuCa

···

--
Posted via http://www.ruby-forum.com/.

thats it, thnx!!

···

--
Posted via http://www.ruby-forum.com/.

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 Crocco wrote:

···

Alle Sunday 27 January 2008, Luca Scaljery ha scritto:

doesn't work

Any suggestion

thnx a lot
LuCa

my_object.send( my_var)

Stefano

--
Posted via http://www.ruby-forum.com/\.

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

Stefano Crocco wrote:
> Alle Sunday 27 January 2008, Luca Scaljery ha scritto:
>> doesn't work
>>
>> Any suggestion
>>
>> thnx a lot
>> LuCa
>
> my_object.send( my_var)
>
> 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

Luca Scaljery 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

···

--
NP: Anathema - Closer
Jabber: sepp2k@jabber.org
ICQ: 205544826