instance method
index_by
Ruby on Rails 7.1.6
Since v2.2.3Signature
index_by()
Convert an enumerable to a hash, using the block result as the key and the element as the value.
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 ...>, ...}
Source
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 52
def index_by
if block_given?
result = {}
each { |elem| result[yield(elem)] = elem }
result
else
to_enum(:index_by) { size if respond_to?(:size) }
end
end
Defined in activesupport/lib/active_support/core_ext/enumerable.rb line 52
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Enumerable