Class: Diff::LCS::Change

Inherits:
Object
  • Object
show all
Includes:
Comparable, ChangeTypeTests
Defined in:
lib/diff/lcs/change.rb

Overview

Represents a simplistic (non-contextual) change. Represents the removal or addition of an element from either the old or the new sequenced enumerable.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ChangeTypeTests

#adding?, #changed?, #deleting?, #finished_a?, #finished_b?, #unchanged?

Constructor Details

- (Change) initialize(action, position, element)

A new instance of Change



62
63
64
65
66
# File 'lib/diff/lcs/change.rb', line 62

def initialize(action, position, element)
  @action = action
  @position = position
  @element = element
end

Instance Attribute Details

- (Object) action (readonly)

Returns the action this Change represents. Can be '+' (#adding?), '-' (#deleting?), '=' (#unchanged?), # or '!' (#changed?). When created by Diff::LCS#diff or Diff::LCS#sdiff, it may also be '>' (#finished_a?) or '<' (#finished_b?).



44
45
46
# File 'lib/diff/lcs/change.rb', line 44

def action
  @action
end

- (Object) element (readonly)

Returns the value of attribute element



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

def element
  @element
end

- (Object) position (readonly)

Returns the value of attribute position



45
46
47
# File 'lib/diff/lcs/change.rb', line 45

def position
  @position
end

Class Method Details

+ (Object) from_a(arr)



73
74
75
# File 'lib/diff/lcs/change.rb', line 73

def self.from_a(arr)
  Diff::LCS::Change.new(arr[0], arr[1], arr[2])
end

Instance Method Details

- (Object) <=>(other)



55
56
57
58
59
60
# File 'lib/diff/lcs/change.rb', line 55

def <=>(other)
  r = self.action <=> other.action
  r = self.position <=> other.position if r.zero?
  r = self.element <=> other.element if r.zero?
  r
end

- (Object) ==(other)



49
50
51
52
53
# File 'lib/diff/lcs/change.rb', line 49

def ==(other)
  (self.action == other.action) and
  (self.position == other.position) and
  (self.element == other.element)
end

- (Object) to_a

Creates a Change from an array produced by Change#to_a.



69
70
71
# File 'lib/diff/lcs/change.rb', line 69

def to_a
  [@action, @position, @element]
end