Class: Array

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

Overview

Extensions to Array needed to support flex array.

Direct Known Subclasses

SpecArray

Instance Method Summary collapse

Instance Method Details

#array_dataObject

Quick access to the array data for internal use.
Returns

  • An array – self



20
21
22
# File 'lib/flex_array/array.rb', line 20

def array_data
  self
end

#array_specsObject

Quick access to the limits for internal use.
Returns

  • An array with one spec component in it.



13
14
15
# File 'lib/flex_array/array.rb', line 13

def array_specs
  SpecArray.new([0...self.length])
end

#limitsObject

Get the specifications of the array index values.
Returns

  • An array with one range in it.



6
7
8
# File 'lib/flex_array/array.rb', line 6

def limits
  [0...self.length]
end

#to_flex_arrayObject

Return this flex array as a flex array!
Returns

  • A flex array that references this array.


Note

  • To avoid a shared reference, use my_array.dup.to_flex_array or

FlexArray.new_from_array(my_array.dup)  instead.


52
53
54
# File 'lib/flex_array/array.rb', line 52

def to_flex_array
  FlexArray.new_from_array(self)
end

#to_index_range(spec) ⇒ Object

Convert this array 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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flex_array/array.rb', line 31

def to_index_range(spec)
  spec_max = spec.max

  self.collect do |value|
    value = Integer(value)
    value = spec_max + value + 1 if value < 0

    unless spec === value
      fail IndexError, "Subscript invalid or out of range: #{self.inspect}"
    end

    value
  end
end