Class: Lookout::Diff::Slice

Inherits:
Object show all
Includes:
Comparable, Enumerable
Defined in:
lib/lookout-3.0/diff/slice.rb

Overview

Slice (subsequence) of a sequence being “diffed”.

Direct Known Subclasses

Algorithms::Difflib::Position::New

Instance Method Summary collapse

Constructor Details

#initialize(items, range = 0...items.size) ⇒ Slice

Returns a new instance of Slice.



9
10
11
# File 'lib/lookout-3.0/diff/slice.rb', line 9

def initialize(items, range = 0...items.size)
  super items, range.exclude_end? ? range.begin..range.end - 1 : range
end

Instance Method Details

#+(other) ⇒ Object

Note:

Logically, the receiver should #touch? OTHER, but this isn’t enforced.

Returns A new slice encompassing the receiver and OTHER.

Returns:

  • A new slice encompassing the receiver and OTHER



41
# File 'lib/lookout-3.0/diff/slice.rb', line 41

def +(other) at(self.begin..other.end) end

#<=>(other) ⇒ Object

Returns The comparison of the receiver’s #begin and #end against those of OTHER.

Returns:

  • The comparison of the receiver’s #begin and #end against those of OTHER



89
90
91
92
93
94
# File 'lib/lookout-3.0/diff/slice.rb', line 89

def <=>(other)
  return nil unless self.class == other.class and items == other.items
  (self.begin <=> other.begin).nonzero? or
    (self.end <=> other.end).nonzero? or
    0
end

#[](index) ⇒ Object

Returns The INDEXth element.

Returns:

  • The INDEXth element



79
# File 'lib/lookout-3.0/diff/slice.rb', line 79

def [](index) items[index] end

#at(range) ⇒ Slice

Returns A new slice encompassing RANGE.

Returns:

  • (Slice)

    A new slice encompassing RANGE



36
# File 'lib/lookout-3.0/diff/slice.rb', line 36

def at(range) self.class.new(items, range) end

#beginInteger

Returns The index of the first element.

Returns:

  • (Integer)

    The index of the first element



82
# File 'lib/lookout-3.0/diff/slice.rb', line 82

def begin; range.begin end

#begin_after(other) ⇒ Slice

Returns A new slice beginning at OTHER#end + 1.

Returns:

  • (Slice)

    A new slice beginning at OTHER#end + 1



30
# File 'lib/lookout-3.0/diff/slice.rb', line 30

def begin_after(other) begin_at(other.end + 1) end

#begin_at(index) ⇒ Slice

Returns A new slice beginning at INDEX.

Returns:

  • (Slice)

    A new slice beginning at INDEX



44
# File 'lib/lookout-3.0/diff/slice.rb', line 44

def begin_at(index) at(index..self.end) end

#begin_before?(other) ⇒ Boolean

Returns True if #begin < OTHER#begin.

Returns:



24
# File 'lib/lookout-3.0/diff/slice.rb', line 24

def begin_before?(other) self.begin < other.begin end

# {|element| ... } ⇒ Object #Enumerator<Object>

Overloads:

  • # {|element| ... } ⇒ Object

    Enumerates the encompassed elements.

    Yield Parameters:

    • element
  • #Enumerator<Object>

    Returns An Enumerator over the encompassed elements.

    Returns:

    • (Enumerator<Object>)

      An Enumerator over the encompassed elements



55
56
57
58
59
# File 'lib/lookout-3.0/diff/slice.rb', line 55

def each
  return enum_for(__method__) unless block_given?
  range.each do |index| yield items[index] end
  self
end

# {|element, index| ... } ⇒ Object #Enumerator<Object, Integer>

Overloads:

  • # {|element, index| ... } ⇒ Object

    Enumerates the encompassed elements and their indexes.

    Yield Parameters:

    • element
    • index (Integer)
  • #Enumerator<Object, Integer>

    Returns An Enumerator over the encompassed elements and their indexes.

    Returns:

    • (Enumerator<Object, Integer>)

      An Enumerator over the encompassed elements and their indexes



69
70
71
72
73
# File 'lib/lookout-3.0/diff/slice.rb', line 69

def each_with_index
  return enum_for(__method__) unless block_given?
  range.each do |index| yield items[index], index end
  self
end

#empty?Boolean

Returns True if #size < 1.

Returns:

  • (Boolean)

    True if #size < 1



15
# File 'lib/lookout-3.0/diff/slice.rb', line 15

def empty?; size < 1 end

#endInteger

Returns The index of the last element.

Returns:

  • (Integer)

    The index of the last element



85
# File 'lib/lookout-3.0/diff/slice.rb', line 85

def end; range.end end

#end_after?(other) ⇒ Boolean

Returns True if #end > OTHER#end.

Returns:

  • (Boolean)

    True if #end > OTHER#end



27
# File 'lib/lookout-3.0/diff/slice.rb', line 27

def end_after?(other) self.end > other.end end

#end_at(index) ⇒ Slice

Returns A new slice ending at INDEX.

Returns:

  • (Slice)

    A new slice ending at INDEX



47
# File 'lib/lookout-3.0/diff/slice.rb', line 47

def end_at(index) at(self.begin..index) end

#end_before(other) ⇒ Slice

Returns A new slice ending at OTHER#begin - 1.

Returns:



33
# File 'lib/lookout-3.0/diff/slice.rb', line 33

def end_before(other) end_at(other.begin - 1) end

#sizeObject

Returns The number of encompassed elements.

Returns:

  • The number of encompassed elements



18
# File 'lib/lookout-3.0/diff/slice.rb', line 18

def size; self.end - self.begin + 1 end

#to_itemsEnumerable<Object>

Returns The encompassed elements.

Returns:

  • (Enumerable<Object>)

    The encompassed elements



76
# File 'lib/lookout-3.0/diff/slice.rb', line 76

def to_items; items[range] end

#touch?(other) ⇒ Boolean

Returns True if #end + 1 = OTHER#begin.

Returns:



21
# File 'lib/lookout-3.0/diff/slice.rb', line 21

def touch?(other) self.end + 1 == other.begin end