提交 4d20de8a 编写于 作者: D David Heinemeier Hansson

Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*

上级 76a3ec7f
## Rails 3.2.0 (unreleased) ##
* Module#synchronize is deprecated with no replacement. Please use `monitor`
from ruby's standard library.
* 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.
* (Date|DateTime|Time)#beginning_of_week accept an optional argument to
be able to set the day at which weeks are assumed to start.
......
......@@ -63,6 +63,13 @@ 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:
#
# people.pluck(:name) # => [ "David Heinemeier Hansson", "Jamie Heinemeier Hansson" ]
def pluck(method)
collect { |element| element.send(method) }
end
# 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:
......
......@@ -126,4 +126,11 @@ def test_exclude?
assert_equal true, GenericEnumerable.new([ 1 ]).exclude?(2)
assert_equal false, GenericEnumerable.new([ 1 ]).exclude?(1)
end
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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册