我目前正在解决一组 TestFirst 问题。我正在处理的问题的规范可以在这里找到:http: //testfirst.org/live/learn_ruby/book_titles
我已经在类之外测试了 title 方法,所以我知道它可以工作,但是一旦我将它放在类中,我就会收到以下错误:
1) Book title should capitalize the first letter
Failure/Error: @book.title.should == "Inferno"
ArgumentError:
wrong number of arguments (0 for 1)
这是我到目前为止所拥有的:
class Book
attr_accessor :title
def initialize(title=nil)
@title = title
end
def title(title)
first_check = 0
articles = %w{a the an in and of}
words = title.split(" ")
words.each do |word|
if first_check == 0
word.capitalize!
first_check += 1
elsif !articles.include?(word)
word.capitalize!
end
end
@title = words.join(" ")
end
end
如果有人可以解释该类的格式应该如何,将不胜感激!