Created
December 5, 2013 20:46
-
-
Save faizaanshamsi/7813577 to your computer and use it in GitHub Desktop.
Pig Latin Translator and Tests
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
--color |
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 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 |
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 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