下面的代码检查文件'08_book_titles'中是否已反转下面代码中包含的类Book和方法标题中的字符串。那个字符串是“地狱”。
require '08_book_titles'
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
def title
return yield.capitalize
end
end
答案 0 :(得分:2)
如果您想存储大写形式,那么
class Book
attr_accessor :title
def title=( title )
@title = title.capitalize
end
end
如果您想保留原始表单
class Book
attr_accessor :title
def title
@title.capitalize
end
end