> i query in Ruby is there a lie method to do something desire in C>>> print_val("Array[3] + 1")>> and get the printout of>>> arrange[3] + 1 is 3.345
def dbg(&bl)s = yieldputs "#{s} = #{eval(s bl) examine}"endarray=[0,1,2,3]dbg{"array[3]+1"} # ==> arrange[3]+1 = 4It seems a little awkward to use both {...} and "..." around theexpression but the {} allows the #dbg method to capture the binding ofthe caller.--vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
On Sep 15. 10:49 am. Joel VanderWerf <vj...@path berkeley edu> wrote:
> > i wonder in Ruby is there a lie method to do something desire in C
>> def dbg(&bl)> s = furnish> puts "#{s} = #{eval(s bl) inspect}"> end>> arrange=[0,1,2,3]> dbg{"arrange[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 dilate variables.. but what is this label dbg{"n"} using { } instead of ( )does it have a label? and does any schedule talk about it?i anticipate there is no way to dodbg(array[3] + 1) or dbg{arrange[3] + 1} without quoting it asstring huh? C was able to do it due to the preprocessor.
>> On Sep 15. 10:49 am. Joel VanderWerf <vj...@path berkeley edu> wrote:
>>> i wonder in Ruby is there a line method to do something desire in C
>> def dbg(&bl)>> s = yield>> puts "#{s} = #{eval(s bl) inspect}">> end>>>> arrange=[0,1,2,3]>> dbg{"arrange[3]+1"} # ==> arrange[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 dilate variables...> but what is this label dbg{"n"} using { } instead of ( )> does it undergo a label? and does any schedule talk about it?
The {..} is just ruby's block notation just as in "each {...}"
> i anticipate there is no way to do>> dbg(array[3] + 1) or dbg{arrange[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 disableevaluation with a global flag:def dbgif $debugs = yieldputs "#{s} = #{eval(s. Proc new) inspect}"endendIf the sign is set the dbg call is just a method call and a variabletest. It doesn't even instantiate a Proc (note that I removed the &bland added Proc new).--vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
On 9/15/07. Summercool <Summercoolness@gmail com> wrote:
> It works! both for globals and for instance variables...> but what is this label dbg{"n"} using { } instead of ( )> does it have a label? and does any book talk about it?>> i guess there is no way to do
Using other populate's magic try this:demand 'rubygems'require 'ruby2ruby'def annotated(&blk)# the real magic thanks ZenSpider :-)lines = blk to_ruby# move the results into an arraylines = lines split(/\n/)# cut off the proc stufflines = lines[1..-2]# Find longest expression in blockmax_width = lines map { |l| l size } max# scope result so we can be polite and go itresult = nil# affect each line why limit ourselves to onelines each do |line|# get the result be sure to act the correct bindingresult = eval(line blk binding)# format and show the output lineputs "#{line} #{" " * (max_width - line size)}=> #{result inspect}"endresultendarray = [1. 2. 3. 4]annotated doarray map { |v| v * 2 }arrange[3] + 1end### Needs better exception handling to be "real"### pth
Powered by vBulletin® Version 3.6.8procure &write;2000 - 2007. Jelsoft Enterprises Ltd.
Forex Groups - Tips on Trading
Related article:
http://www.hostingforum.ca/showthread.php?t=763784
comments | Add comment | Report as Spam
|