views:

462

answers:

5

I am trying to find a better way to integrate IRB with my normal ruby devleopment. Currently I rarely use IRB with my code. I only use it to verify syntax or to try something small.

I know I can load my own code into ruby as a

require 'mycode'

but this usually doesn't mesh with my programming style. Sometimes the variables I want to examine are out of scope or inside of a loop. Is there an easy way to fire up my script and freeze at a certain point inside of IRB? I guess I'm looking for an easier way to debug my ruby code without breaking my F5(compile) key.

Maybe a more experienced ruby developer can share with me a more streamlined method of development.

A: 

I just use rdebug to debug any of my ruby or RoR code.

TonyLa
+2  A: 

I don't tend to use irb directly that frequently, as I tend to be inside rails and so use script/console a bunch, but I do like using the ruby debugger (Ruby Debug gem). It lets you set a breakpoint basically and then step through your code line by line.

Here's a screencast about it that I haven't actually watched, but a quick search pulled it up, and it could be useful:

http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/

Cameron Booth
A: 

If you're willing to use an IDE for debugging, I know Eclipse (via the Ruby Development Tools) has a relatively straightforward interface. If you're doing rails then there's a specific build of eclipse called RadRails which may also help (though I haven't used it for debugging)

Atiaxi
+7  A: 

Install the ruby-debug gem. Of course, require it inside your app (only in development/test mode). Now you can write 'debugger' where you want to stop execution.

Once your app stop at your breakpoint, you can type 'help' to know about all commands. One of them is 'irb'. It starts an IRB session in which you have access to all methods in your current context.

I personally mostly use p (print), eval, v i (instance vars) and v l (local vars). Of course, n for next and c for continue.

The command to step out of a given block/method never worked for me though. I never investigated why :-)

webmat
I have a textmate/e snippet which inserts "require 'ruby-debug'; debugger". I use it about 10 times a day. It's awesome :-)
Orion Edwards
Ooh, I'll have to check it out! Thanks :-)
webmat
+1  A: 

For Ruby development in Eclipse: there's a much improved version of RDT (ruby development tools) available now. To install it directly in Eclipse, click Help > Software Updates > Find and Install > Search for new features radio button > next > new remote Site > Name = Ruby and URL = http://update.aptana.com/update/rdt/3.2/

Another Ruby plugin is the shiny new Eclipse DLTK (dynamic languages toolkit). DLTK stable release 1.0.M5 just came out a few days ago. Here are some useful installation tips.