Class: SubDiff::Diff

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/sub_diff/diff.rb

Overview

Stores a single match (and optional replacement) from a String#sub or String#gsub replacement.

It behaves just like a String and represents the newly replaced string if a replacement was made, or the matched string itself if no changes occurred.

It also has additional methods that provide access to the old string, the newly replaced string, and a boolean to determine if a replacement was actually made.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, value_was = nil) ⇒ Diff

Returns a new instance of Diff


19
20
21
22
# File 'lib/sub_diff/diff.rb', line 19

def initialize(value, value_was = nil)
  @value_was = value_was || value
  super(value)
end

Instance Attribute Details

#value_wasObject (readonly)


15
16
17
# File 'lib/sub_diff/diff.rb', line 15

def value_was
  @value_was
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/sub_diff/diff.rb', line 24

def changed?
  value != value_was
end

#empty?Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/sub_diff/diff.rb', line 28

def empty?
  value.empty? && !changed?
end