Powered By BlogNow - Get Your Free Blog

I have moved!

My new blog is at lindsaar.net

All the old content will stay here though. But check out the new blog

¿ 30/6/2007 - Debugging Ruby on Rails

Sometimes your app can go off the rails...

How to get it back on the rails?

Well, say you have a controller that is calling a model class method and passing it a variable it is getting from params[] in order to do something useful.

You run the action, and get back some weird result.  What to do?

Easy!  Debugger to the rescue!

Ruby-Debug is a gem that makes finding bugs in your code INSANELY easy... like... really easy... really... I'm not joking... easy.... :)

If you haven't watched the ruby-debugger screencast, from Brian Donovan, you should do so now, this is how you find your way around, so you should go watch that first and come back here after.

So now you are back and you have the gem "ruby-debug" installed right?  No?  Do that now :)

So, now you have the ruby-gem, you need to include it in your code.  A good idea for this is to put it in your application.rb file under /app/controller/application.rb so it is included globally.

I use the following code to do it:

require 'ruby-debug' if ENV['RAILS_ENV'] == 'development' 

require 'ruby-debug' if ENV['RAILS_ENV'] == 'test'

What this does is require the ruby-debug gem globally IF your environment is development or test.  This is to avoid accidently loading the debugger in production mode... something you don't want to do...

So now what you do is you go to the code that is causing you a problem, perhaps it is a class method, well, dive in there and insert the line "debugger"... sort of like this (by the way, this code is taken from my Word HTML document parser that wraps around _Why's Hpricot HTML parser to extract all paragraphs, fonts, styles, footters, indexes, glossaries and then clean up the code... LOTS)
  def remove_attributeless_span_tags
debugger
 @text.search("span").each do |span|
 if span.attributes.empty?
 html = span.inner_html.to_s
 span.swap(html)
 end
  end
 end
So, now fire up your rails web server with ./script/server and the go to the page that accesses this method and click whatever you have to click to make it execute.  If you have done it right, nothing will happen :)

Well.. not nothing, but your browser will just look like it is hanging there.

But if you go back to the terminal window that you started ./script/server in, you will see a ruby debug prompt right there in the window!

And it will be at the breakpoint where you types "debugger".

Now you can type "set autolist" and the step or next through the code watching your variables as you go!

Some tips:

  • You can type "p variable" to get the variable value
  • You can also use class or instance methods, for example, if you are in debug and @foo has been initialied to Foo.find(:all).first then you could type "p @foo.to_yaml" to see the data
  • You can use array access methods... say you had @foos = Foo.find(:all), well, then you could type "p @foos[0].to_yaml" to read the details of the #1 @foo....
  • You can type "break 14", actually, break any line... it doesn't have to be 14... :)  And then type "c" for continue and the debugger will execute everything up to that break point and go back to the prompt.
  • Many many other things... Maybe I'll blog later on them.
Anyway, give it a shot, if you have any questions, feel free to ask!

blogLater

Mikel
Post A Comment! :: Send to a Friend!

¿ 23/7/2007 - Screencast now available!

Posted by mikel
My good friend Ryan Bates just made an awesomely cool screen cast about this functionality.

You can find it at:

http://railscasts.com/episodes/54

blogLater

Mikel
Permanent Link

About Me

AKA Raasdnil, this site is about web coding, hosting and all other matters that relate to this... especially Ruby on Rails!

Links