Here is my work for Exercise 11.
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.
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.
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.
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.
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”!
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.
b) Or you can suggest a new lucky number to the user:
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!
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.