【发布时间】:2017-07-27 00:57:20
【问题描述】:
下面的代码是将除“littleWords”和标题的第一个单词之外的所有单词大写。 (即使属于 littleWords,第一个单词也要大写。)
def titleize (word)
littleWords = ["and", "the", "over", "or"]
words = Array.new
words = word.split(" ")
titleWords = Array.new
words.each {|word, index|
if index == 0
word = word.capitalize
else
unless littleWords.include?(word)
word = word.capitalize
end
end
titleWords << word
}
return titleWords.join(" ")
end
测试代码如下。
it "does capitalize 'little words' at the start of a title" do
expect(titleize("the bridge over the river chao praya")).to eq("The Bridge over the River chao praya")
end
但它始终将第一个“the”大写为“the”而不是“The”。我想知道我的代码的哪一部分是错误的。帮帮我……TT
【问题讨论】:
标签: ruby capitalize