Created
December 5, 2013 19:53
-
-
Save faizaanshamsi/7812710 to your computer and use it in GitHub Desktop.
pig latin translator
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 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