blog post: remove binding.pry

This commit is contained in:
Jörg Thalheim 2014-11-14 15:37:12 +01:00
parent 520e832b33
commit d976a8f504

View File

@ -0,0 +1,58 @@
---
layout: post
title: "Remove current binding.pry from pry"
date: 2014-11-14 15:27:42 +0100
comments: true
categories:
- pry
- ruby
---
If you are a ruby user and find it annoying to remove [binding.pry](http://pryrepl.org/) by hand, you may
find the following snippet useful. (Put it in your ~/.pryrc to use it)
```ruby .pryrc
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:
```ruby debug.rb
# ...
if foo == :bar
binding.pry
a_shiny_method
end
# ...
```
```ruby in pry
pry> remove-pry
```
After:
```ruby debug.rb
# ...
if foo == :bar
a_shiny_method
end
# ...
```