diff options
author | Xavier Noria <fxn@hashref.com> | 2009-09-22 09:09:53 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2009-09-22 23:09:30 +0200 |
commit | e79c8bcd7b47979338c2a1c78d21f13a866e7763 (patch) | |
tree | d1d54f11780eb12e70c61d50f0037c418d1ee4a1 /railties/guides | |
parent | 1ec3cdce592a864770f7449971043dd6a0ae0222 (diff) | |
download | rails-e79c8bcd7b47979338c2a1c78d21f13a866e7763.tar.gz rails-e79c8bcd7b47979338c2a1c78d21f13a866e7763.tar.bz2 rails-e79c8bcd7b47979338c2a1c78d21f13a866e7763.zip |
AS guide: documents Enumerable#index_by
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 13 |
1 files changed, 13 insertions, 0 deletions
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: + +<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 |