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



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

def initialize(array)
  @array = array
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



11
12
13
# File 'lib/spicerack/array_index.rb', line 11

def array
  @array
end

Instance Method Details

#freezeObject



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

def freeze
  @array = array.deep_dup.freeze
  index.freeze

  super
end

#indexObject



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

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