From: Steve Hull
Date: 2009-06-02T09:12:31+09:00
Subject: Re: eval? instance_eval?? hmm
James -- perfect. Thanks!
Brian Candler wrote:
> You can easily see what you've done wrong if you just use 'puts' instead
> of 'myObject.instance_eval'
>
> Here's it's because you turned the hash into a string, and then inserted
> the string into your code, and then tried to run it.
Believe it or not, I did realize that. I just couldn't find any docs
showing me the proper usage of the string form of "instance_eval" (ie,
passing an argument).
> irb(main):005:0> instance_eval "#{myMethodName}(myHash)"
> got {"one"=>1}
> => nil
See I thought about trying this, but didn't even bother, as I expected
it to fail. Anyway I'm glad to see it does indeed work like this.
> However there are better ways to achieve this, without using the string
> version of eval. Probably all you want is this:
>
> myObject.send(myMethodName, myHash)
Yup. This works perfectly, thanks!!
> This isn't metaprogramming (metaprogramming = programs which write
> programs). This is just dynamic method dispatch.
Hah yeah I realize that now... cut me some slack, it was ~1:30am my
time and the wife was harassing me to come to bed! ;)
Anyway, as you may have guessed, I'm new to Ruby in general. Only been
working with the language for about 3 months. So far I'm loooooving it.
:)
Thanks for the helpful replies! I got it working exactly how I wanted.
-Steve
--
Posted via http://www.ruby-forum.com/.