Created
December 11, 2015 05:28
-
-
Save mkweick/68acb400fe2195e2731e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class PigLatin | |
def self.translate(phrase) | |
pig = PigLatin.new(phrase).convert | |
end | |
attr_reader :words | |
def initialize(phrase) | |
@words = phrase.split | |
end | |
def convert | |
words.map do |word| | |
if vowel_sound?(word) | |
word + 'ay' | |
else | |
consonant_balance(word) + consonant(word) + 'ay' | |
end | |
end.join(' ') | |
end | |
def vowel_sound?(word) | |
true if vowel?(word[0]) | |
case word[0] + word[1] | |
when 'yt' then true | |
when 'xr' then true | |
end | |
end | |
def consonant_balance(word) | |
word[consonant_end(word)..-1] | |
end | |
def consonant(word) | |
word[0...consonant_end(word)] | |
end | |
def consonant_end(word) | |
word.split('').each_with_index do |letter, index| | |
return index if vowel?(letter) | |
return index + 2 if letter == 'q' | |
end | |
end | |
def vowel?(letter) | |
%w(a e i o u).any? { |vowel| letter == vowel } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment