module Corefines::Enumerable::IndexBy

@!method index_by

Convert enumerable into a Hash, iterating over each element where the
provided block must return the key to be used to map to the value.

It's similar to {::Enumerable#group_by}, but when two elements
corresponds to the same key, then only the last one is preserved in the
resulting Hash.

@example
  people.index_by(&:login)
  => { "flynn" => <Person @login="flynn">, "bradley" => <Person @login="bradley">, ...}

@example
  people.index_by.each(&:login)
  => { "flynn" => <Person @login="flynn">, "bradley" => <Person @login="bradley">, ...}

@yield [obj] gives each element to the block.
@yieldreturn the key to be used to map to the value.
@return [Hash]

Public Instance Methods

index_by() click to toggle source
# File lib/corefines/enumerable.rb, line 32
def index_by
  ::Hash[map { |elem| [ yield(elem), elem ] }]
end