in Rails

readable output in rails script/breakpointer

After hours spent in breakpointer struggling to make sense of the output, I figured there had to be a better way to look at a stack trace than what the default output provides:

irb(ArticlesController):007:0> caller
=> [“./vendor/rails/railties/lib/breakpoint.rb:512:in `breakpoint’”, “./vendor/rails/railties/lib/breakpoint.rb:512:in `breakpoint’”, “./vendor/rails/actionpack/lib/action_controller/caching.rb:510:in `cache_sweeper’”, “./app/controllers/articles_controller.rb:7”, …]

Well, after some research, I found at least 4! And they work for any data structure, not just for stack traces, but I’m a pretty happy camper just with cleaner stack traces!

Solution #1
Building on Using the standard output in breakpointer, you can use each to get a more decent output.

caller.each { |x|  client.puts x }

which returns:

./vendor/rails/railties/lib/breakpoint.rb:512:in `breakpoint’
./vendor/rails/railties/lib/breakpoint.rb:512:in `breakpoint’
./vendor/rails/actionpack/lib/action_controller/caching.rb:510:in `cache_sweeper’
…

Solution #2
Use PrettyPrint

client.require ‘pp’
client.pp caller

Solution #3
Use YAML

client.require ‘yaml’
client.y caller

Solution #4
Use to_yaml

client.puts caller.to_yaml

These methods can also be used to display any complex data.

Try it on the request object for example.

To avoid having to type the require each time, add to .irbrc:

require ‘yaml’
require ‘pp’
  1. Can’t you just do:

    puts caller

    Puts automatically prints array elements on separate lines.

  2. Good point. Should have thought of that one.
    In breakpointer, you would do:

    client.puts caller

    to get the output in breapointer itself

    Thank you, Jules.

Comments are closed.