Method: Enumerable#index_by
- Defined in:
- lib/passive_support/core_ext/enumerable.rb
#index_by ⇒ Object
Convert an enumerable to a hash. Examples:
people.index_by(&:login)
=> { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
=> { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
92 93 94 95 96 97 |
# File 'lib/passive_support/core_ext/enumerable.rb', line 92 def index_by inject({}) do |accum, elem| accum[yield(elem)] = elem accum end end |