class PigLatin::Translator
Constants
- ENDING
how all the words end
Public Class Methods
new(options = {:dash => true})
click to toggle source
# File lib/piggy_latin.rb, line 7 def initialize (options = {:dash => true}) @dash = options[:dash] end
Public Instance Methods
translate(phrase)
click to toggle source
# File lib/piggy_latin.rb, line 11 def translate (phrase) sentences = phrase.split(".") translated_sentences = sentences.map do |sentence| translation(sentence) + "." end translated_sentences.join(" ") end
Private Instance Methods
split(word)
click to toggle source
# File lib/piggy_latin.rb, line 52 def split(word) split_location = word =~ /a|e|i|o|u/ second_segment = word[0,split_location] first_segment = word[split_location,word.size] return second_segment, first_segment end
translate_with_consonant(word)
click to toggle source
# File lib/piggy_latin.rb, line 46 def translate_with_consonant(word) return "#{word[1]}-#{word[0]+ENDING}" if word.size == 2 second_segment, first_segment = split(word) return "#{first_segment}-#{second_segment+ENDING}" end
translate_with_vowel(word)
click to toggle source
# File lib/piggy_latin.rb, line 42 def translate_with_vowel(word) "#{word}-#{"w"+ENDING}" end
translation(phrase)
click to toggle source
# File lib/piggy_latin.rb, line 21 def translation (phrase) words = phrase.split translated_words = words.map do |word| if vowel_is_first(word) translate_with_vowel(word) else translate_with_consonant(word) end end translated_phrase = translated_words.join(" ") if @dash == false translated_phrase = translated_phrase.delete("-") end translated_phrase.capitalize end
vowel_is_first(word)
click to toggle source
# File lib/piggy_latin.rb, line 37 def vowel_is_first(word) return true if word[0] =~ /a|e|i|o|u|A|E|I|O|U/ return false end