diff --git a/lib/pry/commands/watch_expression/expression.rb b/lib/pry/commands/watch_expression/expression.rb index 1a3c1de9..e2a8dfba 100644 --- a/lib/pry/commands/watch_expression/expression.rb +++ b/lib/pry/commands/watch_expression/expression.rb @@ -11,6 +11,10 @@ class Pry def eval! @previous_value = value @value = target_eval(target, source) + begin + @value = @value.dup + rescue Pry::RescuableException + end end def to_s diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index ea62cedd..2642fbd1 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -51,6 +51,19 @@ describe "watch expression" do end end + it "prints when an expression is mutated" do + ReplTester.start do + input 'a = "one"' + output '=> "one"' + + input 'watch a' + output %(Watching a\nwatch: a => "one") + + input "a.sub! 'o', 'p'" + output %(watch: a => "pne"\n=> "pne") + end + end + it "doesn't print when an expresison remains the same" do ReplTester.start do input 'a = 1'