Browse files

AS guide: documents Enumerable#index_by

  • Loading branch information...
1 parent 1ec3cdc commit e79c8bcd7b47979338c2a1c78d21f13a866e7763 @fxn fxn committed Sep 22, 2009
Showing with 13 additions and 0 deletions.
  1. +13 −0 railties/guides/source/active_support_overview.textile
View
13 railties/guides/source/active_support_overview.textile
@@ -871,6 +871,19 @@ Active Support backports +each_with_object+ from Ruby 1.9, which addresses that
WARNING. Note that the item of the collection and the accumulator come in different order in +inject+ and +each_with_object+.
+h4. +index_by+
+
+The method +index_by+ generates a hash with the elements of an enumerable indexed by some key.
+
+It iterates through the collection and passes each element to a block. The element will be keyed by the value returned by the block:
+
+<ruby>
+invoices.index_by(&:number)
+# => {'2009-032' => <Invoice ...>, '2009-008' => <Invoice ...>, ...}
+</ruby>
+
+WARNING. Keys should normally be unique. If the block returns the same value for different elements no collection is built for that key. The last item will win.
+
h3. Extensions to +Array+
h4. Accessing

0 comments on commit e79c8bc

Please sign in to comment.