Skip to content

@kgrz /pig_latin.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.