~/git/blog

My brain-dump of random code/configuration.

14 Nov 2014

Remove Current Binding Dot 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)

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
# ...
comments powered by Disqus