Skip to content

Instantly share code, notes, and snippets.

@colemanfoley
Created September 19, 2012 03:48
Show Gist options
  • Save colemanfoley/3747574 to your computer and use it in GitHub Desktop.
Save colemanfoley/3747574 to your computer and use it in GitHub Desktop.
Pig Latin translator
#The main method, it takes a string that is an English word and returns that word in Pig Latin.
#All words have "ay" appended to them in the line just before the return statement. If the word
#begins with a vowel, it is sent to the << "ay" line unmodified.
#If it is exactly three letters long, the second two characters of english_word are made the first
#two of pig_latin_word, then the first letter of english_word is made the third character of
#pig_latin_word. Then the word is ready to get the << "ay" treatment.
def translate(english_word)
if is_vowel?(english_word[0])
pig_latin_word = english_word
elsif english_word.length == 3
pig_latin_word = ""
pig_latin_word[0,1] = english_word[1,2]
pig_latin_word[2] = english_word[0]
elsif (is_vowel?(english_word[0])==false) && (is_vowel?(english_word[1]) == false)
if is_vowel?(english_word[2])
pig_latin_word = english_word[2..-1]
pig_latin_word << english_word[0..1]
else
pig_latin_word = english_word[3..-1]
pig_latin_word << english_word[0..2]
end
elsif (is_vowel?(english_word[0])==false) && (is_vowel?(english_word[1])==true)
pig_latin_word = english_word[2..-1]
pig_latin_word << english_word[0..1]
end
pig_latin_word << "ay"
return pig_latin_word
end
#Helper method for translate that determines if a character is a vowel or not, returning a boolean
def is_vowel?(string)
string = string.downcase
if (string.include?("a")) || (string.include("e")) || (string.include("i")) || (string.include("o")) || (string.include("u"))
return true
else
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment