Created
December 5, 2013 23:35
-
-
Save vanmichael/7816144 to your computer and use it in GitHub Desktop.
This file contains 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
require 'pry' | |
class PigLatin | |
def initialize(phrase) | |
@phrase = phrase.downcase | |
end | |
#provide the pig latin translation | |
def translate | |
@words = words | |
@words.each do |word| | |
if starts_with_vowel?(word) | |
word << ("way") | |
else | |
until starts_with_vowel?(word) | |
word << ("#{word[0,1]}") | |
word[0,1]="" | |
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) | |
["a","e","i","o","u"].include?word[0,1] | |
end | |
end |
This file contains 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
require 'rspec' | |
require_relative '../../lib/pig_latin.rb' | |
describe PigLatin do | |
it 'should translate words that begin with a consonant' do | |
pig_latin = PigLatin.new("happy") | |
expect(pig_latin.translate).to eql "appyhay" | |
end | |
it 'should translate words that begin with a cluster of consonants' do | |
pig_latin = PigLatin.new("glove") | |
expect(pig_latin.translate).to eql "oveglay" | |
end | |
it 'should translate words that begin with vowels' do | |
pig_latin = PigLatin.new("egg") | |
expect(pig_latin.translate).to eql "eggway" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment