So ive been trying to write the pig latin method with its different instances. However, the join method at the end simply joins the original words of the string as opposed to the modified words that have gone through the iteration:
def translate(string)
alphabet = ("a".."z").to_a
vowels = %w{a e i o u}
consonant = alphabet - vowels
words = string.split
words.each do |word|
if vowels.include?(word[0])
word = word + "ay"
elsif word[0..2] == "sch"
word = word[3..-1] + word[0..2] + "ay"
elsif word[0..1] == "qu"
word = word[2..-1] + word[0..1] + "ay"
elsif word[1..2] == "qu"
word = word[3..-1] + word[0..2] + "ay"
elsif consonant.include?(word[0]) && consonant.include?(word[1]) && consonant.include?(word[2])
word = "#{word[3..-1]}#{word[0..2]}ay"
elsif consonant.include?(word[0]) && consonant.include?(word[1])
word = "#{word[2..-1]}#{word[0..1]}ay"
elsif consonant.include?(word[0])
word = "#{word[1..-1]}#{word[0]}ay"
end
end
p words.join(" ")
end
translate("apple pie")
translate("cherry")
translate("school")
translate("square")
translate("three")
translate("apple")
translate("cat")
This is what it gives me when it runs:
"apple pie"
"cherry"
"school"
"square"
"three"
"apple"
"cat"