Exercise 8. Printing, Printing

Image

I will now show you how to create a format string, but rather than using variables, we’ll 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.

ex8.rb

 1    formatter = "%{first} %{second} %{third} %{fourth}"  2  3    puts formatter % {first: 1, second: 2, third: 3, fourth: 4}  4    puts formatter % {first: "one", second: "two", third: "three",  5                      fourth: "four"}  6    puts formatter % {first: true, ...

Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition, Video Enhanced Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.