valk
valk

Reputation: 9874

How to shorten binding.pry without using dollar-sign global variable?

binding.pry 

is a cool command and many people use it. But it's too much to type each time. What is the best way to add a shorthand for it, something like

bp

Please don't suggest putting a

$bp = binding.pry

into initializers but anything without the dollar sign?

Upvotes: 2

Views: 555

Answers (3)

Engineer Waqas
Engineer Waqas

Reputation: 43

@valk's solution did not work for me because MRI stops on binding.pry line in pry.rb itself, here is what worked for me using a custom snippet:

To create a custom snippet, open VS Code Command Palette Shift + Command + P (Mac) / Ctrl + Shift + P (Windows/Linux), type User Snippets, select Configure User Snippets, and select ruby.json as the language identifier and paste this code into it. Here is the reference:

"Shorten binding.pry": {
  "prefix": "bp",
  "body": [
    "binding.pry"
  ],
  "description": "Add binding using pry gem"
}

Upvotes: 0

Kentaro Imai
Kentaro Imai

Reputation: 654

I just created a gem for this purpose: pry-alias

Upvotes: 2

valk
valk

Reputation: 9874

So, after a little thought, here's the solution. It will require one more next to hit. But there are the other benefits - you can define it per environment, so you'll never get an error on production if you forget to delete or comment out bp.

Go to initializers, and create pry.rb file with these contents:

def bp
  unless %w(production).include? Rails.env
    binding.pry
  end
end

Have fun!

Upvotes: 1

Related Questions