blog/source/_posts/2014-11-14-remove-current-b...

1.1 KiB

layout title date comments categories
post Remove current binding.pry from pry 2014-11-14 15:27:42 +0100 true
pry
ruby

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)

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:

# ...
if foo == :bar
  binding.pry
  a_shiny_method
end
# ...
pry> remove-pry

After:

# ...
if foo == :bar
  a_shiny_method
end
# ...