How to get Ruby objects printed out in their 'natural' form

For debugging purposes, I’d like to be able to do something like

print [1,2,3]

and have “[1,2,3]” printed to the screen, not 123. How do I go about
this. I’ve experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken

Kenneth McDonald wrote:

For debugging purposes, I’d like to be able to do something like

print [1,2,3]

and have “[1,2,3]” printed to the screen, not 123. How do I go about
this. I’ve experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken

C:\Documents and Settings\chen73>irb
irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> p a
[1, 2, 3]
=> nil
irb(main):003:0>

Li

Kenneth McDonald wrote:

For debugging purposes, I’d like to be able to do something like

print [1,2,3]

and have “[1,2,3]” printed to the screen, not 123. How do I go about
this. I’ve experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken

Each object has method inspect and it does the trick. The irb in fact
prints the result’s inspect to show it to you.

[“a”,:b,3].inspect #=> “[“a”, :b, 3]”

TPR.

On Wed, Sep 24, 2008 at 12:49 AM, Kenneth McDonald
[email protected] wrote:

For debugging purposes, I’d like to be able to do something like

print [1,2,3]

and have “[1,2,3]” printed to the screen, not 123. How do I go about this.
I’ve experimented with to_s and puts, and not found anything obvious.

p [1,2,3]
puts [1,2,3].inspect

The last is useful if you want to get the string representation of the
Ruby object, e.g. if you’re using a logger, so you can do something
like:

log.debug(obj.inspect)