Method: Enumerable#pluck

Defined in:
lib/active_support/core_ext/enumerable.rb

#pluck(*keys) ⇒ Object

Convert an enumerable to an array based on the given key.

[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
  => ["David", "Rafael", "Aaron"]

[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
  => [[1, "David"], [2, "Rafael"]]


83
84
85
86
87
88
89
# File 'lib/active_support/core_ext/enumerable.rb', line 83

def pluck(*keys)
  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    map { |element| element[keys.first] }
  end
end