samedi 28 mai 2016

if statement check not working in ruby

I have the following code, but when I run it, the code in the IF statement never gets executed.

I don't understand why the IF statement never gets executed despite the value of variable c and key being the same as shown in the output in bold below.

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

Output

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

Aucun commentaire:

Enregistrer un commentaire