我觉得我真的很接近这个,但我不知道为什么.join
不工作。
这是我写的代码:
class String
def title_case
title = self.split
title.each do |word|
unless (word.include?("of")) || (word.include?("the")) && (title.first != "the")
word.capitalize!
end
title.join(" ")
end
end
end
这是 RSPEC:
describe "String" do
describe "Title case" do
it "capitalizes the first letter of each word" do
"the great gatsby".title_case.should eq("The Great Gatsby")
end
it "works for words with mixed cases" do
"liTTle reD Riding hOOD".title_case.should eq("Little Red Riding Hood")
end
it "ignores articles" do
"The lord of the rings".title_case.should eq("The Lord of the Rings")
end
end
end