Last active
December 30, 2015 05:19
-
-
Save jmoon90/7781734 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
| class PigLatinTranslation | |
| def initialize(phrase) | |
| @phrase = phrase | |
| @translated_phrase = [] | |
| words | |
| puts @translated_phrase.join(' ').capitalize | |
| end | |
| #provide the pig latin translation | |
| def translate(word) | |
| if starts_with_vowel?(word) == true | |
| @translated_phrase << (word + "way") | |
| elsif consonant_letter?(word) == true | |
| letter = word.split('') | |
| letters = [] | |
| @count.times do | |
| letters << letter.shift | |
| end | |
| new_word = letters.join('') + "ay" | |
| @translated_phrase << (letter.join('') + new_word) | |
| end | |
| end | |
| private | |
| #an array of words in the phrase | |
| def words | |
| words = @phrase.split(" ") | |
| words.each do |word| | |
| translate(word) | |
| end | |
| end | |
| def starts_with_vowel?(word) | |
| vowel = ['a', 'e', 'i', 'o', 'u'] | |
| vowel.include?(word[0]) | |
| end | |
| def consonant_letter?(word) | |
| @count = 0 | |
| consonant = ['b', 'c', 'd', 'f', 'g', 'h', 'j', | |
| 'r', 'l', 'm', 'n', 'p', 'q', 'r', | |
| 's', 't', 'v', 'w', 'y', 'z'] | |
| word.split('').each do |letter| | |
| if consonant.include?(letter) | |
| @count += 1 | |
| else | |
| break | |
| end | |
| end | |
| true | |
| 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_relative("pig_latin_translation") | |
| puts "Welcome to John's Pig Latin Translation Script" | |
| puts "----------------------------------------------" | |
| puts "What phrase would you like me to translate?" | |
| print "> " | |
| phrase = gets.chomp | |
| PigLatinTranslation.new(phrase) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment