From f1b4cacbae71585be3f5dcb4c1bdd7c78ef3d590 Mon Sep 17 00:00:00 2001 From: lest Date: Wed, 21 Dec 2011 19:07:00 +0300 Subject: remove Enumerable#each_with_object from core_ext as it is present in ruby 1.9 --- .../source/active_support_core_extensions.textile | 26 ---------------------- 1 file changed, 26 deletions(-) (limited to 'railties') diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index a5f9cd483b..ee4565fdfe 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -2038,32 +2038,6 @@ people.pluck(:name) # => [ "David Heinemeier Hansson", "Jamie Heinemeier Hansson NOTE: Defined in +active_support/core_ext/enumerable.rb+. -h4. +each_with_object+ - -The +inject+ method offers iteration with an accumulator: - - -[2, 3, 4].inject(1) {|product, i| product*i } # => 24 - - -The block is expected to return the value for the accumulator in the next iteration, and this makes building mutable objects a bit cumbersome: - - -[1, 2].inject({}) {|h, i| h[i] = i**2; h} # => {1 => 1, 2 => 4} - - -See that spurious "+; h+"? - -Active Support backports +each_with_object+ from Ruby 1.9, which addresses that use case. It iterates over the collection, passes the accumulator, and returns the accumulator when done. You normally modify the accumulator in place. The example above would be written this way: - - -[1, 2].each_with_object({}) {|i, h| h[i] = i**2} # => {1 => 1, 2 => 4} - - -WARNING. Note that the item of the collection and the accumulator come in different order in +inject+ and +each_with_object+. - -NOTE: Defined in +active_support/core_ext/enumerable.rb+. - h4. +index_by+ The method +index_by+ generates a hash with the elements of an enumerable indexed by some key. -- cgit v1.2.3