views:

106

answers:

2

I'm familiarizing myself with some JRuby code, and I'd like to be able to place a breakpoint in the code and run (as usual) from the command-line, having it break into the debugger when it gets to that point. Is there something I can put in my code to force JRuby to break into the debugger?

I've tried running jruby -r debug foo.rb (instead of the usual jruby foo.rb), and then setting a breakpoint with b bar.py:98, and then continuing. But the debugger stops every time there's an exception, and there seem to be a lot of them before it gets to the line of code I'm interested in. I'd like to be able to put the "break-into-debugger" line(s) in my code and run jruby foo.rb and have the first place the debugger stops be at that line.

(i.e. I'm looking for the Ruby/JRuby equivalent of import pdb;pdb.set_trace() in Python.)

A: 
require 'rubygems'
require 'ruby-debug'
debugger
bnaul
I got "LoadError: no such file to load -- ruby-debug" — perhaps that doesn't work in JRuby?
Daryl Spitzer
Apparently JRuby doesn't support any gems with non-native Ruby components; Google turned up the following, but I have no idea if it's any good: http://debug-commons.rubyforge.org/
bnaul
+3  A: 

You could try Netbeans Ruby IDE, it has JRuby interpreter and debugging tools embedded and you can debug visually in the IDE directly.
If using an IDE is not an option for you, just install de debug gem into your JRuby distro and use it via debugger command:

  1. Manually download the ruby-debug-base-0.10.3.1-java.gem from debug-commons to a local directory.
  2. Install the Gem into your JRuby Gem repository:
    jruby -S gem install -l ruby-debug-base-0.10.3.1-java.gem
  3. Install ruby-debug gem:
    jruby -S gem install --ignore-dependencies ruby-debug

The debugger command should work now.

# test.rb
require 'rubygems'
require 'ruby-debug'
debugger
# run like this:
jruby --debug -S rdebug test.rb

More information on Netbeans wiki, rdebug wiki and JRuby wiki

clyfe
Netbeans is working nicely for me, with only a tiny bit of configuration required. Thanks!
Daryl Spitzer
In JRuby 1.5+, all the required gems will be installed by default, so there will be no need to install anything (esp. with non-default command line options). So, debug will work in JRuby 1.5+ out of the box.
VVSiz