From d909211736b1e85cb011a1a3a87c08d1d1f32a63 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 28 May 2015 20:40:27 -0400 Subject: Add Enumerable#pluck. Allows fetching the same values from arrays as from ActiveRecord associations. --- activesupport/lib/active_support/core_ext/enumerable.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activesupport/lib/active_support') 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: -- cgit v1.2.3