Skip to content

Instantly share code, notes, and snippets.

@supaspoida
Created June 28, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supaspoida/07a03ed6e3a0b3cf0950e33b2ef7aee3 to your computer and use it in GitHub Desktop.
Save supaspoida/07a03ed6e3a0b3cf0950e33b2ef7aee3 to your computer and use it in GitHub Desktop.
Translate Phrases to Pig Latin
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