# Iterate through an array of method names to an object

**URL:** <https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186>\
**Category:** Ruby\
**Created:** [April 16, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186 "2007-04-16T23:11:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 16, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/1 "2007-04-16T23:11:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 16, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/2 "2007-04-16T23:16:47Z")

</div>

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

> "allowCrossPosting ", "allowDuplicatePosts ", "allowInfo ",
> 
> 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

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 16, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/3 "2007-04-16T23:31:38Z")

</div>

That totally worked, thank you so much

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 17, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/4 "2007-04-17T14:32:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 17, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/5 "2007-04-17T14:39:10Z")

</div>

Alle martedì 17 aprile 2007, cammo ha scritto:

> Cam

send accepts both a string or a symbol. The documentation for  
Object#send,  
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  
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  
symbol, but doesn’t change what is stored inside funk, so when funk is  
passed  
to send, it still contains a string.

Stefano

---

<div class="post-metadata">

**Author:** ![sheepfunk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f1d935/32.png) [@sheepfunk](https://www.ruby-forum.com/u/sheepfunk)\
**Post date:** [April 17, 2007, 4:43am UTC](https://www.ruby-forum.com/t/iterate-through-an-array-of-method-names-to-an-object/92186/6 "2007-04-17T04:43:10Z")

</div>

On 4/16/07, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > undefined method `funk’ for #SOAP::Mapping::Object:0x5585bd8  
> > 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

Just to elaborate on Stefano’s response a little, consider this  
scenario:

class A  
def funk  
“funk”  
end  
end

a = A.new  
p a.funk # should this be an error since there is no variable called  
funk?

functions.each do |funk|  
p a.funk # which funk do you think this should refer to?  
end

Clearly it has to be one or the other. For the code you posted to work  
we’d  
have to do something like  
a.(“funk”) or  
f = “funk”  
a.f

everytime we wanted to call a method.
