所以我正在从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