psa: you can now debug with pry on jruby

Datetime:2017-04-19 05:38:00         Topic: JRuby          Share        Original >>
Here to See The Original Article!!!

I’ve been wanting to write this blog post for about a month now, but amongst the fixes for JRuby version 9.1.3.0 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!"
end

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

  some_other_method
end

some_method

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.
    12:
    13:   some_other_method
    14: end

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

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

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

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
...stranger!

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!








New