academy

Inspecting data in Ruby

Jeff Kreeftmeijer

Jeff Kreeftmeijer on

Inspecting data in Ruby

Most exceptions are caused by unexpected data being passed to methods that work fine otherwise, so it's often useful to trace a piece of data through your application to find the cause of an error. In this article, we'll go over inspecting data in Ruby and Rails applications.

puts, #inspect and p

Ruby's puts method is used to print a message to the console.

1unknown = "This is a string"
2puts unknown

When running the script above, the value is printed out to allow you to see what's inside.

1$ ruby puts.rb
2This is a string

However, if the unknown variable holds an empty string, a nil value or an empty hash, puts will print an empty line. With that, you won't be able to see its exact value, and you won't be able to differentiate between empty hashes and strings, for example.

1$ ruby puts_empty_string.rb
2

Luckily, there's an instance method named inspect, which is implemented on every object you'll encounter. It will return a string representation of the object.

1unknown = ""
2puts unknown.inspect

For an empty string, it'll return a pair of quotes, and for an array, it'll return a pair of brackets.

1$ ruby inspect_empty_string.rb
2""
3$ ruby inspect_empty_array.rb
4[]

Since the combination of puts and inspect are used so often, there's a shortcut that does both in one go, called p. It does the exact same thing, but it's quicker to type.

1unknown = ""
2p unknown # equivalent to `puts unknown.inspect`

Inspecting data in Rails views

Although there's the p shortcut, it's still useful to know about the inspect method when you need a string representation of an object without printing it to the console.

In a Rails view, for example, it's sometimes more useful to print the value to the resulting HTML page instead of having to find it in the log.

1<%= params.inspect %>

When inspecting more complex data structures, it's sometimes difficult to find the data you're looking for, because the inspected value is printed on a single line.

Inspecting data in a Rails view with #inspect

Rails provides the debug helper to get around this. Instead of just inspecting the value, it will convert the object to a human readable YAML representation and put it in <pre> tags to preserve formatting.

1<%= debug params %>

Inspecting data in a Rails view with the debug helper

The debug helper uses the to_yaml method under the hood to get the YAML representation, before wrapping it in a <pre> block. To convert an object to a YAML representation without wrapping it in a <pre> tag, you can use #to_yaml on any object directly.

1irb(main):004:0* puts Product.first.to_yaml
2--- !ruby/object:Product
3concise_attributes:
4- !ruby/object:ActiveRecord::Attribute::FromDatabase
5  name: id
6  value_before_type_cast: 4
7- !ruby/object:ActiveRecord::Attribute::FromDatabase
8  name: title
9  value_before_type_cast: Title
10  ....

As you've come to expect, there's a shortcut for printing a YAML representation of an object too. The example above can be called in one go by using y Product.first.

1irb(main):004:0* y Product.first # equivalent to `puts Product.first.to_yaml`
2--- !ruby/object:Product
3...

"puts"-debugging

There are more ways to inspect data in your applications, like the Pry library and Ruby's own debugger. While it's certainly a good idea to check out other methods of inspecting data, writing a log to the console or showing a piece of data in a view is often the quickest way to find out what's happening.

We’d love to know how you liked this article, if you have any questions about it, and what you’d like to read about next, so be sure to let us know at @AppSignal.

Share this article

RSS

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps