diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index ee28a1b1..05d8e36e 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -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 diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index 4801bdb3..e87d8be4 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -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'