所以我正在從Chris Pine的在線教程中學習,並且我堅持使用這個程序。我在做什麼,我覺得只是教程中教導的內容?Ruby Nubie - 程序出了什麼問題?
這是程序
toc = [[1, "Reflections"], [2, "Glasgow Roots"], [3, "Retirement U-turn"], [4, "A Fresh Start"], [5, "Beckham"]]
title = "The table of contents of Sir Alex Ferguson' Biography"
toc.each do |x, y|
lineWidth = 15
puts title.center lineWidth
puts x.ljust(lineWidth/2) + y.rjust(lineWidth/2)
end
錯誤
toc.rb:8:in `block in <main>': undefined method `ljust' for 1:Fixnum (NoMethodError)
from toc.rb:5:in `each'
from toc.rb:5:in `<main>'
請幫助。
你注意到代碼劃分爲奇數(15)由2,這意味着你的字段將是7個字符寬,你的線將是14個字符,而不是你可能預期的15個字符? – dcorking 2014-08-31 13:39:04