On Thu, Feb 10, 2011 at 7:35 AM, Mario R. [email protected] wrote:
I know that I could pass mi(“4==3”) and in the method use eval(param)
but that’s not possible in my case, I need to give a real comparison.
mi(4==3) sees the 4==3 evaluated before #mi is called. So, the method
call ends up being mi(false).
Consider the following:
def mi(bool)
puts bool
end
mi(4==3)
If you look at the AST that is generated from that code, this is what
you get:
s(:block,
s(:defn,
:mi,
s(:args, :bool),
s(:scope, s(:block, s(:call, nil, :puts, s(:arglist, s(:lvar,
:bool)))))),
s(:call,
nil,
:mi,
s(:arglist, s(:call, s(:lit, 4), :==, s(:arglist, s(:lit, 3))))))
Pay particular attention to the second part. I have reformatted it to
emphasize a few things:
s(:call, nil, :mi,
s(:arglist,
s(:call, s(:lit, 4), :==,
s(:arglist, s(:lit, 3))))))
Look at the 3rd and 4th lines there. The #== method is being called on
4, with an arglist composed of 3. That method call happens first, and
the result of it is the argument placed into the #mi method call’s
arglist.
Depending on your actual use case, and on the ruby version that you
are using, you may be about to use ParseTree (gem install ParseTree)
to get the information that you want, but you might get better
guidance if you explain what your goal in wanting to do this actually
is.
Kirk H.
Software Engineer
Engine Y.