Skip to content

Instantly share code, notes, and snippets.

@TSMMark
Last active April 30, 2022 21:04
Show Gist options
  • Save TSMMark/fed0b0a32c4a9936064390fb3b729874 to your computer and use it in GitHub Desktop.
Save TSMMark/fed0b0a32c4a9936064390fb3b729874 to your computer and use it in GitHub Desktop.
alias `binding.pry` as `debugger` in .pryrc pry config file

alias binding.pry as debugger in .pryrc pry config file

The Problem

In my experience, debugger is easier for people to remember and to type. And so most devs on our team prefer typing debugger over binding.pry. However, debugger is worse than binding.pry because it doesn't use Pry, which is better than standard byebug / irb repl.

The Solution

Alias binding.pry as debugger. Now we should be able to use debugger and binding.pry interchangeably, and it will always use Pry!

#!/usr/bin/env ruby
# When `debugger` is invoked, the Pry session will start from inside the `debugger` method context.
# That is not ideal because you'd always have to run `up` to go up the call stack to the frame / context you actually wrote `debugger` in.
# This `:when_started` hook automatically invokes the `up` command to bring us up to the proper context. Problem solved!
Pry.config.hooks.add_hook(:when_started, :go_up_from_debugger) do |_out, _binding, pry_instance|
# Go `up` from the debugger method override.
pry_instance.process_command("up") if binding.send(:caller).any? { |line| line.include?("in `debugger'") }
end
# ... the rest of your .pryrc
# See .pryrc `add_hook(:when_started, :go_up_from_debugger)` for how we go `up` out of the `debugger` method context, and into the parent caller method, as desired.
def debugger
# rubocop:disable Lint/Debugger
binding.pry
# rubocop:enable Lint/Debugger
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment