Hide a Word and Ask for a Letter

Learn how to get input, print data, create variables and assign values in Ruby.

We will start with a few lines of the code. Each line is an instruction. The tools we use to write the code highlight different code elements that relate to the language rules using colors.

hidden_word = "hello"
puts "_ _ _ _ _"
puts
print "Give me a letter: "
gets

Hide the word

Let’s take a detailed look at what the code above does. Line 1 contains three elements. These are as follows:

  • The word (named using two English words joined with an underscore, “_”): hidden_word.
  • The equal sign: =.
  • A sequence of letters, called a string, enclosed in double quotation marks: "hello".

What does Ruby understand?

  • The double quotes surrounding the string demarcate a sequence of characters ("hello") that has no meaning for Ruby even if it’s meaningful for us.
  • The equal sign (=) means that we want to give a name to the string so that we can later use the name to refer to that stringLikewise, when we need to talk about a person, we use the name they were given..
  • The name we give to the string is hidden_word.

What does line 1 mean to us?

We decide that the hidden word is hello, and we give it the name hidden_word. Let’s repeat the above explanation in another way using a diagram that shows:

  • The three types of elements that Ruby sees.
  • The function of the elements using different colors.
  • The naming of the string ("hello") with an arrow.
  • That naming something requires the equal sign by using the same color for the arrow and the equal sign, =.

Show something to the screen

Line 3 has two elements:

  • The word puts.
  • Another sequence of characters "_ _ _ _ _".

What does Ruby understand from Line 3?

  • It recognizes the puts word in the Ruby parlance. It means: Show something to the screen.
  • The string is what we want to show on the screen with the word puts.

We can illustrate it using again a diagram that shows the two elements identified by Ruby.

The effect is to show on the screen the "_ _ _ _ _" string that gives information to the player about the hidden word (that is, the number of letters).

Line 4 is very similar to the previous one. The difference is that we give nothing to puts, in which case the effect is to skip a line.

Using print instead of puts

Line 6 contains the following:

  • The word print
  • The string "Give me a letter:"

Here, instead of puts, we see print followed by a new string (the prompt for a letter). The effect of print is similar to puts except that, while puts shows something and moves to the following line, print shows something and stays at the end of that something.

Get the input

The next line contains only the word gets, which is a Ruby keyword. Its effect is to wait for the person in front of the screen to type letters on the keyboard and end with the ”Enter” key(carriage returnOld typewriters had a carriage, moving and pulling the paper because the characters hit the paper at a specific place. When the operator wanted to move back to the left of the page, they could press a button that returned the carriage to the leftmost position.). Ruby displays on the screen every character that the user types.

Line 8 is the last line of the code.

Let’s test the code

The source code is saved in the hangman.rb file. If we want the computer to read the file and apply what the file asks, we must make the relevant request using ruby hangman.rb. Press the “Run” button and see the output!

hidden_word = "hello"

puts "_ _ _ _ _"
puts

print "Give me a letter: "

gets
Print a message and get input

It gives the following output:

_ _ _ _ _

Give me a letter: h

The program ends here since there is nothing after line 8.

Quiz

Let’s test your knowledge with a short quiz.

1

In the following snippet, what is the output?

print "Hello,"
puts "John"
A)
Hello, Max
B)
Hello
John
C)
Hello, John
Question 1 of 40 attempted