1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Make watch expressions fully global

At the moment Pry::Hooks are not local to each instance, so the hook
was only being added once. This caused problems when you opened two
binding.pry's in one program, as the second one's watch expressions
appeared to be ignored.
This commit is contained in:
Conrad Irwin 2014-01-23 23:36:46 -08:00
parent b4ad243570
commit 0320ae8471
2 changed files with 29 additions and 5 deletions

View file

@ -38,7 +38,7 @@ class Pry
private
def expressions
state.expressions ||= []
Pry.config.watch_expressions ||= []
end
def delete(index)
@ -66,7 +66,7 @@ class Pry
end
end
def eval_and_print_changed
def eval_and_print_changed(output)
expressions.each do |expr|
expr.eval!
if expr.changed?
@ -82,9 +82,9 @@ class Pry
def add_hook
hook = [:after_eval, :watch_expression]
unless Pry.config.hooks.hook_exists?(*hook)
_pry_.hooks.add_hook(*hook) do
eval_and_print_changed
unless Pry.hooks.hook_exists?(*hook)
Pry.hooks.add_hook(*hook) do |_, _pry_|
eval_and_print_changed _pry_.output
end
end
end

View file

@ -64,6 +64,30 @@ describe "watch expression" do
end
end
it "continues to work if you start a second pry instance" do
ReplTester.start do
input 'a = 1'
output '=> 1'
input 'watch a'
output "Watching a\nwatch: a => 1"
input "a = 2"
output "watch: a => 2\n=> 2"
end
ReplTester.start do
input 'b = 1'
output '=> 1'
input 'watch b'
output "Watching b\nwatch: b => 1"
input "b = 2"
output "watch: b => 2\n=> 2"
end
end
describe "deleting expressions" do
before do
eval 'watch :keeper'