Remove Current binding.pry From Pry

If you are a ruby user and find it annoying to remove binding.pry by hand, you may find the following snippet useful. (Put it in your ~/.pryrc to use it)

.pryrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Pry.config.commands.command "remove-pry", "Remove current pry" do
  require 'pry/commands/edit/file_and_line_locator'
  file_name, remove_line =
Pry::Command::Edit::FileAndLineLocator.from_binding(_pry_.current_binding)
  temp_file = Tempfile.new('foo')
  i = 0
  File.foreach(file_name) do |line|
    i += 1
    if i == remove_line
      line.gsub!(/binding.pry(\s)?/, "")
      temp_file.write line unless line =~ /\A[[:space:]]*\z/
    else
      temp_file.write line
    end
  end
  temp_file.close
  FileUtils.cp(temp_file.path, file_name)
end

Usage

Before:

debug.rb
1
2
3
4
5
6
# ...
if foo == :bar
  binding.pry
  a_shiny_method
end
# ...
in pry
1
pry> remove-pry

After:

debug.rb
1
2
3
4
5
# ...
if foo == :bar
  a_shiny_method
end
# ...

Comments