我试图弄清楚如何将键和值对从一个过滤器过滤到另一个过滤器
举个例子,我想要这个杂烩
x = { "one" => "one", "two" => "two", "three" => "three"}
y = x.some_function
y == { "one" => "one", "two" => "two"}
谢谢你的帮助
编辑:在本例中,我可能应该提到,我希望它能充当白名单过滤器.也就是说,我知道我想要什么,而不是我不想要的.
我试图弄清楚如何将键和值对从一个过滤器过滤到另一个过滤器
举个例子,我想要这个杂烩
x = { "one" => "one", "two" => "two", "three" => "three"}
y = x.some_function
y == { "one" => "one", "two" => "two"}
谢谢你的帮助
编辑:在本例中,我可能应该提到,我希望它能充当白名单过滤器.也就是说,我知道我想要什么,而不是我不想要的.
也许这就是你想要的.
wanted_keys = %w[one two]
x = { "one" => "one", "two" => "two", "three" => "three"}
x.select { |key,_| wanted_keys.include? key }
数组和散列中包含的可枚举mixin提供了很多有用的方法,比如select/reject/each/等..我建议您使用ri Enumerable查看文档.