Learn Ruby The Hard Way

笨方法學 Ruby

習題 8: 印出,印出

1
2
3
4
5
6
7
8
9
10
11
12
formatter = "%s %s %s %s"

puts formatter % [1, 2, 3, 4]
puts formatter % ["one", "two", "three", "four"]
puts formatter % [true, false, false, true]
puts formatter % [formatter, formatter, formatter, formatter]
puts formatter % [
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
]

你應該看到的結果

$ ruby ex8.rb
1 2 3 4
one two three four
true false false true
%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
I had this thing. That you could type up right. But it didn't sing. So I said goodnight.
$

加分習題

  1. 自己檢查結果,記錄你犯過的錯誤,並且在下個練習中儘量不犯相同的錯誤。