public
Created

English to Pig Latin converter ™

  • Download Gist
pig_latin.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# 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}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.