#####Exercise 8: Printing, Printing
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
formatter = "%{first} %{second} %{third} %{fourth}"
puts formatter % {first: 1, second: 2, third: 3, fourth: 4}
puts formatter % {first: "one", second: "two", third: "three", fourth: "four"}
puts formatter % {first: true, second: false, third: true, fourth: false}
puts formatter % {first: formatter, second: formatter, third: formatter, fourth: formatter}
puts formatter % {
first: "I had this thing.",
second: "That you could type up right.",
third: "But it didn't sing.",
fourth: "So I said goodnight."
}
|
#####Exercise 9: Printing, Printing, Printing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "JannFebnMarnAprnMaynJunnJulnAug"
puts "Here are the days: #{days}"
puts "Here are the months: #{months}"
puts """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
|
Use three double-quotes, we’ll be able to type as much as we like.
近期评论