Я работаю над основами Ruby в Odin Projects и полностью застрял на 05_book_titles. Заголовок должен быть написан заглавными буквами, включая 1-е слово, но не включая «маленькие слова» (то есть «до», «и т. Д.»), ЕСЛИ это не 1-е слово. Я не могу заставить код делать что-либо кроме того, чтобы извлечь выгоду из всего. Я неправильно использую метод карты? Как я могу включить слова no_cap в возвращаемый заголовок без заглавных букв?
Файл Ruby:
class Book
def title
@title
end
def title=(title)
no_cap = ["if", "or", "in", "a", "and", "the", "of", "to"]
p new_title = @title.split(" ")
p new_new_title = new_title.map{|i| i.capitalize if !no_cap.include? i}
.join(" ")
end
end
Некоторые файлы Spec:
require 'book'
describe Book do
before do
@book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
@book.title = "inferno"
expect(@book.title).to eq("Inferno")
end
it 'should capitalize every word' do
@book.title = "stuart little"
expect(@book.title).to eq("Stuart Little")
end
describe 'should capitalize every word except...' do
describe 'articles' do
specify 'the' do
@book.title = "alexander the great"
expect(@book.title).to eq("Alexander the Great")
end
specify 'a' do
@book.title = "to kill a mockingbird"
expect(@book.title).to eq("To Kill a Mockingbird")
end
specify 'an' do
@book.title = "to eat an apple a day"
expect(@book.title).to eq("To Eat an Apple a Day")
end
end
specify 'conjunctions' do
@book.title = "war and peace"
expect(@book.title).to eq("War and Peace")
end
end
end
end
Результат:
Book
title
should capitalize the first letter (FAILED - 1)
Failures:
1) Book title should capitalize the first letter
Failure/Error: @book.title = "inferno"
NoMethodError:
undefined method `split' for nil:NilClass
# ./05_book_titles/book.rb:8:in `title='
# ./05_book_titles/book_titles_spec.rb:25:in `block (3 levels) in <top (required)>'
Finished in 0.0015 seconds (files took 0.28653 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./05_book_titles/book_titles_spec.rb:24 # Book title should capitalize the first letter