A common task when debugging a Rails app is to view the contents of a data container, like @user, for instance. What we might want to use in rails console is YAML for an easy to read format. Here’s an example.
Nice. The problem is that “y” isn’t available to us in debug mode. Even if you go into IRB in debug mode, “y” ain’t happenin’. Before we explore our options, let’s set the scene.
I’m using byebug as my debugger. To each their own, but since I’m using Ruby 2.0 I felt byebug was most compatible. We’ll enter “byebug” where we want to kick into debug mode…
then launch the rails server with
rails s –debug
where we see byebug is enabled.
We go into our browser to http://localhost:3000 (not the site’s url if using Passenger or Mongrel…) to trigger debug mode. We want to view the contents of @user so we’ll go “next” to load it.
Now we try our familiar
and instead of friendly user output we get ugliness.
Don’t despair. YAML is still available to us, but we have to use a @user.to_yaml call.
The problem is that YAML is supposed to provide a user-friendly display, and this ain’t it. The trick is to output the YAML with a puts statement.
That’s better. There may be configuration options or specific gems that will support the “y” in debug mode, but regardless, YAML is still your friend in debug mode. You can use other formatters like Hirb in debug mode as shown here, so it’s whatever your viewing cup of tea may be.