["autocomplete_foo", -6ren">
gpt4 book ai didi

ruby - 删除 redis-rb 中的多个键

转载 作者:IT王子 更新时间:2023-10-29 05:54:51 26 4
gpt4 key购买 nike

在 Rails 应用程序中使用 redis-rb,以下内容不起作用:

irb> keys = $redis.keys("autocomplete*")
=> ["autocomplete_foo", "autocomplete_bar", "autocomplete_bat"]
irb> $redis.del(keys)
=> 0

这很好用:

irb> $redis.del("autocomplete_foo", "autocomplete_bar")
=> 2

我是否遗漏了一些明显的东西?来源只是:

# Delete a key.
def del(*keys)
synchronize do
@client.call [:del, *keys]
end
end

在我看来它应该可以传递一个数组...?

最佳答案

splat 运算符工作方式的一些编码探索:

def foo(*keys)
puts keys.inspect
end

>> foo("hi", "there")
["hi", "there"]

>> foo(["hi", "there"])
[["hi", "there"]]

>> foo(*["hi", "there"])
["hi", "there"]

因此传入常规数组将导致该数组作为单个项目进行评估,这样您就可以在方法中的数组中获得一个数组。如果在调用方法时在数组前加上 *:

$redis.del(*keys)

这让方法知道解压它/不接受任何进一步的参数。这样应该可以解决您遇到的问题!

只是为了进一步说明,这是可行的:

>> foo("hello", *["hi", "there"])

这会导致语法错误:

>> foo("hello", *["hi", "there"], "world")

关于ruby - 删除 redis-rb 中的多个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6061996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com