Categories
guides

Viewing Ruby Object/Hash Contents

I don’t know what I’m doing, so lots of times, I am working with a Ruby variable that I don’t know the contents of, and when I try to just output it to my webpage, I just get something like #<Variable:0x123abc1don7kn0w> – I’ve found these two methods of viewing the variable contents, depending on if it is an object or a hash.

<%# Objects %>
<pre><%= @variable.inspect %></pre>

<%# Hashes/Arrays %>
<pre><%= JSON.pretty_generate(variable) %></pre>

It isn’t always full on “pretty print”, but it gets the job done.

Sources for objects and hashes

By bo.

I'm @boyEatsSteak in a lot of places. I have been wanting 🍕 for weeks. I like gadgets and technology and keeping notes and tips for my future self since all I can remember is that I forget things.