Use string difference in watch

This commit is contained in:
Conrad Irwin 2014-02-07 18:02:00 -08:00
parent e3c80b20f2
commit ea307f8721
1 changed files with 8 additions and 14 deletions

View File

@ -1,36 +1,30 @@
class Pry class Pry
class Command::WatchExpression class Command::WatchExpression
class Expression class Expression
NODUP = [TrueClass, FalseClass, NilClass, Numeric, Symbol].freeze
attr_reader :target, :source, :value, :previous_value attr_reader :target, :source, :value, :previous_value
def initialize(target, source) def initialize(target, source)
@target = target @target = target
@source = source @source = Code.new(source).strip
end end
def eval! def eval!
@previous_value = value @previous_value = @value
@value = target_eval(target, source) @value = Pry::ColorPrinter.pp(target_eval(target, source), "")
@value = @value.dup unless NODUP.any? { |klass| klass === @value }
end end
def to_s def to_s
"#{print_source} => #{print_value}" "#{source} => #{value}"
end end
# Has the value of the expression changed?
#
# We use the pretty-printed string represenation to detect differences
# as this avoids problems with dup (causes too many differences) and == (causes too few)
def changed? def changed?
(value != previous_value) (value != previous_value)
end end
def print_value
Pry::ColorPrinter.pp(value, "")
end
def print_source
Code.new(source).strip
end
private private
def target_eval(target, source) def target_eval(target, source)