Project

General

Profile

Actions

Feature #15975

closed

Add Array#pluck

Added by lewispb (Lewis Buckley) almost 5 years ago. Updated over 3 years ago.

Status:
Rejected
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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0