James Coglan
6/1/2009 8:46:00 AM
[Note: parts of this message were removed to make it a legal post.]
2009/6/1 Steve Hull <p.witty@gmail.com>
> Hey ruby wizards. I got a question for you. I want to call a method on
> my object instance, with the method name specified by a string. I want
> to *pass in* a hash to my method.
>
> I tried:
>
> myMethodName = "doSomeStuff"
> myObject.instance_eval "#{myMethodName}(#{myHash})"
>
> But it complained that it didn't have a method named #{myHash.to_s}.
>
> So I tried myObject.instance_eval { myMethodName(myHash) }
>
> But it compalined that it didn't have a method named "myMethodName"
> (which is obvious -- the method I wanted to call is doSomeStuff).
>
> I'm new to metaprogramming in ruby. What am I missing here?
myObject.__send__(myMethodName, myHash) is a more appropriate idiom for what
you seem to be doing.