Exercise 14: Prompting and Passing

Let's do one exercise that uses ARGV and gets.chomp together to ask the user something specific. You will need this for the next exercise where we learn to read and write files. In this exercise we'll use gets.chomp slightly differently by having it print a simple > prompt. This is similar to a game like Zork or Adventure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
user_name = ARGV.first # gets the first argument
prompt = '> '

puts "Hi #{user_name}."
puts "I'd like to ask you a few questions."
puts "Do you like me #{user_name}? "
puts prompt
likes = $stdin.gets.chomp

puts "Where do you live #{user_name}? "
puts prompt
lives = $stdin.gets.chomp

# a comma for puts is like using it twice
puts "What kind of computer do you have? ", prompt
computer = $stdin.gets.chomp

puts """
Alright, so you said #{likes} about liking me.
You live in #{lives}.  Not sure where that is.
And you have a #{computer} computer.  Nice.
"""

We make a variable prompt that is set to the prompt we want, and we give that to gets.chomp instead of typing it over and over. Now if we want to make the prompt something else, we just change it in this one spot and rerun the script. Very handy.

You will also notice that we used ARGV.first in this script to get the first command line argument. In the previous script I used first, second, third = ARGV to get three arguments, but that won't work for just one argument. The explanation as to why is complex at this point in your learning, so just remember that you use ARGV.first to get only one argument, and use the other form when you want more than one command line argument. Later in the book you'll understand why when you learn about Arrays.

What You Should See

When you run this, remember that you have to give the script your name for the ARGV arguments.

$ ruby ex14.rb zed
Hi zed.
I'd like to ask you a few questions.
Do you like me zed?
>
Yes
Where do you live zed?
>
San Francisco
What kind of computer do you have?
>
Tandy

Alright, so you said Yes about liking me.
You live in San Francisco.  Not sure where that is.
And you have a Tandy computer.  Nice.

Study Drills

  1. Find out what Zork and Adventure were. Try to find a copy and play it.
  2. Change the prompt variable to something else entirely.
  3. Add another argument and use it in your script, the same way you did in the previous exercise with first, second = ARGV.
  4. Make sure you understand how I combined a """ style multiline string with the #{} format activator as the last print.

Common Student Questions

I don't understand what you mean by changing the prompt?
See the variable prompt = '> '. Change that to have a different value. You know this, it's just a string and you've done 13 exercises making them, so take the time to figure it out.
Can I use double-quotes for the prompt variable?
You totally can. Go ahead and try that.
You have a Tandy computer?
I did when I was little.
I get undefined local variable or method `prompt' when I run it.
You either spelled the name of the prompt variable wrong or forgot that line. Go back and compare each line of code to mine, from at the bottom of the script to the top. Any time you see this error, it means you spelled something wrong, or forgot to create the variable.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download and use as you see fit.

$29.99

Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.