|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
kendear wrote:
> i wonder in Ruby, is there a line method to do something like in C > > > print_val("Array[3] + 1") > > and get the printout of > > > Array[3] + 1 is 3.345 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. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Summercool wrote:
> > 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? The {..} is just ruby's block notation, just as in "each {...}" > 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. Not easy. Another advantage of using the block notation is that you can disable evaluation with a global flag: def dbg if $debug s = yield puts "#{s} = #{eval(s, Proc.new).inspect}" end end If the flag is set, the dbg call is just a method call and a variable test. It doesn't even instantiate a Proc (note that I removed the &bl and added Proc.new). -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 9/15/07, Summercool <Summercoolness@gmail.com> wrote:
> 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 Using other people's magic try this: require 'rubygems' require 'ruby2ruby' def annotated(&blk) # the real magic, thanks ZenSpider :-) lines = blk.to_ruby # Turn the results into an array lines = lines.split(/\n/) # Chop off the proc stuff lines = lines[1..-2] # Find longest expression in block max_width = lines.map { |l| l.size }.max # scope result, so we can be polite and return it result = nil # Process each line, why limit ourselves to one lines.each do |line| # get the result, be sure to keep the correct binding result = eval(line, blk.binding) # format and display the output line puts "#{line} #{" " * (max_width - line.size)}=> #{result.inspect}" end result end array = [1, 2, 3, 4] annotated do array.map { |v| v * 2 } array[3] + 1 end ### Needs better exception handling to be "real" ### pth |
|
![]() |
| Outils de la discussion | |
|
|