Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Created December 5, 2013 20:46
Show Gist options
  • Save faizaanshamsi/7813577 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/7813577 to your computer and use it in GitHub Desktop.
Pig Latin Translator and Tests
class PigLatin
def initialize(phrase)
@phrase = phrase
end
#provide the pig latin translation
def translate
@words = words
@words.collect! do |word|
if starts_with_vowel?(word)
word += 'way'
else
#do the consonant translation
until starts_with_vowel?(word) do
word = "#{word[1..-1]}" + "#{word[0]}"
end
word += "ay"
end
end
@words.join(' ')
end
private
#an array of words in the phrase
def words
@phrase.split(' ')
end
def starts_with_vowel?(word)
if word =~ /\A[^aeiou]/
false
else
true
end
end
end
class PigLatin
def initialize(phrase)
@phrase = phrase
end
#provide the pig latin translation
def translate
@words = words
@words.collect! do |word|
if starts_with_vowel?(word)
word += 'way'
else
#do the consonant translation
until starts_with_vowel?(word) do
word = "#{word[1..-1]}" + "#{word[0]}"
end
word += "ay"
end
end
@words.join(' ')
end
private
#an array of words in the phrase
def words
@phrase.split(' ')
end
def starts_with_vowel?(word)
if word =~ /\A[^aeiou]/
false
else
true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment