Class: ANSI::Diff

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

Overview

Diff produces colorized differences of two string or objects.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object1, object2, options = {}) ⇒ Diff

Setup new Diff object. If the objects given are not Strings and do not have #to_str defined to coerce them to such, then their #inspect methods are used to convert them to strings for comparison.

Parameters:

  • object1 (Object)

    First object to compare.

  • object2 (Object)

    Second object to compare.

  • options (Hash) (defaults to: {})

    Options for contoller the way difference is shown. (Not yet used.)



33
34
35
36
37
38
# File 'lib/ansi/diff.rb', line 33

def initialize(object1, object2, options={})
  @object1 = convert(object1)
  @object2 = convert(object2)

  @diff1, @diff2 = diff_string(@object1, @object2)
end

Class Method Details

.diff(object1, object2, options = {}) ⇒ Object

Highlights the differnce between two strings.

This class method is equivalent to calling:

ANSI::Diff.new(object1, object2).to_a


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

def self.diff(object1, object2, options={})
  new(object1, object2, options={}).to_a
end

Instance Method Details

#diff1Object

Returns the first object's difference string.



41
42
43
# File 'lib/ansi/diff.rb', line 41

def diff1
  @diff1
end

#diff2Object

Returns the second object's difference string.



46
47
48
# File 'lib/ansi/diff.rb', line 46

def diff2
  @diff2
end

#join(separator = $/) ⇒ String

Returns both first and second difference strings separated by a the given separator. The default is $/, the record separator.

Parameters:

  • separator (String) (defaults to: $/)

    The string to use as the separtor between the difference strings.

Returns:

  • (String)

    Joined difference strings.



67
68
69
# File 'lib/ansi/diff.rb', line 67

def join(separator=$/)
  "#{@diff1}#{separator}#{@diff2}"
end

#to_aArray

Returns the first and second difference strings in an array.

Returns:

  • (Array)

    Both difference strings.



74
75
76
# File 'lib/ansi/diff.rb', line 74

def to_a
  [diff1, diff2]
end

#to_sString

TODO:

Should we use $/ record separator instead?

Returns both first and second difference strings separated by a new line character.

Returns:

  • (String)

    Joined difference strings.



56
57
58
# File 'lib/ansi/diff.rb', line 56

def to_s
  "#{@diff1}\n#{@diff2}"
end