# Printing the contents of variables to the screen legibly

**URL:** <https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184>\
**Category:** Rails\
**Created:** [May 20, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184 "2008-05-20T22:08:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 20, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/1 "2008-05-20T22:08:48Z")

</div>

Hi there,

I am a recent convert from PHP, specifically CakePHP. When I would run  
a query in CakePHP and read that query into an array, I could run a  
function debug() on that array and it would print all of the contents  
of that array very neatly on the screen. If I run a query like @order  
= Order.find(1), it reads all of the order information as well as the  
related line\_items information into the @order variable, but when i  
run @order.inspect I can only see the order information (and not the  
associated line\_items) and it also reads it out in one long string  
instead of each hashed value on a new line etc… Is there a better  
method than inspect to print the contents of a variable more legibly  
to the screen?

Thanks!

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 20, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/2 "2008-05-20T22:16:31Z")

</div>

Rails also has a debug() function. Not sure if that’d get you child  
elements automatically tho…

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 20, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/3 "2008-05-20T23:36:34Z")

</div>

On 20 May 2008, at 21:15, Pardee, Roy wrote:

> Rails also has a debug() function. Not sure if that’d get you child  
> elements automatically tho…

debug is fine in a view, at the console pp (pretty print) is nice too  
(you need to require ‘pp’ first)

Fred

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 21, 2008, 12:03am UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/4 "2008-05-21T00:03:00Z")

</div>

is there anyway to see the related information as well? Like a query  
that returns a post and all related comments. Debug only seems to show  
the post but not the comments.

On May 20, 5:38 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 20, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/5 "2008-05-20T23:38:35Z")

</div>

validkeys wrote:  
Is there a better

> method than inspect to print the contents of a variable more legibly  
> to the screen?

If I’m in a view I use Rails’ debug method. Everywhere else (including  
the logger) I use object.to\_yaml.

Works beautifully.

---

<div class="post-metadata">

**Author:** ![validkeys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/da6949/32.png) [@validkeys](https://www.ruby-forum.com/u/validkeys)\
**Post date:** [May 21, 2008, 12:32am UTC](https://www.ruby-forum.com/t/printing-the-contents-of-variables-to-the-screen-legibly/138184/6 "2008-05-21T00:32:47Z")

</div>

validkeys wrote:

> is there anyway to see the related information as well? Like a query  
> that returns a post and all related comments. Debug only seems to show  
> the post but not the comments.
> 
> On May 20, 5:38 pm, Daniel W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

It will show whatever is held in memory when dumped. If you have a post  
with comments try…

post.comments  
debug post

And it should spit out everything.
