Method: Enumerable#pluck

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

#pluck(*keys) ⇒ Object

Extract the given key from each element in the enumerable.

[{ 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"]]


152
153
154
155
156
157
158
159
# File 'lib/active_support/core_ext/enumerable.rb', line 152

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