Rails' index_by: the easy way to convert an Array to a Hash

As Aditya Sanghi notes in “Quickly convert an Array to a Hash”, a very quick way to convert an Array to a Hash, is to use Rails’ Enumerable#index_by:

Post.all.index_by { |post| post.id } # => { 1 => #<Post ...>, 2 => #<Post ...>, ... } Post.all.index_by(&:title) # => { "My first post" => #<Post ...>, "My second post" => #<Post ...>, ... }