------art_54898_23635811.1176777767818
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 4/16/07, Stefano Crocco <stefano.crocco / alice.it> wrote:
>
> Alle luned16 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


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.

------art_54898_23635811.1176777767818--