Last active
August 29, 2015 13:56
-
-
Save truffaut/9083358 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
class PigLatinTranslator | |
def translate(word) | |
if /\Ay/.match(word) | |
word.slice!(0) | |
word + "yay" | |
elsif match = /\A[^aeiouy]+/.match(word) | |
consonants = match.to_s.length | |
ending = word.slice!(0, consonants) + "ay" | |
word + ending | |
else | |
word + "way" | |
end | |
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 'pry' | |
require_relative '../lib/pig_latin_translator.rb' | |
describe PigLatinTranslator do | |
let(:translator) { PigLatinTranslator.new } | |
describe '#translate' do | |
context "the word begins with consonants" do | |
it "removes leading consonants before first vowel, appending consonants + 'ay' to the end of the word" do | |
expect(translator.translate("glove")).to eq("oveglay") | |
end | |
end | |
context "the word begins with vowels" do | |
it "adds 'way' to the end of the word" do | |
expect(translator.translate("egg")).to eq("eggway") | |
end | |
end | |
context "'y' is encountered before a vowel" do | |
context "'y' is the first letter in the word" do | |
it "removes 'y' and appends it to the end of the string followed by 'ay'" do | |
expect(translator.translate("yellow")).to eq("ellowyay") | |
end | |
end | |
context "'y' is not the first letter in the word" do | |
it "removes the preceding consonants and appends them to the end of the word followed by 'ay'" do | |
expect(translator.translate("rhythm")).to eq("ythmrhay") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment