Printing the contents of variables to the screen legibly

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!

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

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

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. [email protected]

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.

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. [email protected]

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs