Call a Function

J

Jason Shelton

Hello=2C
=20
I currently have an array of strings. Let's say=2C for example=2C my array=
contains the following:
=20
['one'=2C 'two'=2C 'three']
=20
Each element in my array is the name of a function in my program. I want t=
o call the functions=2C using the array element. So for example=2C lets sa=
y that my array is named 'numArray'. I want to call the function in my pro=
gram 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=2C I can elaborate. Thanks in advance for all help.
=20
- Shelton
_________________________________________________________________
Windows Live=99 Hotmail=AE:=85more than just e-mail.=20
http://windowslive.com/explore?ocid=3DTXT_TAGLM_WL_t2_hm_justgotbetter_expl=
ore_012009=
 
S

Sebastian Hungerecker

Jason said:
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
 
M

Matthew Moss

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.


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
 
J

Jonathon Brenner

Use "eval".

irb(main):001:0> def one
irb(main):002:1> puts "1"
irb(main):003:1> end
=3D> nil
irb(main):004:0> def two
irb(main):005:1> puts "2"
irb(main):006:1> end
=3D> nil
irb(main):007:0> metharray =3D ['one', 'two']
=3D> ["one", "two"]
irb(main):008:0> metharray.each { |e| eval e }
1
2
=3D> ["one", "two"]


Hello,

I currently have an array of strings. Let's say, for example, my array c= ontains 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 progr=
am named 'one'. I am now trying numArray[0]. Is there a way to make the n=
umArray[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
_________________________________________________________________
Windows Live=99 Hotmail(R):=85more than just e-mail.
http://windowslive.com/explore?ocid=3DTXT_TAGLM_WL_t2_hm_justgotbetter_ex=
plore_012009
 
D

David A. Black

Hi --

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.


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!
 
M

Matthew Moss

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

David A. Black

Hi --

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!
 
J

Julian Leviston

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/

Jason said:
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
 
S

Sebastian Hungerecker

Julian said:
Wouldn't you have to pass in the calling context (self from the =A0
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=20
without an explicit receiver. So no, you don't.

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top