Forum: Ruby Call a Function

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F4387b44a3f17713865d010641d3461b?d=identicon&s=25 Jason Shelton (Guest)
on 2009-02-03 22:40
(Received via mailing list)
Hello,

I currently have an array of strings.  Let's say, for example, my array
contains the following:

                            ['one', 'two', 'three']

Each element in my array is the name of a function in my program.  I
want to call the functions, using the array element.  So for example,
lets say that my array is named 'numArray'.  I want to call the function
in my program named 'one'.  I am now trying numArray[0].  Is there a way
to make the numArray[0] be recognized as 'one' and call the function?
If my question is not clear, I can elaborate.  Thanks in advance for all
help.

- Shelton
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-02-03 22:48
(Received via mailing list)
Jason Shelton wrote:
> I currently have an array of strings.  Let's say, for example, my array
> contains the following:
>
>                             ['one', 'two', 'three']
>
> Each element in my array is the name of a function in my program.  I want
> to call the functions, using the array element.

Object#send is the method you want. It takes a method name (follows by a
list
of arguments, if there are any) as an argument and calls that method on
the
receiver.

HTH,
Sebastian
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2009-02-03 22:55
(Received via mailing list)
On Feb 3, 2009, at 3:39 PM, Jason Shelton wrote:

> example, lets say that my array is named 'numArray'.  I want to call
> the function in my program named 'one'.  I am now trying
> numArray[0].  Is there a way to make the numArray[0] be recognized
> as 'one' and call the function?  If my question is not clear, I can
> elaborate.  Thanks in advance for all help.


Convert the string to a symbol and send it to the appropriate object.

   > send( numArray[0].to_sym )
   => NoMethodError: undefined method 'one' for main:Object

   > def one; puts "One!"; end
   => nil

   > send( numArray[0].to_sym )
   One!
   => nil
5b4aaa2337d29b4294bf12e74ded2052?d=identicon&s=25 Jonathon Brenner (Guest)
on 2009-02-03 23:00
(Received via mailing list)
Use "eval".

irb(main):001:0> def one
irb(main):002:1> puts "1"
irb(main):003:1> end
=> nil
irb(main):004:0> def two
irb(main):005:1> puts "2"
irb(main):006:1> end
=> nil
irb(main):007:0> metharray = ['one', 'two']
=> ["one", "two"]
irb(main):008:0> metharray.each { |e| eval e }
1
2
=> ["one", "two"]
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-02-03 23:07
(Received via mailing list)
> Convert the string to a symbol and send it to the appropriate object.
That isn't even necessary, sending a string works just as fine.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-03 23:08
(Received via mailing list)
Hi --

On Wed, 4 Feb 2009, Matthew Moss wrote:

>>
>> Each element in my array is the name of a function in my program.  I want
>> to call the functions, using the array element.  So for example, lets say
>> that my array is named 'numArray'.  I want to call the function in my
>> program named 'one'.  I am now trying numArray[0].  Is there a way to make
>> the numArray[0] be recognized as 'one' and call the function?  If my
>> question is not clear, I can elaborate.  Thanks in advance for all help.
>
>
> Convert the string to a symbol and send it to the appropriate object.

No need to convert it. send will take a string.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-02-03 23:09
(Received via mailing list)
> Use "eval".
No, please do not.
Using eval is not necessary *most* of the time, especially in this
case where you got the send method.
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2009-02-03 23:20
(Received via mailing list)
On Feb 3, 2009, at 4:00 PM, Dominik Honnef wrote:

>
>> Convert the string to a symbol and send it to the appropriate object.
> That isn't even necessary, sending a string works just as fine.

Yeah, after the fact, I tried it like you say. For some reason, I had
the impression that strings didn't auto-convert to symbols.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-03 23:28
(Received via mailing list)
Hi --

On Wed, 4 Feb 2009, Matthew Moss wrote:

>
> On Feb 3, 2009, at 4:00 PM, Dominik Honnef wrote:
>
>>
>>> Convert the string to a symbol and send it to the appropriate object.
>> That isn't even necessary, sending a string works just as fine.
>
> Yeah, after the fact, I tried it like you say. For some reason, I had the
> impression that strings didn't auto-convert to symbols.

The documentation is misleading; it specifies the argument as a
symbol, but it can be either. (And I consider that a feature; I don't
think it's just working by chance.)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-04 00:32
(Received via mailing list)
Wouldn't you have to pass in the calling context (self from the
callers pov) if you were deffing your methods without a class? Ie
needs to k ow the object to send on, no? :)

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 04/02/2009, at 8:46 AM, Sebastian Hungerecker
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-02-04 11:18
(Received via mailing list)
Julian Leviston wrote:
> Wouldn't you have to pass in the calling context (self from the  
> callers pov) if you were deffing your methods without a class?

self from the callers pov is also self from send's pov if you invoke it
without an explicit receiver. So no, you don't.

HTH,
Sebastian
This topic is locked and can not be replied to.