How to save the output from `each` method
Trying to write a method that will take a string and return the pig-latin
version of it. The single-word version works fine, but when I give it "eat
pies", it returns "eat pies" and not "eatay iespay" like it should. Here's
the code:
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
Can you also explain why this might be happening?
No comments:
Post a Comment