index_by()
public
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 ...>, ...}
Show source
def index_by
return to_enum :index_by unless block_given?
Hash[map { |elem| [yield(elem), elem] }]
end