Created
June 28, 2017 17:04
-
-
Save supaspoida/07a03ed6e3a0b3cf0950e33b2ef7aee3 to your computer and use it in GitHub Desktop.
Translate Phrases to Pig Latin
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
require 'spec_helper' | |
module PigLatin | |
extend self | |
def translate(phrase) | |
words = phrase.scan(/\w+/) | |
translations = words.map { |word| [word, Translate[word]] } | |
translations.inject(phrase) do |template, (original, translated)| | |
template.gsub(original, translated) | |
end | |
end | |
Translate = -> word { | |
case word | |
when /^[aeiou]/ | |
word + 'ay' | |
when /^[A-Z]/ | |
Translate[word.downcase].capitalize | |
else | |
word[1..-1] + word[0] + 'ay' | |
end | |
} | |
end | |
describe "Pig latin" do | |
subject { PigLatin.translate(phrase) } | |
describe 'hello' do | |
let(:phrase) { 'hello' } | |
it { should == 'ellohay' } | |
end | |
describe 'apples' do | |
let(:phrase) { 'apples' } | |
it { should == 'applesay' } | |
end | |
describe 'eat world' do | |
let(:phrase) { 'eat world' } | |
it { should == 'eatay orldway' } | |
end | |
describe 'Hello' do | |
let(:phrase) { 'Hello' } | |
it { should == 'Ellohay' } | |
end | |
describe 'Eat' do | |
let(:phrase) { 'Eat' } | |
it { should == 'Eatay' } | |
end | |
describe 'hello... world?!' do | |
let(:phrase) { 'hello... world?!' } | |
it { should == 'ellohay... orldway?!' } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment