Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Created December 5, 2013 19:53
Show Gist options
  • Save faizaanshamsi/7812710 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/7812710 to your computer and use it in GitHub Desktop.
pig latin translator
class PigLatinTranslation
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
word = "#{word[1..-1]}" + "#{word[0]}" + "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
puts "What phrase do you want to convert to pig latin?"
input = gets.chomp
latin = PigLatinTranslation.new(input)
puts latin.translate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment