1

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?

3
  • 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. Commented Oct 12, 2022 at 9: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... Commented Oct 12, 2022 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! Commented Oct 13, 2022 at 9:47

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.