Skip to content

Instantly share code, notes, and snippets.

@toddnestor
Last active July 28, 2016 18:57
Show Gist options
  • Save toddnestor/3139b33afd6de2af71cc4998046966a5 to your computer and use it in GitHub Desktop.
Save toddnestor/3139b33afd6de2af71cc4998046966a5 to your computer and use it in GitHub Desktop.
def pig_latinize_word( word )
parts = word.split(/\b([^a,e,i,o,u]{0,}qu|[^a,e,i,o,u]+)?([a,e,i,o,u][a-z]{0,})?/i) #https://regex101.com/r/yP8yF9/3 <-- this is where I worked out the regex
parts.shift if parts.first.empty? #getting rid of that first empty element that results from the regex matching the entire word, adding the conditional just in case there are outliers where the first word is not blank that I haven't thought of
parts << parts.shift #moving the first element to be the last element
"#{parts.join('')}ay"
end
def translate( words )
( words.split(' ').inject([]) {|pig_latinized_words,word| pig_latinized_words << pig_latinize_word( word )} ).join(' ') #did it all in one line because I was curious if Ruby would let me, it just splits the words up by spaces, makes a new array of the pig latinized words, and then joins them back with spaces
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment