"one", "two" => "two", "three" => "three"} y = -6ren">
gpt4 book ai didi

Ruby 哈希白名单过滤器

转载 作者:数据小太阳 更新时间:2023-10-29 06:19:54 27 4
gpt4 key购买 nike

我想弄清楚如何将键和值对从一个过滤器过滤到另一个过滤器

例如我想取这个散列

x = { "one" => "one", "two" => "two", "three" => "three"}

y = x.some_function

y == { "one" => "one", "two" => "two"}

谢谢你的帮助

编辑:在这个例子中可能应该提到,我希望它作为一个白名单过滤器。也就是说,我知道我想要什么,而不是我不想要什么。

最佳答案

Rails 的 ActiveSupport 库还为您提供切片,除了在关键级别处理哈希:

y = x.slice("one", "two") # => { "one" => "one", "two" => "two" }
y = x.except("three") # => { "one" => "one", "two" => "two" }
x.slice!("one", "two") # x is now { "one" => "one", "two" => "two" }

这些非常好,我一直在使用它们。

关于Ruby 哈希白名单过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/711863/

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