If string any of the Value matches, I want to output the value
Code:
list = {
"red" => ["apple", "cherry"],
"blue" => ["sky", "cloud"],
"white" => ["paper"]
}
str = "testString"
list.each do |k, v|
puts "string: #{str}"
puts "value: #{v}"
puts /^*#{v}*/.match? str.to_s
end
I expect the output is false because nothing matches
but the actual output is all true..
string: testString
value: String
true
string: testString
value: String
true
string: testString
value: String
true
If "testString" matches any of the "Value"
how can print the key of value?
The code below is my wrong code.
list.each do |k, v|
puts "string: #{str}"
puts "value: #{v}"
if /^*#{v.to_s}*/.match? str
puts "key of value is : #{k}"
end
end