Skip to content

Commit bf34437

Browse files
committed
add Array#pluck
1 parent 59be51c commit bf34437

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ Enable the helpers by requiring the `support` submodule:
191191
require 'yake/support'
192192
```
193193

194+
`Array` helpers:
195+
196+
```ruby
197+
array = [{ a: 'b'}, { a: 'c' }, { a: 'd' }]
198+
199+
array.pluck(:a)
200+
# => ["b", "c", "d"]
201+
202+
array.to_dynamodb
203+
# => {:L=>[{:a=>{:S=>"b"}}, {:a=>{:S=>"c"}}, {:a=>{:S=>"d"}}]}
204+
```
205+
194206
`Object` helpers:
195207

196208
```ruby

lib/yake/support.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'uri'
77

88
class Array
9+
def pluck(key) = map { |x| x[key] }
910
def to_dynamodb = { L: map(&:to_dynamodb) }
1011
end
1112

spec/support_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
end
7676
end
7777

78+
context '#pluck' do
79+
it 'should return a list of the plucked values' do
80+
expect(([subject] * 3).pluck('fizz')).to eq(%w[buzz] * 3)
81+
end
82+
end
83+
7884
context '#strict_encode64' do
7985
it 'should transform a Hash to a strict Base64-encoded string' do
8086
expect(subject.strict_encode64).to eq 'eyJmaXp6IjoiYnV6eiIsImphenoiOiJmdXp6IiwicmF6eiI6W3sibXRheiI6ImhhenoifV0sIndpenoiOnsia2l6eiI6ImthenoifX0='

0 commit comments

Comments
 (0)