Skip to content

Instantly share code, notes, and snippets.

@sjwats
Last active December 30, 2015 04:19
Show Gist options
  • Select an option

  • Save sjwats/7775466 to your computer and use it in GitHub Desktop.

Select an option

Save sjwats/7775466 to your computer and use it in GitHub Desktop.
OOPII - Pig Latin
class PigLatinTranslation
def initialize(phrase)
@phrase = phrase
@vowels = ["a", "e", "i", "o", "u"]
@translation_array = []
end
def translate
words_array = words
index = 0
words_array.each do |word|
if starts_with_vowel?(word)
@translation_array << (word + "way")
else
while !@vowels.include?(word[index])
index += 1
end
@translation_array << (word[index..-1] + word[0...index] + "ay")
end
end
p "#{@translation_array.join(" ")}"
end
private
def words
@phrase.split.map
end
def starts_with_vowel?(word)
@vowels.include?(word[0])
end
end
pig_translation = PigLatinTranslation.new("ooze")
pig_translation.translate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment