尝试编写一个方法,该方法将接受一个字符串并返回它的猪拉丁版本。单字版本工作正常,但是当我给它时"eat pies"
,它会返回"eat pies"
而不是"eatay iespay"
像它应该的那样。这是代码:
def translate(string)
x = string
if x.split.count > 1
splitskies = x.split
splitskies.each do |w|
if w[0].match(/[aeiou]/)
w = w + "ay"
elsif w[0] !~ (/[aeiou]/) && w[1] !~ (/[aeiou]/)
w = w[2..-1] + w[0..1] + "ay"
elsif w[0] !~ (/[aeiou]/)
w = w[1..-1] + w[0] + 'ay'
end
return splitskies.join(' ')
end
else
if x[0].match(/[aeiou]/)
x = x + "ay"
elsif x[0] !~ (/[aeiou]/) && x[1] !~ (/[aeiou]/)
x[2..-1] + x[0..1] + "ay"
elsif x[0] !~ (/[aeiou]/)
l = x.slice!(0)
x = x + l + "ay"
end
end
end
你能解释一下为什么会发生这种情况吗?