[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

calling a method using a variable name??

ajtwatching

3/14/2007 1:52:00 AM

Hey folks,

Like many posts have no doubt started.. I'm giving this ruby business
a crack!! ;)

Now this might be a really bad approach.. but I'll throw it out there.

Here's my simple class.

class Myclass
def initialize
end

def hello
puts "hello"
end

def bye
puts "bye"
end
end

As expected, works a treat.

friendly = Myclass.new
friendly.hello
friendly.bye

Now, given I now what methods are available, is there a way to do
something similar to this...

methods = ['hello', 'bye']
methods.each { |m| friendly.m }

Regards,

ajt.


4 Answers

Tim Hunter

3/14/2007 1:59:00 AM

0

ajtwatching wrote:
> Hey folks,
>
> Like many posts have no doubt started.. I'm giving this ruby business
> a crack!! ;)
>
> Now this might be a really bad approach.. but I'll throw it out there.
>
> Here's my simple class.
>
> class Myclass
> def initialize
> end
>
> def hello
> puts "hello"
> end
>
> def bye
> puts "bye"
> end
> end
>
> As expected, works a treat.
>
> friendly = Myclass.new
> friendly.hello
> friendly.bye
>
> Now, given I now what methods are available, is there a way to do
> something similar to this...
>
> methods = ['hello', 'bye']
> methods.each { |m| friendly.m }
>
> Regards,
>
> ajt.
>
>
>
Look up the __send__ method:

obj.__send__("methodname", arg1, arg2)


KDr2

3/14/2007 2:11:00 AM

0

"ajtwatching" <ajtwatching@gmail.com> writes:

> Hey folks,
>
> Like many posts have no doubt started.. I'm giving this ruby business
> a crack!! ;)
>
> Now this might be a really bad approach.. but I'll throw it out there.
>
> Here's my simple class.
>
> class Myclass
> def initialize
> end
>
> def hello
> puts "hello"
> end
>
> def bye
> puts "bye"
> end
> end
>
> As expected, works a treat.
>
> friendly = Myclass.new
> friendly.hello
> friendly.bye
>
> Now, given I now what methods are available, is there a way to do
> something similar to this...
>
> methods = ['hello', 'bye']
> methods.each { |m| friendly.m }
methods.each{|m| friendly.send m}
>
> Regards,
>
> ajt.
>
>

--
http:...

------yours Killy Draw

Rick DeNatale

3/14/2007 9:57:00 AM

0

On 3/13/07, Timothy Hunter <TimHunter@nc.rr.com> wrote:

> Look up the __send__ method:
>
> obj.__send__("methodname", arg1, arg2)

Or just send which in most cases is preferable. They both do the same
thing (in fact I believe that one is an alias of the other). __send__
is there to cover cases where a class defines a send method for
another purpose.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Giles Bowkett

3/14/2007 8:35:00 PM

0

Yep, +1. Use #send() rather than #__send__().

Example from irb:

>> "muppet".send(:to_s)
=> "muppet"

You're telling the String object "muppet" to receive the message
"to_s", in a Smalltalk sense, or, in a Java sense, you're telling the
String object "muppet" to call the method "to_s".

To do it with a variable:

>> kermit = "to_s"
=> "to_s"
>> "muppet".send(kermit)
=> "muppet"

Badda bing badda boom.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://giles.t...


On 3/14/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 3/13/07, Timothy Hunter <TimHunter@nc.rr.com> wrote:
>
> > Look up the __send__ method:
> >
> > obj.__send__("methodname", arg1, arg2)
>
> Or just send which in most cases is preferable. They both do the same
> thing (in fact I believe that one is an alias of the other). __send__
> is there to cover cases where a class defines a send method for
> another purpose.
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
>