iterate through an array of method names to an object

S

Skye Weir-mathewes

I've been trying to create a iterator that will run through and array of
method names, sending each one to an object. For some reason the object
doesn't like the method names if I send them via an iterator, but if I
spell each one out, it works fine. When I try to use the iterator I get
and error indicating that ruby doesn't think my method names actually
name a method.

Here's some of my code:

functions = ["addHeadersAndFooters", "addListNameToSubject", "admin",
"allowCrossPosting ", "allowDuplicatePosts ", "allowInfo ",
"anyoneCanPost "]

functions.each do |funk|
puts name_o_my_object.funk
end

gives me the following error:

undefined method `funk' for #<SOAP::Mapping::Object:0x5585bd8>
(NoMethodError)

but if I write somthing like:

functions.each do |funk|
puts name_o_my_object.addHeadersAndFooters
end

the iterator works fine, so... what's up with that?
 
S

Stefano Crocco

Alle luned=C3=AC 16 aprile 2007, Skye Weir-mathewes ha scritto:
I've been trying to create a iterator that will run through and array of
method names, sending each one to an object. For some reason the object
doesn't like the method names if I send them via an iterator, but if I
spell each one out, it works fine. When I try to use the iterator I get
and error indicating that ruby doesn't think my method names actually
name a method.

Here's some of my code:

functions =3D ["addHeadersAndFooters", "addListNameToSubject", "admin",
"allowCrossPosting ", "allowDuplicatePosts ", "allowInfo ",
"anyoneCanPost "]

functions.each do |funk|
puts name_o_my_object.funk
end

gives me the following error:

undefined method `funk' for #<SOAP::Mapping::Object:0x5585bd8>
(NoMethodError)

but if I write somthing like:

functions.each do |funk|
puts name_o_my_object.addHeadersAndFooters
end

the iterator works fine, so... what's up with that?

In the first case, you write name_o_my_object.funk. In this case, ruby trie=
s=20
to call a method called funk on the object name_o_my_object. What you need =
to=20
do is:

puts name_o_my_object.send(funk)

I hope this helps

Stefano
 
C

cammo

But also the send method will expect to get a symbol not a string so
you'll need to do something like

functions.each do |funk|
funk.to_sym
puts name_o_my_object.send(funk)
end

I tried that in irb and it worked so fingers crossed should work for
you too.

Cam


Alle lunedì 16 aprile 2007, Skye Weir-mathewes ha scritto:


I've been trying to create a iterator that will run through and array of
method names, sending each one to an object. For some reason the object
doesn't like the method names if I send them via an iterator, but if I
spell each one out, it works fine. When I try to use the iterator I get
and error indicating that ruby doesn't think my method names actually
name a method.
Here's some of my code:
functions = ["addHeadersAndFooters", "addListNameToSubject", "admin",
"allowCrossPosting ", "allowDuplicatePosts ", "allowInfo ",
"anyoneCanPost "]
functions.each do |funk|
puts name_o_my_object.funk
end
gives me the following error:
undefined method `funk' for #<SOAP::Mapping::Object:0x5585bd8>
(NoMethodError)
but if I write somthing like:
functions.each do |funk|
puts name_o_my_object.addHeadersAndFooters
end
the iterator works fine, so... what's up with that?

In the first case, you write name_o_my_object.funk. In this case, ruby tries
to call a method called funk on the object name_o_my_object. What you need to
do is:

puts name_o_my_object.send(funk)

I hope this helps

Stefano
 
S

Stefano Crocco

Alle marted=EC 17 aprile 2007, cammo ha scritto:
But also the send method will expect to get a symbol not a string so
you'll need to do something like

functions.each do |funk|
=A0 =A0 funk.to_sym
=A0 =A0 puts name_o_my_object.send(funk)
=A0 end

I tried that in irb and it worked so fingers crossed should work for
you too.

Cam

send accepts both a string or a symbol. The documentation for Object#send,=
=20
states

object.send(symbol, [args...])

and the documentation of Object class, says:

In the descriptions of Object's methods, the parameter symbol refers to a=20
symbol, which is either a quoted string or a Symbol

Besides, your own code passes a string to send, since funk.to_sym returns a=
=20
symbol, but doesn't change what is stored inside funk, so when funk is pass=
ed=20
to send, it still contains a string.

Stefano
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top