Created
December 6, 2013 21:04
-
-
Save sjwats/7832063 to your computer and use it in GitHub Desktop.
Pig Latin TDD Style
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 PigLatinTranslation | |
| def initialize(phrase) | |
| @phrase = phrase | |
| @vowels = ["a", "e", "i", "o", "u"] | |
| @translation_array = [] | |
| end | |
| def translate | |
| words_array = words | |
| index = 0 | |
| words_array.each do |word| | |
| if starts_with_vowel?(word) | |
| @translation_array << (word + "way") | |
| else | |
| while [email protected]?(word[index]) | |
| index += 1 | |
| end | |
| @translation_array << (word[index..-1] + word[0...index] + "ay") | |
| end | |
| end | |
| p "#{@translation_array.join(" ")}" | |
| end | |
| private | |
| def words | |
| @phrase.split.map | |
| end | |
| def starts_with_vowel?(word) | |
| @vowels.include?(word[0]) | |
| 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
| require 'rspec' | |
| require_relative 'pig_latin' | |
| describe PigLatinTranslation do | |
| it " 'happy' → 'appyhay' " do | |
| pig_translation = PigLatinTranslation.new('happy') | |
| expect(pig_translation.translate).to eql('appyhay') | |
| end | |
| it " 'duck' → 'uckday' " do | |
| pig_translation = PigLatinTranslation.new('duck') | |
| expect(pig_translation.translate).to eql('uckday') | |
| end | |
| it " 'glove' → 'oveglay' " do | |
| pig_translation = PigLatinTranslation.new('glove') | |
| expect(pig_translation.translate).to eql('oveglay') | |
| end | |
| it " 'egg' → 'eggway' " do | |
| pig_translation = PigLatinTranslation.new('egg') | |
| expect(pig_translation.translate).to eql('eggway') | |
| end | |
| it " 'inbox' → 'inboxway' " do | |
| pig_translation = PigLatinTranslation.new('inbox') | |
| expect(pig_translation.translate).to eql('inboxway') | |
| end | |
| it " 'eight' → 'eightway' " do | |
| pig_translation = PigLatinTranslation.new('eight') | |
| expect(pig_translation.translate).to eql('eightway') | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment