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

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 :slight_smile:

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

···

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 6/4/2004

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]

···

--- Richard Lionheart <NoOne@Nowhere.com> wrote:

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:

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

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

···

--
kaspar

semantics & semiotics
code manufacture

www.tua.ch/ruby

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

···

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 6/4/2004

"Richard Lionheart" <NoOne@Nowhere.com> schrieb im Newsbeitrag
news:9aKdnRz0R5mAj17dRVn-jg@comcast.com...

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

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 :slight_smile:

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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 6/4/2004

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

···

At 17:28 07/06/2004 +0900, you wrote:

"Richard Lionheart" <NoOne@Nowhere.com> schrieb im Newsbeitrag
news:9aKdnRz0R5mAj17dRVn-jg@comcast.com...

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

-------------------------------------------------------------------------
Web: @jhr is virteal, virtually real
Phone: +33 (0) 4 92 27 74 17