Project

General

Profile

Feature #15975

Add Array#pluck

Added by lewispb (Lewis Buckley) about 1 year ago. Updated 10 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:93486]

Description

Inspired by https://github.com/rails/rails/issues/20339

While developing web applications I've often wanted to quickly extract an array of values from an array of hashes.

With an array of objects, this is possible:

irb(main):001:0> require 'ostruct'
=> true
irb(main):002:0> [OpenStruct.new(name: "Lewis")].map(&:name)
=> ["Lewis"]

This PR adds Array#pluck allowing this:

irb(main):001:0> [ {name: "Lewis"} ].pluck(:name)
=> ["Lewis"]

without this PR:

irb(main):001:0> [ {name: "Lewis"} ].map { |item| item[:name] }
=> ["Lewis"]

Implemented here:

https://github.com/ruby/ruby/pull/2263

Also available in: Atom PDF