Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: How to make IRB print structure for Arrays and Hashes

Tags:

ruby

irb

When I make a new array/hash in irb, it prints out a nice format to show the structure, ex.

["value1", "value2", "value3"] {"key1" => "value1"} 

... but when I try to print out my variables using puts, I get them collapsed:

value1 value2 value3 key1 value1 

I gather that puts is not the right command for what I want, but what is? I want to be able to view my variables in irb in the first format, not the second.

like image 699
neezer Avatar asked Mar 31 '09 21:03

neezer


1 Answers

You can either use the inspect method:

a=["value1", "value2", "value3"] puts a.inspect 

Or, even better, use the pp (pretty print) lib:

require 'pp' a=["value1", "value2", "value3"] pp a 
like image 135
dmondark Avatar answered Nov 23 '22 13:11

dmondark