[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Luca Scaljery

1/27/2008 8:38:00 PM

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-....

6 Answers

Stefano Crocco

1/27/2008 8:42:00 PM

0

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


Luca Scaljery

1/27/2008 8:47:00 PM

0

thats it, thnx!!
--
Posted via http://www.ruby-....

Luca Scaljery

2/3/2008 12:14:00 PM

0

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-....

Stefano Crocco

2/3/2008 12:19:00 PM

0

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


Sebastian Hungerecker

2/3/2008 12:21:00 PM

0

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

Luca Scaljery

2/3/2008 7:45:00 PM

0

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

thnx!!!!
LuCa
--
Posted via http://www.ruby-....