mercredi 27 juillet 2016

if statement in ruby, to determine input type

First of all I'm new to ruby, and all of coding. I do this as a hobby and work in a totally different field. I'm trying to make a small hangman game in ruby. I am trying to validate the guess input using the following code

puts "Enter a word: "
answer = gets.chomp
tries = 5
answer_array = answer.chars

until tries == 0
        hits = ""
        puts "enter your guess: "
        guess = gets[0]
        if guess.class != "String"
                puts "enter a letter!!"
        else
                puts "nice!!!"
        end
        tries -= 1
end

for some reason, whatever input I get for "guess", the program throws "enter a letter!". Is there something wrong with the way I defined the if statement. I just want it to differentiate between a String and an Integer. Thanks in advance!!

Aucun commentaire:

Enregistrer un commentaire