Learn Ruby the Hard Way – Exercise 11 Asking Questions

Here is my work for Exercise 11.

Exercise 11 code
Exercise 11 code

Screen Shot 2015-09-15 at 4.54.12 PM
There’s my code output! And yes, I made up random numbers. I am very tall, very old, and… not too heavy? What is a healthy height for a 6’foot tall person who is 95 anyways?

Study Drills
1. Go online and find out what Ruby’s gets.chomp does.
gets is a method (like how puts is a method that writes to the screen) that accepts a single line of data from the standard input and returns a string. In our case, the standard input is the keyboard. We can use gets to accept user input. The gets method attaches a n to the end of the input and returns a string. (A new line character). We can see this from the Ruby code below.

Example to show the gets method.
Example to show the gets method.

We set a variable name and use the method gets to accept user input. Ruby assigns the user input (in this case, I typed “Stacey”) and assigns the string “Stacey\n” to the variable of name. This may not be obvious when we use the puts method to print the variable value. However, if we add name to a string “here.”, and print that, we see that the two words are split by a line break.

What if we don’t want the new line character? This is where chomp comes in.

chomp is a method for Strings. The chomp method returns a new string, with the given record separator removed from the end of the string. If the given record separator is not present, the method removes carriage return characters. (\r, \n, \rn)

Let’s look the carriage return characters first.

chomp in action!
chomp in action!

Notice how “Hello\n\r”.chomp returns “Hello\n” ? This is because the chomp method removed the trailing “\r” and returned the new string “Hello\n”.

Let’s look at what happens with a given record separator. If we enter “lo” as the record separator, we see that it gets removed from the end of the string, leaving us with “Hel” after “lo” is removed. Note that carriage return characters not at the end of the string will not be removed. “Hello \n there” remains the same after we use the chomp method.

Screen Shot 2015-09-15 at 5.22.31 PM
In the code above, we can also see how to remove all trailing new lines from a string. When we enter an empty string (enclosed by single quotation marks here), the method removes all the trailing new lines from “Hello\r\n\r\n”. (It removes the two ‘\r\n’). But how come it doesn’t work for the last example?

I was stuck here for a bit because I assumed it would remove all carriage return characters (\r, \n, \r\n). So how come only ‘\r\n’ was removed? By looking at the other examples in the documentation, I realized I had misread! If $/ is an empty string, chomp will remove all trailing new lines. Since \r moves the cursor to the beginning of the current line, it does not create a new line. Only \n and \rn are characters for new lines.

You can see the three in action in the Ruby code and output below. Notice how \r moves the cursor backs to the beginning of the string and rewrites Hello into Goodbye.

Comparing r, n, rn
Comparing \r, \n, \rn

Here’s another example. I purposely chose a longer word to begin with to show how \r works. First “Four” is typed, then the cursor goes back to the beginning of the line and writes “Two”. Since Four has one more character than Two… we end up with “Twor”!

Screen Shot 2015-09-15 at 10.35.40 PM
\r in action

So when we use chomp on “Hello\r\n\r\r\n”, it removes the new line character ‘\r\n’ at the very end, then it encounters ‘\r’ which is not a new line character. The method stops here and returns the string. This is why we get “Hello\r\n\r”! Mystery solved!

Lesson: Read the documentation carefully even when you think you understand! 

Bringing it back full circle to our exercise code, we use age = gets.chomp to ask the user for age input and then use the chomp method on the string to remove the trailing n new line character. We end up with a string assigned to the age variable, free of the new line. As we can see from the output of line 8, the final statement is all in one line, with no line breaks.

2. Can you find other ways to use it? Try some of the samples you find.
a) You can also use gets.chomp to display a greeting to the user.

Welcome the user to a language!
Welcome the user to a language!
Output of the greeting example.
Output of the greeting example.

b) Or you can suggest a new lucky number to the user:

Asking the user for their lucky number
Asking the user for their lucky number

Here, we have to use the .to_i method to change the string into an integer, so that we can do math with it. We add 1 to the number, then use string interpolation to suggest the bigger number to the user. To make the code more concise, we can change line 2 to number = gets.chomp.to_i + 1!

Lucky 7!
Lucky 7!

3. Write another “form” like this to ask some other questions.
Here is a form to ask the user’s name, birth year, and favourite number. It calculates the person’s age in 2015 and what double their favourite number is. 

My form
My form
My form with 3 user inputs.
My form with 3 user inputs.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s