Skip to content

Instantly share code, notes, and snippets.

@kgrz
Created April 22, 2013 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgrz/5432485 to your computer and use it in GitHub Desktop.
Save kgrz/5432485 to your computer and use it in GitHub Desktop.
English to Pig Latin converter ™
# Based on the definition at http://en.wikipedia.org/wiki/Pig_Latin
# For http://stackoverflow.com/questions/16138526/unexpected-end-kend-ruby-piglatin-program?noredirect=1#comment23055830_16138526
words = %w{happy duck love egg inbox eight}
def convert_to_pig_latin(word)
# If word starts with a vowel
if word =~ /^[aeiou]/
"#{word}way"
else
# The word starts with a consonant. Now to find out
# how many from the starting point are consonants.
#
# String#match#length returns the first occurrence of
# the match.
first_instance = word.match(/[aeiou]/).length
# All this is a potential one-liner. Elaborating it for
# the sake of it.
consonant_group = word.slice(0...first_instance)
word_without_consonant_group = word.slice(first_instance..-1)
new_word = "#{word_without_consonant_group}#{consonant_group}"
return "#{new_word}ay"
end
end
pig_latinized = words.map{ |x| convert_to_pig_latin(x) }
puts pig_latinized === %w{appyhay uckday ovelay eggway inboxway eightway}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment