Exercise 8: Printing, Printing
I will now show you how to create a format string, but rather than using variables, use values by their names. Some of this is a bit more advanced, but don't worry you'll learn what all of these mean later. Just type this in, make it work, and write a comment above each line translating it to English.
What You Should See
$ ruby ex8.rb
1 2 3 4
one two three four
true false true false
%{first} %{second} %{third} %{fourth} %{first} %{second} %{third} %{fourth} %{first} %{second} %{third} %{fourth} %{first} %{second} %{third} %{fourth}
I had this thing. That you could type up right. But it didn't sing. So I said goodnight.
Study this carefully and try to see how I put the formatter inside the formatter.
Study Drills
Repeat the Study Drill from Exercise 7.
Common Student Questions
- Should I use %{} or #{} for formatting?
- You will almost always use #{} to format your strings, but there are times when you want to apply the same format to multiple values. That's when %{} comes in handy.
- Why do I have to put quotes around "one" but not around true or false?
- Ruby recognizes true and false as keywords representing the concept of true and false. If you put quotes around them then they are turned into strings and won't work. You'll learn more about how these work in Exercise 27.