Skip to content

Instantly share code, notes, and snippets.

Created December 15, 2012 01:25
Show Gist options
  • Save anonymous/4290318 to your computer and use it in GitHub Desktop.
Save anonymous/4290318 to your computer and use it in GitHub Desktop.
def translate(word)
word_list = word.split(/ /)
letters = ('a'..'z').to_a
vowels = ['a', 'e', 'i', 'o', 'u']
consonants = letters - vowels
result = ""
word_list.each do |word|
if vowels.include?(word[0]) #translate word beginning with vowels.
result << word + "ay"
elsif consonants.include?(word[0])
if word.include?("qu") #word contains 'qu'.
if word[0..1].include?("qu") #word begins with 'qu'.
result << word[2..-1] + word[0..1] + "ay"
else #'qu' is somewhere else in the word.
position = word.index("qu")
result << word[(position + 2)..-1] + word[0..(position-1)] + "quay"
end
elsif consonants.include?(word[1])
if consonants.include?(word[2]) #word begins with 3 consonants.
result << word[3..-1] + word[0..2] + "ay"
else #word begins with 2 consonants.
result << word[2..-1] + word[0..1] + "ay"
end
else #word begins with 1 consonant.
result << word[1..-1] + word[0] + "ay"
end
else
result << word #word doesn't begin with a vowel or consonant.
end
result << " " unless word == word_list.last #add space after words except the last one.
end
return result
end
puts translate("square")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment