Skip to content

Commit 8912af0

Browse files
authored
Merge pull request #644 from ivoanjo/fix-map-enumerator
Fix Map#each and #each_pair not returning enumerator outside of MRI
2 parents df52663 + 0bdc3c0 commit 8912af0

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

lib/concurrent/collection/map/non_concurrent_map_backend.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def clear
9595
end
9696

9797
def each_pair
98-
return enum_for :each_pair unless block_given?
9998
dupped_backend.each_pair do |k, v|
10099
yield k, v
101100
end

lib/concurrent/map.rb

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

174+
def each_pair
175+
return enum_for :each_pair unless block_given?
176+
super
177+
end
178+
174179
alias_method :each, :each_pair unless method_defined?(:each)
175180

176181
def key(value)

spec/concurrent/collection_each_shared.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@
4242
end
4343
end
4444
end
45+
46+
context 'when no block is given' do
47+
it 'returns an enumerator' do
48+
@cache[:a] = 1
49+
@cache[:b] = 2
50+
51+
expect(@cache.send(method)).to be_a Enumerator
52+
end
53+
54+
it 'returns an object which is enumerable' do
55+
@cache[:a] = 1
56+
@cache[:b] = 2
57+
58+
expect(@cache.send(method).to_a).to contain_exactly([:a, 1], [:b, 2])
59+
end
60+
end
4561
end

spec/concurrent/map_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ module Concurrent
255255
end
256256

257257
it 'updates dont block reads' do
258+
if defined?(Concurrent::Collection::SynchronizedMapBackend) && described_class <= Concurrent::Collection::SynchronizedMapBackend
259+
skip("Test does not apply to #{Concurrent::Collection::SynchronizedMapBackend}")
260+
end
261+
258262
getters_count = 20
259263
key_klass = Concurrent::ThreadSafe::Test::HashCollisionKey
260264
keys = [key_klass.new(1, 100),

0 commit comments

Comments
 (0)