Created
January 8, 2014 22:32
-
-
Save cnocon/8325716 to your computer and use it in GitHub Desktop.
pig latin
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
def pig_latin(word) | |
if !!(word =~ /\s/) | |
split_sentence = word.split(' ').map {|w| w.split(//)} | |
translate_sentence(split_sentence) | |
else | |
split_word = word.split(//) | |
translate_single_word(split_word) | |
end | |
end | |
def translate_single_word(split_word) | |
if !!(split_word[0] =~ /[aeiou]/i) | |
pig_word = split_word << 'ay' | |
else | |
consonants = split_word[(0...(split_word.index { |char| !!(char =~ /[aeiou]/i) }))] | |
pig_word = split_word - consonants << consonants.flatten.to_s << 'ay' | |
end | |
end | |
def translate_sentence(split_sentence) | |
split_sentence.map! do |word| | |
if !!(word[0] =~ /[aeiou]/i) | |
pig_word = split_sentence << 'ay' | |
else | |
#puts "word: #{word}" | |
b = word.index { |char| char =~ /[aeiou]i/ } | |
#puts word[0] | |
puts "b: #{b}" | |
consonants = word[(0...(b))] | |
pig_word = word - consonants << consonants.flatten.to_s << 'ay' | |
end | |
end | |
split_sentence.join | |
end | |
p pig_latin('apple') | |
p pig_latin('chomp') | |
p pig_latin('dogs and apples') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment