diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2011-12-02 13:00:03 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2011-12-02 13:00:56 +0100 |
commit | 4d20de8a50d889a09e6f5642984775fe796ca943 (patch) | |
tree | 10f22611c7b0b50a39ea57800500701d58538da7 /activesupport/lib | |
parent | 76a3ec7f6e348681e4bafa8b02ca660ab450b0ee (diff) | |
download | rails-4d20de8a50d889a09e6f5642984775fe796ca943.tar.gz rails-4d20de8a50d889a09e6f5642984775fe796ca943.tar.bz2 rails-4d20de8a50d889a09e6f5642984775fe796ca943.zip |
Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9343bb7106..d9856f2e84 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -63,6 +63,13 @@ module Enumerable 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: |