Method: Enumerable#index_to

Defined in:
lib/casual_support/enumerable/index_to.rb

#index_to {|key| ... } ⇒ Hash<'K, 'V>

Converts the Enumerable into a Hash, using its elements as keys and using the given block to compute an associated value for each key.

Examples:

cache = id_list.index_to{|id| find_by_id(id) }

Yields:

  • (key)

Yield Parameters:

  • key ('K)

Yield Returns:

  • ('V)

Returns:



15
16
17
# File 'lib/casual_support/enumerable/index_to.rb', line 15

def index_to()
  self.reduce({}){|h, k| h.put!(k, (yield k)) }
end