Skip to content

Commit ad7191f

Browse files
motorollerbyroot
authored andcommitted
Implement variadic hset
1 parent ba82682 commit ad7191f

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

lib/redis.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,15 +2105,17 @@ def hlen(key)
21052105
end
21062106
end
21072107

2108-
# Set the string value of a hash field.
2108+
# Set one or more hash values.
2109+
#
2110+
# @example
2111+
# redis.hset("hash", "f1", "v1", "f2", "v2") # => 2
21092112
#
21102113
# @param [String] key
2111-
# @param [String] field
2112-
# @param [String] value
2113-
# @return [Boolean] whether or not the field was **added** to the hash
2114-
def hset(key, field, value)
2114+
# @param [Array<String>] attrs array of fields and values
2115+
# @return [Integer] The number of fields that were added to the hash
2116+
def hset(key, *attrs)
21152117
synchronize do |client|
2116-
client.call([:hset, key, field, value], &Boolify)
2118+
client.call([:hset, key, *attrs])
21172119
end
21182120
end
21192121

lib/redis/distributed.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ def hlen(key)
669669
node_for(key).hlen(key)
670670
end
671671

672-
# Set the string value of a hash field.
673-
def hset(key, field, value)
674-
node_for(key).hset(key, field, value)
672+
# Set multiple hash fields to multiple values.
673+
def hset(key, *attrs)
674+
node_for(key).hset(key, *attrs)
675675
end
676676

677677
# Set the value of a hash field, only if the field does not exist.

test/lint/hashes.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ module Lint
44
module Hashes
55

66
def test_hset_and_hget
7-
r.hset("foo", "f1", "s1")
7+
assert_equal 1, r.hset("foo", "f1", "s1")
88

99
assert_equal "s1", r.hget("foo", "f1")
1010
end
1111

12+
def test_variadic_hset
13+
target_version "4.0.0" do
14+
assert_equal 2, r.hset("foo", "f1", "s1", "f2", "s2")
15+
16+
assert_equal "s1", r.hget("foo", "f1")
17+
assert_equal "s2", r.hget("foo", "f2")
18+
end
19+
end
20+
1221
def test_hsetnx
1322
r.hset("foo", "f1", "s1")
1423
r.hsetnx("foo", "f1", "s2")

0 commit comments

Comments
 (0)