Fellow Stranger
Fellow Stranger

Reputation: 34013

Print hash in view with new line for each key-value pair

Sometimes I choose to print out params or an object in a view, but when loaded with data it cannot be hard to visually scan.

Is their any "trick" to easily print a hash such that each key-value pair is printed on a new line?

In the console I use y my_hash.

Upvotes: 2

Views: 6988

Answers (2)

sscirrus
sscirrus

Reputation: 56719

Here's a very simple alternative:

<% hash.each do |k,v| %>
  <%= k %>: <%= y %> <br />
<% end %>

Upvotes: 2

SHS
SHS

Reputation: 7744

No trick. There isn't any out-of-the-box way to do this. You'll have to format the hash.

An example:

puts( your_hash.map{ |k,v| "#{k} => #{v}" }.sort )

Upvotes: 4

Related Questions