aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorKevin Deisz <kevin.deisz@gmail.com>2015-05-28 20:40:27 -0400
committerKevin Deisz <kevin.deisz@gmail.com>2015-05-28 20:41:03 -0400
commitd909211736b1e85cb011a1a3a87c08d1d1f32a63 (patch)
tree093ff74c6cddca72964fed20f47542dad37a9ff4 /activesupport/lib/active_support
parent2f9d88954caf4fd75098e813a3ab8bf50ef6ace4 (diff)
downloadrails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.tar.gz
rails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.tar.bz2
rails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.zip
Add Enumerable#pluck.
Allows fetching the same values from arrays as from ActiveRecord associations.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 7a893292b3..d28f26260e 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -71,6 +71,14 @@ module Enumerable
def without(*elements)
reject { |element| elements.include?(element) }
end
+
+ # Convert an enumerable to an array based on the given key.
+ #
+ # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
+ # => ["David", "Rafael", "Aaron"]
+ def pluck(key)
+ map { |element| element[key] }
+ end
end
class Range #:nodoc: