Re: one line to print the statement AS WELL AS the evaluated value like in C
On Sep 15, 10:49 am, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> kendear wrote:
> > i wonder in Ruby, is there a line method to do something like in C
>
> def dbg(&bl)
> s = yield
> puts "#{s} = #{eval(s, bl).inspect}"
> end
>
> array=[0,1,2,3]
> dbg{"array[3]+1"} # ==> array[3]+1 = 4
>
> It seems a little awkward to use both {...} and "..." around the
> expression, but the {} allows the #dbg method to capture the binding of
> the caller.
It works! both for globals and for instance variables...
but what is this call dbg{"n"} using { } instead of ( )
does it have a name? and does any book talk about it?
i guess there is no way to do
dbg(array[3] + 1) or dbg{array[3] + 1} without quoting it as
string huh? C was able to do it due to the preprocessor.
|