0

When I debug rails code, I sometimes set a binding.pry on a certain place that is triggered too often to be debugged, e.g. a loop.

Then I use the disable-pry command to step out of it and let the rest of the program continue. But now when I make another request to the rails server, my breakpoints aren't triggered anymore. And this of course makes sense, as I called disable-pry before.

The only known solution to me is to restart the whole server. But this becomes painful after a while, as reloading the whole environment can be pretty slow.

Is there a way to re-enable pry after having it disabled with disable-pry without restarting the whole ruby process?

23tux
  • 14,104
  • 15
  • 88
  • 187
  • You could use `continue` instead of `disable-pry` to step out of your breakpoint and let the rest of the program run. This way you're not disabling `pry`. – benj-p Oct 12 '22 at 09:51
  • As I wrote above "...that is triggered too often to be debugged, e.g. a loop.". When using continue (or `Ctrl-D`) and the loop has thousand of runs it could take a while... – 23tux Oct 12 '22 at 13:55
  • Sorry, I misunderstood that part. What I usually tend to do in this case is to add a condition to execute `binding.pry` only once (using an index for example). Would love to know if there's a better solution! – benj-p Oct 13 '22 at 09:47

1 Answers1

1

Use ENV['DISABLE_PRY'] = nil or ENV.delete('DISABLE_PRY') to reverse what disable-pry does.

As of version 0.13, this is what the method disable-pry looks like.

[1] pry(main)> show-method disable-pry                                


From: /app/vendor/ruby/3.1.0/gems/pry-0.13.1/lib/pry/commands/disable_pry.rb Number of lines: 23
class DisablePry < Pry::ClassCommand
  match 'disable-pry'
  group 'Navigating Pry'
  description 'Stops all future calls to pry and exits the current session.'

  banner <<-'BANNER'
    Usage: disable-pry

    After this command is run any further calls to pry will immediately return `nil`
    without interrupting the flow of your program. This is particularly useful when
    you've debugged the problem you were having, and now wish the program to run to
    the end.

    As alternatives, consider using `exit!` to force the current Ruby process
    to quit immediately; or using `edit -p` to remove the `binding.pry`
    from the code.
  BANNER

  def process
    ENV['DISABLE_PRY'] = 'true'
    pry_instance.run_command "exit"
  end
end
hdorio
  • 12,902
  • 5
  • 34
  • 34