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

**URL:** <https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332>\
**Category:** Ruby\
**Created:** [September 23, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332 "2008-09-23T18:49:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 23, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332/1 "2008-09-23T18:49:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 23, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332/2 "2008-09-23T18:53:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 23, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332/3 "2008-09-23T19:04:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 24, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-get-ruby-objects-printed-out-in-their-natural-form/149332/4 "2008-09-24T18:49:51Z")

</div>

On Wed, Sep 24, 2008 at 12:49 AM, Kenneth McDonald  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)
