Skip to content

Commit

Permalink
Accept a hash as value in hset
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jun 9, 2020
1 parent ad7191f commit 87df483
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2109,11 +2109,16 @@ def hlen(key)
#
# @example
# redis.hset("hash", "f1", "v1", "f2", "v2") # => 2
# redis.hset("hash", { "f1" => "v1", "f2" => "v2" }) # => 2
#
# @param [String] key
# @param [Array<String>] attrs array of fields and values
# @param [Array<String> | Hash<String, String>] attrs array or hash of fields and values
# @return [Integer] The number of fields that were added to the hash
def hset(key, *attrs)
if attrs.size == 1 && attrs.first.is_a?(Hash)
attrs = attrs.first.flatten
end

synchronize do |client|
client.call([:hset, key, *attrs])
end
Expand Down
5 changes: 5 additions & 0 deletions test/lint/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def test_variadic_hset

assert_equal "s1", r.hget("foo", "f1")
assert_equal "s2", r.hget("foo", "f2")

assert_equal 2, r.hset("bar", { "f1" => "s1", "f2" => "s2" })

assert_equal "s1", r.hget("bar", "f1")
assert_equal "s2", r.hget("bar", "f2")
end
end

Expand Down

0 comments on commit 87df483

Please sign in to comment.