Search⌘ K
AI Features

Improve the Proposal Check

Understand how to enhance input validation in a Ruby hangman game by converting uppercase letters to lowercase and rejecting inputs longer than one letter. Learn to apply string methods like chomp and downcase, and control flow using conditional statements to improve user input handling.

Towards the end of the last lesson, we identified the following unwanted behaviors:

  • Giving an uppercase letter doesn’t work.
  • Giving two letters instead of one results in a two-letter comparison.

Handle the uppercase letter

Let’s understand the scenario first and try to fix it.

Ruby
hidden_word = "hello"
puts "_ _ _ _ _"
puts
print "Give me a letter: "
answer = gets
answer = answer.chomp.downcase
if hidden_word.include?(answer.chomp)
puts "The letter is part of the hidden word"
else
puts "Invalid letter"
end

We add one more process at line 9, right after giving a name (answer) to the letter that the user proposed.

We send the chomp message to remove the carriage return character and send the downcase message to the produced result. As we said before, every action produces a result to which we can send another message. Here, chomp produces a new string to which we send the downcase message. The string understands the message and responds by changing all the characters to lowercase letters, producing a new equivalent string with all lowercase letters.

We give the same name answer, to the received string (we cannot recall the old string anymore).

Now, at line 11, we compare the lowercase letters with the lowercase letters of the hidden word.

hidden_word = "hello"

puts "_ _ _ _ _"
puts

print "Give me a letter: "

answer = gets
answer = answer.chomp.downcase

if hidden_word.include?(answer.chomp)
    puts "The letter is part of the hidden word"
else
    puts "Invalid letter"
end
Uppercase input

If we execute the above code, we will see that the letter H is accepted as valid input.

root@educative:/usercode# ruby hangman.rb
_ _ _ _ _

Give me a letter: H
The letter is part of the hidden word

Handle the multiple-letter input

Let’s turn to the two-letter case. We must ensure that the proposal does not contain two letters and, if it does, then reject the proposal.

Ruby
hidden_word = "hello"
puts "_ _ _ _ _"
puts
print "Give me a letter: "
answer = gets
answer = answer.chomp.downcase
if answer.length > 1
puts "You must give only one letter!"
elsif hidden_word.include?(answer)
puts "The letter is part of the hidden word"
else
puts "Invalid letter"
end

We added a new choice at line 11 that tests the number of characters that the player typed. After the if word, Ruby sees an answer that refers to the proposal. We just turned the proposal to downcase letters. The proposal is a string to which we send the length message. The string processes the message and produces a value equal to the number of characters it contains. This is also known as the length of the string.

Afterward, Ruby sees the > mathematical sign, which means that the value to the left of > is greater than the value of what follows >, which in this case is 1. If the length of the string is greater than 1, the execution path of the code is line 12 (that outputs a message saying that we must give only one letter). Otherwise, the program skips line 12.

Ruby meets a new word, elsif, in line 13. This word is built by combining else and if. It means that we specify another choice of what to do next, depending on the result of hidden_word.include?(answer). Follow the arrows in the diagram below to see the possible flows:

The expression after the elsif word looks like the earlier expression that checks whether the hidden word contains the proposed letter without the carriage return character that we just removed.

hidden_word = "hello"

puts "_ _ _ _ _"
puts

print "Give me a letter: "

answer = gets
answer = answer.chomp.downcase

if answer.length > 1
    puts "You must give only one letter!"
elsif hidden_word.include?(answer)
    puts "The letter is part of the hidden word"
else
    puts "Invalid letter" 
end
Handling multiple letters

Our code will not allow the input to contain more than one letter, and we will see the following output:

root@educative:/usercode# ruby hangman.rb
_ _ _ _ _

Give me a letter: he
You must give only one letter!

We got what we wanted.

Both annoying cases are solved. Our game has improved.

Quiz

Test yourself with a short quiz.

1.

In the following snippet, what is the output?

letter = "A".downcase

if letter == "A"
   puts "Letter: A"
elsif letter == "M"
   puts "Letter: M"
elsif letter == "a"
   puts "Letter: a"
else
   puts "Letter: unknown"
end
A.

THe output is Letter: unknown.

B.

The output is Letter: A.

C.

The output is Letter: a.


1 / 3