Skip to content

Commit 87df483

Browse files
committed
Accept a hash as value in hset
1 parent ad7191f commit 87df483

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/redis.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,11 +2109,16 @@ def hlen(key)
21092109
#
21102110
# @example
21112111
# redis.hset("hash", "f1", "v1", "f2", "v2") # => 2
2112+
# redis.hset("hash", { "f1" => "v1", "f2" => "v2" }) # => 2
21122113
#
21132114
# @param [String] key
2114-
# @param [Array<String>] attrs array of fields and values
2115+
# @param [Array<String> | Hash<String, String>] attrs array or hash of fields and values
21152116
# @return [Integer] The number of fields that were added to the hash
21162117
def hset(key, *attrs)
2118+
if attrs.size == 1 && attrs.first.is_a?(Hash)
2119+
attrs = attrs.first.flatten
2120+
end
2121+
21172122
synchronize do |client|
21182123
client.call([:hset, key, *attrs])
21192124
end

test/lint/hashes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def test_variadic_hset
1515

1616
assert_equal "s1", r.hget("foo", "f1")
1717
assert_equal "s2", r.hget("foo", "f2")
18+
19+
assert_equal 2, r.hset("bar", { "f1" => "s1", "f2" => "s2" })
20+
21+
assert_equal "s1", r.hget("bar", "f1")
22+
assert_equal "s2", r.hget("bar", "f2")
1823
end
1924
end
2025

0 commit comments

Comments
 (0)