Skip to content

Commit d894aa6

Browse files
author
Petr Chalupa
authored
Merge pull request #586 from lucasallan/map_each
Add each as an alias for each_pair for Concurrent::Map
2 parents 3894d5b + d83fb11 commit d894aa6

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

lib/concurrent/map.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def each_value
171171
each_pair {|k, v| yield v}
172172
end unless method_defined?(:each_value)
173173

174+
alias_method :each, :each_pair unless method_defined?(:each)
175+
174176
def key(value)
175177
each_pair {|k, v| return k if v == value}
176178
nil
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
shared_examples :collection_each do
2+
3+
it 'common' do
4+
@cache.send(method) { |k, v| fail }
5+
expect(@cache).to eq @cache.send(method) {}
6+
@cache[:a] = 1
7+
8+
h = {}
9+
@cache.send(method) { |k, v| h[k] = v }
10+
expect({:a => 1}).to eq h
11+
12+
@cache[:b] = 2
13+
h = {}
14+
@cache.send(method) { |k, v| h[k] = v }
15+
expect({:a => 1, :b => 2}).to eq h
16+
end
17+
18+
it 'pair iterator' do
19+
@cache[:a] = 1
20+
@cache[:b] = 2
21+
i = 0
22+
r = @cache.send(method) do |k, v|
23+
if i == 0
24+
i += 1
25+
next
26+
fail
27+
elsif i == 1
28+
break :breaked
29+
end
30+
end
31+
32+
expect(:breaked).to eq r
33+
end
34+
35+
it 'allows modification' do
36+
@cache[:a] = 1
37+
@cache[:b] = 1
38+
@cache[:c] = 1
39+
40+
expect_size_change(1) do
41+
@cache.send(method) do |k, v|
42+
@cache[:z] = 1
43+
end
44+
end
45+
end
46+
end

spec/concurrent/map_spec.rb

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require_relative 'collection_each_shared'
12
Thread.abort_on_exception = true
23

34
module Concurrent
@@ -623,48 +624,14 @@ def key # assert_collision_resistance expects to be able to call .key to get the
623624
end
624625

625626
describe '#each_pair' do
626-
it 'common' do
627-
@cache.each_pair { |k, v| fail }
628-
expect(@cache).to eq @cache.each_pair {}
629-
@cache[:a] = 1
630-
631-
h = {}
632-
@cache.each_pair { |k, v| h[k] = v }
633-
expect({:a => 1}).to eq h
634-
635-
@cache[:b] = 2
636-
h = {}
637-
@cache.each_pair { |k, v| h[k] = v }
638-
expect({:a => 1, :b => 2}).to eq h
639-
end
640-
641-
it 'pair iterator' do
642-
@cache[:a] = 1
643-
@cache[:b] = 2
644-
i = 0
645-
r = @cache.each_pair do |k, v|
646-
if i == 0
647-
i += 1
648-
next
649-
fail
650-
elsif i == 1
651-
break :breaked
652-
end
653-
end
654-
655-
expect(:breaked).to eq r
627+
it_should_behave_like :collection_each do
628+
let(:method) { :each_pair }
656629
end
630+
end
657631

658-
it 'allows modification' do
659-
@cache[:a] = 1
660-
@cache[:b] = 1
661-
@cache[:c] = 1
662-
663-
expect_size_change(1) do
664-
@cache.each_pair do |k, v|
665-
@cache[:z] = 1
666-
end
667-
end
632+
describe '#each' do
633+
it_should_behave_like :collection_each do
634+
let(:method) { :each }
668635
end
669636
end
670637

0 commit comments

Comments
 (0)