From: Kirk Haines Date: 2011-02-11T00:36:26+09:00 Subject: Re: print comparison On Thu, Feb 10, 2011 at 7:35 AM, Mario Ruiz wrote: > is it possible to print the comparison given as parameter in a method? > > something like this: > > mi(4==3) > def mi(param) >  I want to know here that the param was 4==3 > end > > > 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 Haines Software Engineer Engine Yard