From 777fa257aaa962bf67f6e5522efaa420ea7dc88b Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Fri, 29 May 2015 09:41:03 -0400 Subject: Allow Enumerable#pluck to take a splat. This allows easier integration with ActiveRecord, such that AR#pluck will now use Enumerable#pluck if the relation is loaded, without needing to hit the database. --- activesupport/lib/active_support/core_ext/enumerable.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index d28f26260e..fc7531d088 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -76,8 +76,15 @@ module Enumerable # # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name) # => ["David", "Rafael", "Aaron"] - def pluck(key) - map { |element| element[key] } + # + # [{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name) + # => [[1, "David"], [2, "Rafael"]] + def pluck(*keys) + if keys.many? + map { |element| keys.map { |key| element[key] } } + else + map { |element| element[keys.first] } + end end end -- cgit v1.2.3