Wait for a Proposal and Check It

Learn to use the if-else statement to compare strings.

In the previous lesson, the code ended with gets. As a result, the code waits for some input from the user. Now, we will add lines to the code to use the input and check if the given letter is part of the hidden word.

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

A brief recap

The code is the same as discussed in the previous lesson. In line 6, We instruct Ruby to display the prompt message for the user.

Storing the input

Here we find something similar to what we already saw earlier:

  • The word answer
  • The equal sign =
  • The word gets

Previously, the code effect was to attach a name to a string. Here we give the name answer to the result produced by gets.

What does this mean?

Since we’re using and referring to these elements repeatedly, we’ve named them for easier recall (as we would do with people).

When a language interpreter picks a word, it tries to do something. What it does depends on the programming language. In Ruby, it proceeds as follows:

  • When the word is on the left of an equal sign, it decides to use it as a name given to what is on the right side of the equal sign.

  • When the word is isolated (as gets in the code above), it does the following:

    • It searches if the word is a name that was given to something and replaces the name by the value.
    • It checks if it knows the name as something that’s able to perform some action, like gets that waits for the user’s input. If it does the language interpreter performs the action, which usually produces a result. Again, it replaces the name with the produced value.

Conditional statements

The following elements in line 10 are new:

  • The word if, a special word in Ruby
  • The word hidden_word
  • A dot .
  • The word include?
  • A left parenthesis (
  • The word answer
  • A dot .
  • The word chomp
  • A right parenthesis )

Take a breath. It gets complicated.

The if word marks a condition, the result of which appears after it is used to make a decision. If the result is correct (or true), Ruby uses the lines following the if statement until it reaches the else word in line 12. If the result is wrong (or false), Ruby skips the lines until the else statement and considers everything between else and end in line 14.

In other words, if marks the beginning of the choice between two branches. The branch to follow depends on the truthness of a condition resulting from everything after if. The diagram hereafter shows the two branches with the arrows departing from the if box.

So, the first thing that appears after if is the word hidden_word. Ruby finds that the word is the name given to a string associated above and uses it.

A dot (.) follows the name. The dot means, “Send a message to the string.” The word after the dot, include?, is the name of the message. Everything after the message is attached to the message.

Before going further, let us talk about the sending of the message. Say hidden_word is someone’s name. We send a message named include? to that person. When they receive the message, they will respond in some way to the message unless they do not understand the meaning of the message. The same is true with the string in our example. If the string understands the include? message, it will perform some action. Otherwise, the system is going to fail. Ruby will complain and stop executing the code. Let us try to visualize all this with a diagram showing the following:

  • The messages and the receivers in specific colors
  • The arrows symbolizing the sending
  • That Ruby spots the message using the dot by using the same colors

Afterward, Ruby meets the parentheses. These are optional but they make it clear that everything inside the parentheses is part of the content we want to attach to the message.

Before sending the include? message, Ruby must evaluate the attachment. The attachment is made of the word answer. The name was given to the user’s input (remember the string produced by gets). As a dot (.) follows answer, the attachment is not the answer. Ruby must first send the chomp message (the word right after the dot). This time, the message has no attachment.

Does a string understand chomp? Yes.

When a string receives chomp, it removes trailing characters, provided they are the enter characters (Carriage Return), and produces a resulting string.

Having sent chomp, Ruby has the attachment to the include? message (as shown in the following diagram).

The hidden-word string checks if the character sequence it receives as an attachment appears in itself on receiving the message. It produces a positive or negative result consumed by if (see the diagram). Based on the response, Ruby moves to either line 11 or line 13.

The two possible choices lead to very similar lines. They both show a message.

Test the code

Press the “Run” button to execute the code.

hidden_word = "hello"

puts "_ _ _ _ _"
puts

print "Give me a letter: "

answer = gets 

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

First, input the letter h. Since it belongs to the hidden word, the code will accept the input and show the following message:

_ _ _ _ _

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

Now, let’s try the input with the uppercase letter H. This time, the input will not be accepted and we will see the following message:

_ _ _ _ _

Give me a letter: H
Invalid letter

Oops! It says the letter is not part of the hidden word. It’s strict indeed. Did you notice that all the letters in the hidden word are lowercase? Another interesting case to test is typing two letters, like he instead of just one. This will also be accepted as both of these letters are present in the hidden_word and will give the following output:

_ _ _ _ _

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

It passed because the job of include? is to check whether matching sequences exist—and in this case, they do since he occurs at the beginning of the hidden_word. Of course, typing two letters that are not a subsequence of the hidden_word like ha results in the message Invalid letter being returned.

_ _ _ _ _

Give me a letter: ha
Invalid letter

Quiz

Test your concepts with a short quiz.

1

In the following snippet, what is the output?

if 1 == 1
   puts "One"
else
   puts "1"
end
A)

The output is One.

B)

The output is 1.

C)

The output is empty.

Question 1 of 40 attempted