From 0320ae8471cbd02c04fd7c1f329cd2844bd6d545 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 23 Jan 2014 23:36:46 -0800 Subject: [PATCH] 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. --- lib/pry/commands/watch_expression.rb | 10 +++++----- spec/commands/watch_expression_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) 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'