From: Florian Gross Date: 2004-07-19T08:02:29+09:00 Subject: Re: Current method James Britt wrote: > Here is an example based on something I believe Rick Kilmer posted here > some time ago, plus some additional neat Ruby magic for dynamic code > tracing (also from Rich, I think, but I might be wrong on that). > > # Eval a code block using the original context of the variables > def trace( &block ) > expr = block.call > puts "trace: #{expr} = #{eval(expr, block)}" > end > trace { "x + y + z" } # Show this to Java(tm) fans. Using Binding.of_caller (available from http://noegnud.sourceforge.net/flgr/binding_of_caller.rb) this can be simplified to trace "x + y + z": require 'binding_of_caller' def trace(expr) Binding.of_caller do |context| result = eval(expr, context) puts "trace: #{expr} = #{result.inspect}" return result end end I'm using it like this: irb(main):026:0> x = 2; y = 3; trace "(x + y) ** (x * y)" trace: (x + y) ** (x * y) = 15625 => 15625 Regards, Florian Gross