def titleize(x) capitalized = x.split.each do |i| if i.length >= 2 if i == "of" || "the" || "and" next else i.capitalize! end else next end end capitalized.join(' ') end
以下是我的Rspec输出:
1) Simon says titleize capitalizes a word Failure/Error: expect(titleize("jaws")).to eq("Jaws") expected: "Jaws" got: "jaws" (compared using ==)
你有一个 string literal in condition 警告:
string literal in condition
if i == "of" || "the" || "and"
你在试着比较 i of 或 the and
i
of
the
and
if i == "of" || i == "the" || i == "and"
更惯用的Ruby应该是使用 include?
include?
if ['of', 'the', 'and'].include?(i)
这样至少你可以 Jaws
Jaws
war and peace 因为如果传递的字的长度小于或等于2,则它将执行 next peace .
war and peace
next
peace