Class: Integer
Overview
Extensions to Integer needed to support flex array.
Instance Method Summary collapse
-
#to_index_range(spec) ⇒ Object
Convert this integer to an range index against the spec.
-
#to_spec_component(stride) ⇒ Object
Convert this integer to a limits component.
Instance Method Details
#to_index_range(spec) ⇒ Object
Convert this integer 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.
23 24 25 26 27 28 29 30 31 |
# File 'lib/flex_array/integer.rb', line 23 def to_index_range(spec) alter_ego = (self >= 0) ? self : (spec.max + self + 1) if spec === alter_ego alter_ego..alter_ego 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 of 0…self
8 9 10 11 12 13 14 |
# File 'lib/flex_array/integer.rb', line 8 def to_spec_component(stride) if self >= 0 SpecComponent.new(0...self, stride) else fail ArgumentError, "Invalid flex array dimension: #{self.inspect}" end end |