From: Stefan Rusterholz Date: 2007-10-19T23:44:44+09:00 Subject: Re: the level of Ruby programmers vs PHP's Ben Giddings wrote: > On Oct 19, 2007, at 04:32, SpringFlowers AutumnMoon wrote: >> print_debug(2**3) >> print_debug(8**(1.0/3)) >> print_debug(a + 2.038) >> [...] >> >> it will print out the expression as well as the value at the same >> time, >> as a convenience for testing and for debugging. > > This is one of the few reasons I wish Ruby had macros. Having a > macro like this can be very useful, but it isn't possible with a > function/method because the argument gets evaluated before being > passed to the context of the function/method. In the above examples > print_debug would be passed 3, 8, 2.0 and some float value. > > I don't know if it would be possible to have some special macroish > functionality in Ruby that would allow "print_debug()" but not open > the can of worms that is macros. > > Ben Ugly solution: def debug_eval(code, binding) puts "#{code}\n=> #{eval(code, binding)}" end a = 3 b = 5 debug_eval(%{a*b}, binding) Though IMHO all you need when you print the debug output of some statement is a label, such as p [:value_of_foo, foo_expression] That's usually sufficient here. I mean, when you use such a statement you *know* what expression you're inspecting. At least I seriously hope that such statements are only used for short time inspection and for that timeframe, your memory should be sufficient, no? Regards Stefan -- Posted via http://www.ruby-forum.com/.