发出正确格式化的ruby以通过rspec

发出正确格式化的ruby以通过rspec,ruby,rspec,Ruby,Rspec,我目前正在处理一组TestFirst问题。我正在处理的问题的规范可以在这里找到: 我已经在类之外测试了title方法,所以我知道它是有效的,但一旦我将其放入类中,我会得到以下错误: 1) Book title should capitalize the first letter Failure/Error: @book.title.should == "Inferno" ArgumentError: wrong number of arguments (0 for

我目前正在处理一组TestFirst问题。我正在处理的问题的规范可以在这里找到:

我已经在类之外测试了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

如果有人能解释这个类应该如何格式化,我们将不胜感激

您的问题就在这里:

def title(title)
您的
Book#title
方法需要一个参数,但您的规范中没有提供参数:

@book.title.should == "Inferno"
# ----^^^^^ this method call needs an argument
我想你实际上想要一本
书#title=
方法:

def title=(title)
  # The same method body as you already have
end
然后使用
attr\u accessor:title
提供的
title
访问器方法,分配新标题将使用
title=
方法。由于您提供了自己的mutator方法,您可以使用:


那肯定管用。。。现在我明白了setter方法是什么了。非常感谢。
class Book
  attr_reader :title

  def initialize(title=nil)
    @title = title
  end

  def title=(title)
    #...
  end
end