Wednesday, June 13, 2007

Rails scripts/breakpointer questions

NOTES:
  1. Things of interest : local_variables, instance_variables, caller, methods
  2. Keep a close eye on the prompt. A * means breakpointer (irb really) expects you to enter more ruby code since it senses that the code entered so far is not fully valid yet.
  3. breakpointer (irb really) will by default print the return value to the irb console. You can explicitly return false to disable printing of large datasets.

    breakpointer> @copy = @huge_dataset; false

    For regular irb (and script/console) setting conf.return_format allows you to limit what is printed, but conf object doesn't seem to exist in breakpointer's irb session.

How do you print out variable values?
puts doesn't work :) Update: It actually prints the output along with the script/server output.

You can use @logger.info etc which will print the info to development.log

Local variables
I put a breakpoint in a helper method and tried to view some local variables defined in the helper method, but it didn't work.

breakpoint> local_variables
=> ["id", "block", "_"]

(note that I cleaned the irb prompt and replaced it with breakpoint for blogging)
The reason is breakpoint drops me in the breakpoint.rb's breakpoint method.

breakpoint> caller
=> ["/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint.rb:512:in `breakpoint'", "/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/breakpoint.rb:512:in `breakpoint'", "script/../...


Now to figure out how we pop the stack back up so we end up in the method. Anyone know?

Printing Huge datasets
Suppose I am debugging some calculations I am doing on a hude dataset in breakpointer:

breakpoint> @huge_dataset

This will print the entire dataset. Ctrl-C will stop the display.


breakpoint> @huge_dataset;

This doesn't print the dataset.

So how is one to print the dataset. One solution is to use to_yaml.

breakpoint> fh = File.open("/tmp/foo", "w");
breakpoint> fh.puts(@huge_dataset.to_yaml)
=> nil
breakpoint>

I would really like to print the value in the breakpointer's irb console though.
Is there a special console object that I can target?

Update: Another way to debug your rails app - ruby-debug

2 comments:

Tom Rossi said...

I saw your post, but didn't understand how you got around it. No matter where I place my breakpoint, I seem to be getting these results:

breakpoint> local_variables
=> ["id", "block", "_"]

- Chetan Patil said...

Hi Tom,
I couldn't get around it. But the more I think about it, breakpoint doesn't seem to be a typical debugger where you can step through the program. Instead, breakpoint allows you to essentially inspect the state of the system at that point. You can inspect instance variables and hot patch your code, but you can't step through it.

Looks like breakpoint is now deprecated and Ruby debug is recommended over it.
http://wiki.rubyonrails.org/rails/pages/HowtoDebugWithBreakpoint

Hope this helps.