资本化书名的第一个字

时间:2014-05-20 06:10:52

标签: ruby rspec

必须进行以下测试:

require 'book'

describe Book do

  before do
    @book = Book.new
  end

  describe 'title' do
    it 'should capitalize the first letter' do
      @book.title = "inferno"
      @book.title.should == "Inferno"
    end

我不确定我该做什么。我有以下代码:

class Book
@book = Book.new
def title
    x = @book
    y = x.split("")
    y[0] = y[0].upcase
    z = y.join("")
    z
end

end

但它不会通过,我该怎么办?

1 个答案:

答案 0 :(得分:1)

编写自定义Book#title=Book#title方法以通过RSpec测试。

class Book
  def title=(t)
    @title = t.capitalize
  end

  def title
    @title
  end
end