I’ve been wanting to write this blog post for about a month now, but amongst the fixes for JRuby version are a pair of fixes to backtrace line generation during debugging.

Why is this relevant? Because it means that you can now debug interactively from a pry console on JRuby too!

To do so, you can use either my fork of the pry-debugger gem, pry-debugger-jruby or the slightly simpler pry-nav gem.

You can include either of them in your Gemfile — I recommend pry-debugger-jruby as it has support for breakpoints:

# Gemfile
source 'https://rubygems.org'

gem 'pry-debugger-jruby'

Here’s a quick example which I’ll use to demonstrate its usage:

require 'pry'

def some_other_method
  puts "Hello,"
  puts "...stranger!"

def some_method
  binding.pry          # Execution will stop here.
  puts 'Hello, World!' # Run 'step' or 'next' in the
                       # console to move here.



And here’s an example execution:

$ JRUBY_OPTS=--debug bundle exec ruby foo.rb

From: foo.rb @ line 9 Object#some_method:

     8: def some_method
 =>  9:   binding.pry          # Execution will stop here.
    10:   puts 'Hello, World!' # Run 'step' or 'next' in the
    11:                        # console to move here.
    13:   some_other_method
    14: end

[1] pry(main)> break foo.rb:5
Breakpoint 1: foo.rb @ line 5 (Enabled) :

    3: def some_other_method
    4:   puts "Hello,"
 => 5:   puts "...stranger!"
    6: end
    8: def some_method

[2] pry(main)> next
Hello, World!

Breakpoint 1. First hit.

From: foo.rb @ line 5 Object#some_other_method:

    3: def some_other_method
    4:   puts "Hello,"
 => 5:   puts "...stranger!"
    6: end

[2] pry(main)> finish

Note that you have to either pass the --debug flag onto the jruby executable, or add it to the JRUBY_OPTS environment variable to enable debugging, otherwise you’ll just get an error when you try to use the debugging facilities.

Also note that I recommend you use --debug only when needed, as it slows down application (and spec) execution by quite a lot!

Hope this was helpful, and happy debugging!