readable output in rails script/breakpointer

Posted on Sat May 06 16:24:00 UTC 2006

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’

Posted in Rails  |  Tags , ,  |  2 comments

Comments

  1. Jules Jules said // May 06, 2006 at 06:21 PM

    Can't you just do: puts caller Puts automatically prints array elements on separate lines.

  2. Pascal Pascal said // May 06, 2006 at 07:12 PM

    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.

(leave url/email »)

Comment Markup Help