[ruby-core:93495] [Ruby master Feature#15975] Add Array#pluck
From:
shevegen@...
Date:
2019-07-02 16:14:41 UTC
List:
ruby-core #93495
Issue #15975 has been updated by shevegen (Robert A. Heiler).
Hmm. I don't doubt that this may possibly be useful, but the method name is
a bit ... weird. My first association with this name, oddly enough, is to
associate duck typing with it, and then to "pluck the duck" (yes, strange
association but I could not help it ...).
I do not have a better alternative suggestion for a name, though. It
reminds me a little bit of a more flexible variant of .dig(), though.
----------------------------------------
Feature #15975: Add Array#pluck
https://bugs.ruby-lang.org/issues/15975#change-79048
* Author: lewispb (Lewis Buckley)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
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:
```rb
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:
```rb
irb(main):001:0> [ {name: "Lewis"} ].pluck(:name)
=> ["Lewis"]
```
without this PR:
```rb
irb(main):001:0> [ {name: "Lewis"} ].map { |item| item[:name] }
=> ["Lewis"]
```
Implemented here:
https://github.com/ruby/ruby/pull/2263
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>