diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-06-01 12:40:19 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-06-01 12:40:19 -0300 |
commit | 4e63bde623a0478ec55bcbfbe1afd8b4938148c8 (patch) | |
tree | a72e1caa12f5c7db65851147c5a1da370025783b /activesupport/lib | |
parent | bdfc662a1195d13e3ef78cc8e79df8a8a2e3ca97 (diff) | |
parent | 777fa257aaa962bf67f6e5522efaa420ea7dc88b (diff) | |
download | rails-4e63bde623a0478ec55bcbfbe1afd8b4938148c8.tar.gz rails-4e63bde623a0478ec55bcbfbe1afd8b4938148c8.tar.bz2 rails-4e63bde623a0478ec55bcbfbe1afd8b4938148c8.zip |
Merge pull request #20362 from kddeisz/enumerable_pluck
Allow Enumerable#pluck to take a splat.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 11 |
1 files changed, 9 insertions, 2 deletions
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 |