E
Eric Mahurin
Anybody think something like this would be useful?
module Kernel
def d(message=3D"",&block)
ret =3D block.call
$stderr.puts("DEBUG: #{block.inspect} #{message} =3D>
#{ret}") if $DEBUG
ret
end
end
irb(main):020:0> $DEBUG=3Dtrue
=3D> true
irb(main):019:0> d{1+2*3}
DEBUG: #<Proc:0xb7df9adc@(irb):19> =3D> 7
=3D> 7
irb(main):020:0> $DEBUG=3Dfalse
=3D> false
irb(main):021:0> d{1+2*3}
=3D> 7
If you have an expression to want to see the result of, you
just surround it with d{...}. The block.inspect gives you the
filename and line number and you get the evaluated value. No
need to have a separate "p" or "puts" statement (and possibly
assignment to a variable) because this returns the value.
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).
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
module Kernel
def d(message=3D"",&block)
ret =3D block.call
$stderr.puts("DEBUG: #{block.inspect} #{message} =3D>
#{ret}") if $DEBUG
ret
end
end
irb(main):020:0> $DEBUG=3Dtrue
=3D> true
irb(main):019:0> d{1+2*3}
DEBUG: #<Proc:0xb7df9adc@(irb):19> =3D> 7
=3D> 7
irb(main):020:0> $DEBUG=3Dfalse
=3D> false
irb(main):021:0> d{1+2*3}
=3D> 7
If you have an expression to want to see the result of, you
just surround it with d{...}. The block.inspect gives you the
filename and line number and you get the evaluated value. No
need to have a separate "p" or "puts" statement (and possibly
assignment to a variable) because this returns the value.
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).
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20