私は以下のコードを持っていますが、実行するとIF文のコードは決して実行されません。ルビで文のチェックが機能しない場合
変数c
とkey
の値が下の太字の出力と同じでも、IF文が実行されない理由はわかりません。 key
がsymbol
、ないstring
であるためである
text = 'cab'
letters = {
'a': 1,
'b': 2,
'c': 3,
}
text.split("").each do |c|
puts c
puts "Text #{c}"
for key, value in letters
puts "Key #{key}, Text #{c}, Value #{value}"
if c == key
puts "hello #{c}"
end
end
end
出力
c Text c Key a, Text c, Value 1 Key b, Text c, Value 2 Key c, Text c, Value 3 a Text a Key a, Text a, Value 1 Key b, Text a, Value 2 Key c, Text a, Value 3 b Text b Key a, Text b, Value 1 Key b, Text b, Value 2 Key c, Text b, Value 3
あなたのキーはシンボルで、文字列に変更します: 'if c == key.to_s' – 7urkm3n