One line to print the statement AS WELL AS the evaluated val

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

On Sep 15, 2007, at 1:51 AM, 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

All you need is something like:

puts "myArray[3] + 1 is #{myArray[3] + 1} "

String interpolation. Great stuff.
Of course you have to create that array first to have something in it.

You could also do:

puts "myArray[3] + 1 is " + (myArray[3] + 1).to_s

Lots of ways, but the first one is most succinct.
Ruby is very flexible!
You can even go from this into a DSL with words that evaluate to
expressions.

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.

On Sep 15, 2007, at 7:11 AM, Dan Z. wrote:

Here you go:
def print_val(str)
puts “#{str} is #{eval(str)}”
end

That didn’t work for me. I had modify it like so:

foo = [1, 2, 3, 4, 5]
def print_val(str)
puts “#{str} is #{eval(str, TOPLEVEL_BINDING)}”
end
print_val(“foo[3] + 1”)

Regards, Morton

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

Here you go:
def print_val(str)
puts “#{str} is #{eval(str)}”
end

This should do what you want. Be wary of side effects, though. For
example, to display the value of “a += 3” requires evaluation, and that
changes the value of “a” (and when you remove this debugging line, your
program no longer works the way you expect).

Have fun,
Dan

On Sep 15, 10:49 am, Joel VanderWerf [email protected] wrote:

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.

On 15.09.2007 19:13, John J. wrote:

Array[3] + 1 is 3.345

def print_val(expr,b=binding)
print( expr, " is ", eval(expr,b).inspect, “\n”)
end

irb(main):026:0> a=%{foo bar baz}
=> “foo bar baz”
irb(main):027:0> print_val “a[1]”, binding
a[1] is 111
=> nil
irb(main):028:0> print_val “a[1]”
NameError: undefined local variable or method a' for main:Object from (irb):21:inprint_val’
from (irb):21:in `print_val’
from (irb):28
from :0

You can even omit the “b” argument if you use something like
“Binding.of_caller”.

Kind regards

robert

Summercool wrote:

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).

Morton G. wrote:

def print_val(str)
puts “#{str} is #{eval(str, TOPLEVEL_BINDING)}”
end
print_val(“foo[3] + 1”)

Regards, Morton

You’re right, thanks. When I tested this, I used the first poster’s
variable name, “Array”. So it worked for me, but only because I happened
to use a name with global slope.

Dan

On Sep 15, 5:22 am, Morton G. [email protected] wrote:

That didn’t work for me. I had modify it like so:

foo = [1, 2, 3, 4, 5]
def print_val(str)
puts “#{str} is #{eval(str, TOPLEVEL_BINDING)}”
end
print_val(“foo[3] + 1”)

it works but then if print_val is called inside an instance method,
then it won’t work.

On 9/15/07, Summercool [email protected] 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 :slight_smile:

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