Skip to content

Commit c9350a2

Browse files
committed
Refactor *scan_each methods to capture and pass the block.
It's faster and cleaner.
1 parent aa81254 commit c9350a2

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lib/redis.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,12 +2291,12 @@ def scan(cursor, options={})
22912291
# - `:count => Integer`: return count keys at most per iteration
22922292
#
22932293
# @return [Enumerator] an enumerator for all found keys
2294-
def scan_each(options={})
2294+
def scan_each(options={}, &block)
22952295
return to_enum(:scan_each, options) unless block_given?
22962296
cursor = 0
22972297
loop do
22982298
cursor, keys = scan(cursor, options)
2299-
keys.each {|key| yield key}
2299+
keys.each(&block)
23002300
break if cursor == "0"
23012301
end
23022302
end
@@ -2329,12 +2329,12 @@ def hscan(key, cursor, options={})
23292329
# - `:count => Integer`: return count keys at most per iteration
23302330
#
23312331
# @return [Enumerator] an enumerator for all found keys
2332-
def hscan_each(key, options={})
2332+
def hscan_each(key, options={}, &block)
23332333
return to_enum(:hscan_each, key, options) unless block_given?
23342334
cursor = 0
23352335
loop do
2336-
cursor, key_values = hscan(key, cursor, options)
2337-
key_values.each {|key, val| yield key, val }
2336+
cursor, values = hscan(key, cursor, options)
2337+
values.each(&block)
23382338
break if cursor == "0"
23392339
end
23402340
end
@@ -2368,12 +2368,12 @@ def zscan(key, cursor, options={})
23682368
# - `:count => Integer`: return count keys at most per iteration
23692369
#
23702370
# @return [Enumerator] an enumerator for all found scores and members
2371-
def zscan_each(key, options={})
2371+
def zscan_each(key, options={}, &block)
23722372
return to_enum(:zscan_each, key, options) unless block_given?
23732373
cursor = 0
23742374
loop do
2375-
cursor, member_score = zscan(key, cursor, options)
2376-
member_score.each {|member, score| yield member, score }
2375+
cursor, values = zscan(key, cursor, options)
2376+
values.each(&block)
23772377
break if cursor == "0"
23782378
end
23792379
end
@@ -2404,12 +2404,12 @@ def sscan(key, cursor, options={})
24042404
# - `:count => Integer`: return count keys at most per iteration
24052405
#
24062406
# @return [Enumerator] an enumerator for all keys in the set
2407-
def sscan_each(key, options={})
2407+
def sscan_each(key, options={}, &block)
24082408
return to_enum(:sscan_each, key, options) unless block_given?
24092409
cursor = 0
24102410
loop do
24112411
cursor, keys = sscan(key, cursor, options)
2412-
keys.each {|key| yield key}
2412+
keys.each(&block)
24132413
break if cursor == "0"
24142414
end
24152415
end

test/scanning_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ def test_hscan_each_block
281281
r.hmset "hash", *elements
282282

283283
keys_from_scan = []
284-
r.hscan_each("hash") do |*key_value|
285-
keys_from_scan << key_value
284+
r.hscan_each("hash") do |field, value|
285+
keys_from_scan << [field, value]
286286
end
287287
all_keys = r.hgetall("hash").to_a
288288

@@ -301,8 +301,8 @@ def test_hscan_each_block_match
301301
r.hmset "hash", *elements
302302

303303
keys_from_scan = []
304-
r.hscan_each("hash", :match => "key:1?") do |*key_value|
305-
keys_from_scan << key_value
304+
r.hscan_each("hash", :match => "key:1?") do |field, value|
305+
keys_from_scan << [field, value]
306306
end
307307
all_keys = r.hgetall("hash").to_a.select{|k,v| k =~ /^key:1.$/}
308308

@@ -388,8 +388,8 @@ def test_zscan_each_block
388388
r.zadd "zset", elements
389389

390390
scores_from_scan = []
391-
r.zscan_each("zset") do |*key_score|
392-
scores_from_scan << key_score
391+
r.zscan_each("zset") do |member, score|
392+
scores_from_scan << [member, score]
393393
end
394394
member_scores = r.zrange("zset", 0, -1, :with_scores => true)
395395

@@ -408,8 +408,8 @@ def test_zscan_each_block_match
408408
r.zadd "zset", elements
409409

410410
scores_from_scan = []
411-
r.zscan_each("zset", :match => "key:1??") do |*key_score|
412-
scores_from_scan << key_score
411+
r.zscan_each("zset", :match => "key:1??") do |member, score|
412+
scores_from_scan << [member, score]
413413
end
414414
member_scores = r.zrange("zset", 0, -1, :with_scores => true)
415415
filtered_members = member_scores.select{|k,s| k =~ /^key:1..$/}

0 commit comments

Comments
 (0)