我正在进行simon_says练习。我需要让测试通过,但不能让最后一个通过。它应该将作为参数传递的所有单词的首字母大写为titleize方法。
我最初的本能是跳过4个字符以下的所有单词的大小写但这似乎不起作用,因为长度不是决定因素(大约3个字母的单词应该大写,有些不是,与4相同)字母的单词)。
这里有一个小词和一个非小词有什么区别?
这是我的错误消息:
1) Simon says titleize does capitalize 'little words' at the start of a title
Failure/Error: titleize("the bridge over the river kwai").should == "The Br
idge over the River Kwai"
expected: "The Bridge over the River Kwai"
got: "The Bridge Over The River Kwai" (using ==)
# ./03_simon_says/simon_says_spec.rb:92:in `block (3 levels) in <top (requi
red)>'
Finished in 0.01401 seconds
15 examples, 1 failure
到目前为止,这是我的代码:
def titleize(t)
q = t.split(" ")
u = []
if q.length == 1
p = t.split("")
p[0] = p[0].upcase
r = p.join("")
return r
else q.each do |i|
if i == "and"
u.push(i)
else
p = i.split("")
p[0] = p[0].upcase
r = p.join("")
u.push(r)
end
end
s = u.join(" ")
return s
end
end