Class: Lookout::Diff::Slice
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/lookout-3.0/diff/slice.rb
Overview
Slice (subsequence) of a sequence being “diffed”.
Direct Known Subclasses
Instance Method Summary collapse
-
#+(other) ⇒ Object
A new slice encompassing the receiver and OTHER.
- #<=>(other) ⇒ Object
-
#[](index) ⇒ Object
The INDEXth element.
-
#at(range) ⇒ Slice
A new slice encompassing RANGE.
-
#begin ⇒ Integer
The index of the first element.
-
#begin_after(other) ⇒ Slice
A new slice beginning at OTHER#end + 1.
-
#begin_at(index) ⇒ Slice
A new slice beginning at INDEX.
- #begin_before?(other) ⇒ Boolean
- #each ⇒ Object
- #each_with_index ⇒ Object
-
#empty? ⇒ Boolean
True if #size < 1.
-
#end ⇒ Integer
The index of the last element.
- #end_after?(other) ⇒ Boolean
-
#end_at(index) ⇒ Slice
A new slice ending at INDEX.
-
#end_before(other) ⇒ Slice
A new slice ending at OTHER#begin - 1.
-
#initialize(items, range = 0...items.size) ⇒ Slice
constructor
A new instance of Slice.
-
#size ⇒ Object
The number of encompassed elements.
-
#to_items ⇒ Enumerable<Object>
The encompassed elements.
- #touch?(other) ⇒ Boolean
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
Logically, the receiver should #touch? OTHER, but this isn’t enforced.
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
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.
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.
36 |
# File 'lib/lookout-3.0/diff/slice.rb', line 36 def at(range) self.class.new(items, range) end |
#begin ⇒ Integer
Returns 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.
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.
44 |
# File 'lib/lookout-3.0/diff/slice.rb', line 44 def begin_at(index) at(index..self.end) end |
#begin_before?(other) ⇒ Boolean
24 |
# File 'lib/lookout-3.0/diff/slice.rb', line 24 def begin_before?(other) self.begin < other.begin end |
# {|element| ... } ⇒ Object # ⇒ Enumerator<Object>
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>
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.
15 |
# File 'lib/lookout-3.0/diff/slice.rb', line 15 def empty?; size < 1 end |
#end ⇒ Integer
Returns 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
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.
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.
33 |
# File 'lib/lookout-3.0/diff/slice.rb', line 33 def end_before(other) end_at(other.begin - 1) end |
#size ⇒ Object
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_items ⇒ Enumerable<Object>
Returns The encompassed elements.
76 |
# File 'lib/lookout-3.0/diff/slice.rb', line 76 def to_items; items[range] end |