From: Eric Mahurin Date: 2005-09-10T02:34:40+09:00 Subject: Re: quick print type debugging --- Florian Gro� wrote: > Eric Mahurin wrote: > > > What would be nice is if you could somehow convert the > block > > back to text (and put it in the debug message). Anybody > know > > how to do that? I know I could make the block be a string > of > > the code instead to solve the problem (d{"..."}), but that > > would be uglier and less efficient when we have debugging > off > > (because we have to eval). > > See attachment. It's not very general, though. Perfect. This was from a ruby quiz, right? Now here is the new method (didn't need binding_of_caller.rb): require 'proc_source.rb' $DEBUG_LEVEL = ENV['DEBUG']||0 module Kernel def d(level=1,&block) if $DEBUG_LEVEL>=level ret = yield filename,lineno = block.source_descriptor $stderr.printf("DEBUG: %s:%d {%s} => %s\n", filename,lineno,block.source,ret) ret else yield end end end $DEBUG_LEVEL = 1 x = d{1+2} + 3*4 - d{5/6} DEBUG: (irb):24 {} => 3 DEBUG: (irb):24 {} => 0 => 15 Didn't work. Any idea why, Florian? I tried aProc.source after using lambda to create it and it worked (also in irb). But, with a block it doesn't seem to. > I think I'd go with the string solution, use > Binding.of_caller to put it > into the right context and alias it to a method that does > nothing when > $DEBUG isn't set. I need to evaluate the block even without debugging because the result will be used as part of an expression independent of debugging mode (see above). __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com