From e79c8bcd7b47979338c2a1c78d21f13a866e7763 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 22 Sep 2009 09:09:53 +0200 Subject: AS guide: documents Enumerable#index_by --- railties/guides/source/active_support_overview.textile | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index d09b8cde88..3f6e9b306d 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/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: + + +invoices.index_by(&:number) +# => {'2009-032' => , '2009-008' => , ...} + + +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 -- cgit v1.2.3