# Pretty-printing (or otherwise) a hash in a view template

**URL:** <https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638>\
**Category:** Rails\
**Created:** [October 29, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638 "2007-10-29T18:22:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 29, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638/1 "2007-10-29T18:22:25Z")

</div>

I have some text boxes into which i want to output the contents of some  
hashes, in the nice readable way we all know and love. Can i use  
pretty-print for this? As far as i can tell, pp only works to standard  
output and can’t be used to create text.

Is there another way to nicely print out the contents of a hash?

thanks!  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 29, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638/2 "2007-10-29T19:09:50Z")

</div>

Max W. wrote:

> I have some text boxes into which i want to output the contents of some  
> hashes, in the nice readable way we all know and love. Can i use  
> pretty-print for this? As far as i can tell, pp only works to standard  
> output and can’t be used to create text.
> 
> Is there another way to nicely print out the contents of a hash?
> 
> thanks!  
> max

I actually found out how to do this from another post somewhere - you  
can get the output of pp into a string with

text = PP.pp(myhash, “”)

The resultant string has any quotes inside it escaped out.

thanks

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 29, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638/3 "2007-10-29T21:30:21Z")

</div>

On 10/29/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there another way to nicely print out the contents of a hash?

Give to\_yaml a shot. It’s a very readable way to dump ruby objects.

–  
James M. | removed\_email\_address@domain.invalid  
Ruby and Ruby on Rails consulting  
[blog.restphone.com](http://blog.restphone.com)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 30, 2007, 11:46am UTC](https://www.ruby-forum.com/t/pretty-printing-or-otherwise-a-hash-in-a-view-template/118638/4 "2007-10-30T11:46:28Z")

</div>

James M. wrote:

> On 10/29/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Is there another way to nicely print out the contents of a hash?
> 
> Give to\_yaml a shot. It’s a very readable way to dump ruby objects.
> 
> –  
> James M. | removed\_email\_address@domain.invalid  
> Ruby and Ruby on Rails consulting  
> [blog.restphone.com](http://blog.restphone.com)

That’s not what i’m after, though i agree that yaml is nice and  
readable. thanks anyway. 🙂

In addition to PP.pp(myhash, “”), btw, i discovered that myhash.inspect  
also works.
