From: Austin McDonald Date: 2004-06-12T16:47:32+09:00 Subject: Re: Any way to get the calling method's binding? Right. I'm out of it at the moment; got wrapped up in following the idea trails and wound up where Gavin started... my apologies. I think I'll get some sleep. Austin Austin McDonald wrote: > Excellent; I had the right idea, but yours is much cleaner. > > For giggles, and maybe b/c it's a little more explicit, you could use > Kernel#binding to make it obvious what's going on: > > def trace(expr, b) > LOG.debug "#{expr} = #{eval(expr, b)}" > end > > def something() > x=5 > y=7 > trace "x+y", binding > end > > something # ==> x+y = 12 > > Although I like Joel's version better, I think this is a little clearer. > > Austin > > > > Joel VanderWerf wrote: > >> Austin McDonald wrote: >> >>> In the meantime, you could do this: >>> >>> def trace (expr) >>> LOG.debug "#{expr} = #{yield expr}" >>> end >>> >>> def something >>> x=5 >>> x=7 >>> trace "x+y", {|n| eval n} >>> end >>> >>> something => 12 >> >> >> >> def trace(&block) >> expr = block.call >> puts "#{expr} = #{eval(expr, block)}" >> end >> >> def something >> x = 5 >> y = 7 >> trace {"x + y"} >> end >> >> something # ==> x + y = 12 >> >> >> > >