Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 哈希在数组中的值作为值_Ruby_Arrays_Hash - Fatal编程技术网

Ruby 哈希在数组中的值作为值

Ruby 哈希在数组中的值作为值,ruby,arrays,hash,Ruby,Arrays,Hash,我想在数组中找到某个值作为散列中的值。如果我只是使用ans.has\u value?(“car”),它只能在不在数组中时找到这样的值。但是如果我使用ans.has_value?([“car”]),它只能在只有一个[“car”]的情况下找到true;如果数组中有两个或多个项作为value,则返回false。请建议如何在数组的多个项中查找值 ans=Hash.new ans["a"]=["car"] ans["b"]=["scar"] ans["a"]+=["car"] puts ans.has_v

我想在数组中找到某个值作为散列中的值。如果我只是使用
ans.has\u value?(“car”)
,它只能在不在数组中时找到这样的值。但是如果我使用
ans.has_value?([“car”])
,它只能在只有一个
[“car”]
的情况下找到
true
;如果数组中有两个或多个项作为value,则返回
false
。请建议如何在数组的多个项中查找值

ans=Hash.new
ans["a"]=["car"]
ans["b"]=["scar"]
ans["a"]+=["car"]
puts ans.has_value?(["car"])
puts ans

print "Press ENTER to continue . . ."

gets
这将产生:

false {"a"=>["car", "car"], "b"=>["scar"]} Press ENTER to continue . . . 假的 {“a”=>[“car”,“car”],“b”=>[“scar”]} 按ENTER继续。 这应该起作用:

!!ans.detect{|_, value| value.include?('car')}

我在寻找返回值为true,所以是的,这是我需要的------ans.detect{|,value | value.include?('car')}.last.include?“car”@user2301576我稍微编辑了我的答案。太棒了,我可以问一下|,value |@user2301576中下划线的功能是什么吗。按照惯例,将传递给块的变量标记为未使用。