Created
December 7, 2013 21:50
-
-
Save jmoon90/7849189 to your computer and use it in GitHub Desktop.
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_translator' | |
| describe PigLatinTranslator do | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('happy').translate).to eql('appyhay') | |
| end | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('glove').translate).to eql('oveglay') | |
| end | |
| it 'converts consonant to pig latin' do | |
| expect(PigLatinTranslator.new('bridge').translate).to eql('idgebray') | |
| end | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('egg').translate).to eql('eggway') | |
| end | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('inbox').translate).to eql('inboxway') | |
| end | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('eight').translate).to eql('eightway') | |
| end | |
| it 'converts words in a phrase to pig latin' do | |
| expect(PigLatinTranslator.new('duck').translate).to eql('uckday') | |
| end | |
| it 'determines if the first letter of the word is a consonant or vowel' do | |
| expect(PigLatinTranslator.new('inbox').first_letter_check).to eql('vowel') | |
| end | |
| it 'determines if the first letter of the word is a consonant or vowel' do | |
| expect(PigLatinTranslator.new('happy').first_letter_check).to eql('consonant') | |
| end | |
| it 'determines if the first letter is a consonant or vowel' do | |
| expect(PigLatinTranslator.new('eight').first_letter_check).to eql('vowel') | |
| 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
| class PigLatinTranslator | |
| def initialize(word) | |
| @word = word | |
| @split_word = @word.split('') | |
| @total_count = 0 | |
| end | |
| def translate | |
| if first_letter_check == 'vowel' | |
| end_letter = 'way' | |
| @word + end_letter | |
| else | |
| consonant | |
| @total_count.times { @split_word.shift } | |
| end_letter = @consonant_letters.join('') + "ay" | |
| @split_word.join('') + end_letter | |
| end | |
| end | |
| def consonant | |
| @consonant_letters = [] | |
| @split_word.each do |letter| | |
| if consonants.include?(letter) | |
| @total_count += 1 | |
| @consonant_letters << letter | |
| else | |
| break | |
| end | |
| end | |
| end | |
| def first_letter_check | |
| @letter = @split_word[0] | |
| if vowel.include?(@letter) | |
| 'vowel' | |
| else | |
| 'consonant' | |
| end | |
| end | |
| def vowel | |
| ['a', 'e', 'i', 'o', 'u'] | |
| end | |
| def consonants | |
| ['b','c','d','f','g','h','j','k','l','m', | |
| 'n','p','q','r','s','t','v','w','x','y','z'] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment