Skip to content

Commit

Permalink
Revert "Added Enumerable#pluck to wrap the common pattern of collect(…
Browse files Browse the repository at this point in the history
…&: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
tenderlove committed Dec 22, 2011
1 parent 2adc145 commit 367741e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 0 additions & 2 deletions activesupport/CHANGELOG.md
Expand Up @@ -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.

Expand Down
25 changes: 20 additions & 5 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Expand Up @@ -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*

This comment has been minimized.

Copy link
@exviva

exviva Dec 22, 2011

Contributor

Oops?

This comment has been minimized.

Copy link
@vijaydev

vijaydev Dec 22, 2011

Member

That's already fixed.


# Convert an enumerable to a hash. Examples:
#
Expand Down
7 changes: 0 additions & 7 deletions activesupport/test/core_ext/enumerable_test.rb
Expand Up @@ -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

@fxn
Copy link
Member

@fxn fxn commented on 367741e Dec 23, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@audionerd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Enumerable#pluck gone for good then?

Please sign in to comment.