When arg is a variable, can the variable's name be displayed?

  • Thread starter Richard Lionheart
  • Start date
R

Richard Lionheart

Hi,

For debugging purposes, I'd like to write a routine to dump a variables
name and value, sort of like Object#inspect but different. I'd like to
write:

def dumpv(var)
$stdin.print var.name + ' = "' + v.to_s + "\"\n"
$stdin.flush
end

v1 = 'abc'
dump(v1) # ==> v1 = "abc"

v2 = 123
dump(v2 ) # ==> v2 = "123"

I recognize that this simple thing, if it worked for strings and numbers,
probably wouldn't work for some more complex types, but I'll worry about
that another day :)

And I acknowledge this isn't the world's greatest debugging tool, but it'll
help me develop some tool-writing skills in Ruby.

TIA,
Richard
 
J

Jeff Mitchell

--- Richard Lionheart said:
Hi,

For debugging purposes, I'd like to write a routine to dump a variables
name and value, sort of like Object#inspect but different. I'd like to
write:

I put the following on http://www.rubygarden.org/ruby?OneLiners

def show(&block)
printf("%-25s>> %s\n", expr = block.call, eval(expr, block.binding).inspect)
end

Example:

show {%{ a = [1,2,3] }} ;
show {%{ a.slice(1,2) }} ;
show {%{ a.map { |x| x**3 } }}

produces:

a = [1,2,3] >> [1, 2, 3]
a.slice(1,2) >> [2, 3]
a.map { |x| x**3 } >> [1, 8, 27]







__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/
 
K

Kaspar Schiess

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello Richard,

Close but no cigar:

def debug(s)
set_trace_func proc { |event, file, line, id, b, classname|
if (event == 'return')
begin
puts "#{s} = #{eval(s, b).inspect}"
rescue NameError
puts "#{s} is undefined"
end
end
set_trace_func(nil)
}
end

require 'test/unit'

class DebugTest < Test::Unit::TestCase

def puts(str)
@@last = str
end

def test_local
a = [1,2,3]
debug 'a'

assert_equal 'a = [1, 2, 3]', @@last
end

def test_instance
@a = [1,2,3]
debug '@a'

assert_equal '@a = [1, 2, 3]', @@last
end

def test_class
@@a = [1,2,3]
debug '@@a'

assert_equal '@@a = [1, 2, 3]', @@last
end

def test_global
$a = [1,2,3]
debug '$a'

assert_equal '$a = [1, 2, 3]', @@last
end
end

best regards,


- --
kaspar

semantics & semiotics
code manufacture

www.tua.ch/ruby
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAwy2xFifl4CA0ImQRAtA2AJ0YzhaaRmwac+berljlc+7FetqrAwCfXd8A
SbQ6siHcMYw4hcfTsBJkNJ8=
=IdAe
-----END PGP SIGNATURE-----

--
kaspar

semantics & semiotics
code manufacture

www.tua.ch/ruby
 
R

Richard Lionheart

Hi Jeff and Kaspar,

Thanks for your responses. At first blush, they don't seem to do what I
had in mind. However, they have Ruby constructs I haven't dealt with, so I
need to read their documentation and "play" with them a little. I've got to
travel for the next couple of days, so I'll take along my Ruby books and
print-outs of your missives. I'll post back later in the week either
reporting I've had an "Aha! moment" or seek additional comments.

Again, thanks for posting.

Regards,
Richard
 
R

Robert Klemme

Richard Lionheart said:
Hi,

For debugging purposes, I'd like to write a routine to dump a variables
name and value, sort of like Object#inspect but different. I'd like to
write:

def dumpv(var)
$stdin.print var.name + ' = "' + v.to_s + "\"\n"
$stdin.flush
end

Btw: you didn't mean to use $stdin for printing, do you?

def dumpv(varname, env)
print varname.to_s, ' = "', eval(varname.to_s, env), "\"\n"
end

v1 = 'abc'
dumpv :v1, binding

Regards

robert
 
J

Jean-Hugues ROBERT

Btw: you didn't mean to use $stdin for printing, do you?

def dumpv(varname, env)
print varname.to_s, ' = "', eval(varname.to_s, env), "\"\n"
end

v1 = 'abc'
dumpv :v1, binding

Regards
robert

This was mentionned before:

def dumpv( &block )
expr = block.call()
print expr, ' = "', eval( expr, block), "\"\n"
end

v1 = 'abc'
dumpv{ :v1}

That the same but thanks to the fact that a Proc is an OK binding
for eval... you avoid the extra binding parameter.

Yours,

JeanHuguesRobert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top