I’m working on creating a pig latin translator in ruby. It works for most words, but I’m having some trouble getting it to work with more than one word at a time. So for example when you just put in the word “apple” you get “appleay” but if more than one word is inputted it does not translate them. I’ve been searching around for a solution but have come up empty. A few other threads on here have been really helpful in getting me this far. Any tips would be greatly appreciated.
I’ve also added in a couple of exceptions in the if/else statement to allow for the proper pig latin translation of “quiet” and “square”, where “qu” is considered a consonant.
Thanks in advance for any help guys!
def translate (word)
alpha = ('a'..'z').to_a
vowels = %w[a e i o u]
consonants = alpha - vowels
if vowels.include?(word[0..0])
word + 'ay'
elsif consonants.include?(word[0..0]) && consonants.include?(word[1..1])
word[2..-1] + word[0..1] + 'ay'
elsif word[0..1] == "qu"
word[2..word.length]+"quay"
elsif word[0..2] == "squ"
word[3..word.length]+"squay"
else consonants.include?(word[0])
word[1..-1] + word[0..0] + 'ay'
end
end
You could do it this way: