Class: Spicerack::ArrayIndex

Inherits:
Object
  • Object
show all
Includes:
ShortCircuIt
Defined in:
lib/spicerack/array_index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*array) ⇒ ArrayIndex

Returns a new instance of ArrayIndex.



19
20
21
22
23
24
25
# File 'lib/spicerack/array_index.rb', line 19

def initialize(*array)
  if array.length == 1 && array[0].respond_to?(:to_ary)
    @array = array[0].to_a
  else
    @array = array
  end
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



14
15
16
# File 'lib/spicerack/array_index.rb', line 14

def array
  @array
end

Instance Method Details

#freezeObject



34
35
36
37
38
39
# File 'lib/spicerack/array_index.rb', line 34

def freeze
  @array = _deep_freeze_and_dup_object(array).freeze
  index.freeze

  super
end

#indexObject



27
28
29
30
31
# File 'lib/spicerack/array_index.rb', line 27

def index
  array.each_with_index.each_with_object({}) do |(element, index), hash|
    hash[element] = index unless hash.key?(element)
  end
end