Class: Range

Inherits:
Object show all
Defined in:
lib/flex_array/range.rb

Overview

Extensions to Range needed to support flex array.

Instance Method Summary collapse

Instance Method Details

#to_index_range(spec) ⇒ Object

Convert this range to an range index against the spec.
Parameters

  • spec - The spec component used to validate this index.


Returns

  • A range.


Exceptions

  • IndexError if the range is not valid.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flex_array/range.rb', line 27

def to_index_range(spec)
  self_min, self_max, spec_max = self.begin, self.end, spec.max
  self_max -= 1 if self_max > 0 && self.exclude_end?

  self_min = spec_max + self_min + 1 if self_min < 0
  self_max = spec_max + self_max + 1 if self_max < 0

  if spec === self_min && spec === self_max && self_min < self_max
    self_min..self_max
  else
    fail IndexError, "Subscript out of range: #{self.inspect}"
  end
end

#to_spec_component(stride) ⇒ Object

Convert this integer to a limits component.
Parameters

  • stride - the number of cells separating data with adjacent indexes.


Returns

  • A SpecComponent object with the same range as self.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/flex_array/range.rb', line 8

def to_spec_component(stride)
  min = self.min

  if self == (0...0)
    SpecComponent.new(0...0, stride)
  elsif !self.none? && min.is_a?(Integer) && (min >= 0) && self.max.is_a?(Integer)
    SpecComponent.new(self, stride)
  else
    fail ArgumentError, "Invalid flex array dimension: #{self.inspect}"
  end
end