Created
April 22, 2013 04:45
-
-
Save kgrz/5432485 to your computer and use it in GitHub Desktop.
English to Pig Latin converter ™
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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