Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "Added Enumerable#pluck to wrap the common pattern of collect(…

…&:method) *DHH*"

This reverts commit 4d20de8.

Conflicts:

	activesupport/CHANGELOG.md
	activesupport/lib/active_support/core_ext/enumerable.rb
	activesupport/test/core_ext/enumerable_test.rb
  • Loading branch information...
commit 367741ef22d1538b8550cf6e4b2276a0946066c0 1 parent 2adc145
Aaron Patterson tenderlove authored
2  activesupport/CHANGELOG.md
View
@@ -7,8 +7,6 @@
* Add ActiveSupport::Cache::NullStore for use in development and testing. *Brian Durand*
-* Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*
-
* Module#synchronize is deprecated with no replacement. Please use `monitor`
from ruby's standard library.
25 activesupport/lib/active_support/core_ext/enumerable.rb
View
@@ -26,12 +26,27 @@ def sum(identity = 0, &block)
end
end
- # Plucks the value of the passed method for each element and returns the result as an array. Example:
+ # Iterates over a collection, passing the current element *and* the
+ # +memo+ to the block. Handy for building up hashes or
+ # reducing collections down to one object. Examples:
#
- # people.pluck(:name) # => [ "David Heinemeier Hansson", "Jamie Heinemeier Hansson" ]
- def pluck(method)
- collect { |element| element.send(method) }
- end
+ # %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
+ # # => {'foo' => 'FOO', 'bar' => 'BAR'}
+ #
+ # *Note* that you can't use immutable objects like numbers, true or false as
+ # the memo. You would think the following returns 120, but since the memo is
+ # never changed, it does not.
+ #
+ # (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1
+ #
+ def each_with_object(memo)
+ return to_enum :each_with_object, memo unless block_given?
+ each do |element|
+ yield element, memo
+ end
+ memo
+ end unless [].respond_to?(:each_with_object)
+>>>>>>> parent of 4d20de8... Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*
Olek Janiszewski
exviva added a note

Oops?

Vijay Dev Collaborator

That's already fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
# Convert an enumerable to a hash. Examples:
#
7 activesupport/test/core_ext/enumerable_test.rb
View
@@ -117,11 +117,4 @@ def test_exclude?
assert_equal true, GenericEnumerable.new([ 1 ]).exclude?(2)
assert_equal false, GenericEnumerable.new([ 1 ]).exclude?(1)
end
-
- def test_pluck_single_method
- person = Struct.new(:name)
- people = [ person.new("David"), person.new("Jamie") ]
-
- assert_equal [ "David", "Jamie" ], people.pluck(:name)
- end
end

2 comments on commit 367741e

Xavier Noria
Owner

The AS guide had a section for it, I have removed it in a64ab95

Eric Skogen

Is Enumerable#pluck gone for good then?

Please sign in to comment.
Something went wrong with that request. Please try again.