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.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flex_array/range.rb', line 18

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.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/flex_array/range.rb', line 5

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