HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Pig Latin Translator in Ruby and Rspec

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
latinrubyandrspecpigtranslator

Problem

PigLatin Kata

```
PigLatin Kata
Create a PigLatin class that is initialized with a string
- detail: The string is a list of words seperated by spaces: 'hello world'
- detail: The string is accessed by a method named phrase
- detail: The string can be reset at any time without re-initializing
- example: PigLatin.new('hello world')

completed (Y|n):

Translate Method
Create a translate method that translates the phrase from english to pig-latin.
- detail: The method will return a string.
- detail: The empty string will return nil.
- example: "" translates to nil

completed (Y|n):

Translate words that start with vowels.
- detail: Append "ay" to the word if it ends in a consonant.
- example: "ask" translates to "askay"
- detail: Append "yay" to the word if it ends with a vowel.
- example: "apple" translates to "appleyay"
- detail: Append "nay" to the word if it ends with "y".
- example: "any" translates to "anynay"

completed (Y|n):

Translate a word that starts with a single consonant.
- detail: Removing the consonant from the front of the word.
- detail: Add the consonant to the end of the word.
- detail: Append 'ay' to the resulting word.
- example: "hello" translates to "ellohay"
- example: "world" translates to "orldway"

completed (Y|n):

Translate words that start with multiple consonants.
- detail: Remove all leading consonants from the front of the word.
- detail: Add the consonants to the end of the word.
- detail: Append 'ay' to the resulting word.
- example: "known" translates to "ownknay"
- example: "special" translates to "ecialspay"

completed (Y|n):

Support any number of words in the phrase.
- example: "hello world" translates to "ellohay orldway"
- detail: Each component of a hyphenated word is translated

Solution

The code clearly works, and is very concise. I think that to make it more readable, instead of using comments, you could have expressive method names:

def translate_word(word)
  replace_consonants_to_end_of_word(word)

  append_n_to_last_letter_y(word) or append_y_to_last_letter_vowel(word)

  recapitalize(word)

  word += 'ay'
end

def replace_consonants_to_end_of_word(word)
  word.concat(word.slice!(/^[^aeiou]*/i || ""))
end

def append_n_to_last_letter_y(word)
  word.gsub!(/y$/, "yn")
end

def append_y_to_last_letter_vowel(word)
  word.gsub!(/([aeiou])$/, '\1y')
end

def recapitalize(word)
  word.capitalize! if word.downcase!
end


Sure, it's a little longer, but I believe it is more readable, and one can appreciate more how you tackled each of the requirements.

Nice code, anyway.

Code Snippets

def translate_word(word)
  replace_consonants_to_end_of_word(word)

  append_n_to_last_letter_y(word) or append_y_to_last_letter_vowel(word)

  recapitalize(word)

  word += 'ay'
end

def replace_consonants_to_end_of_word(word)
  word.concat(word.slice!(/^[^aeiou]*/i || ""))
end

def append_n_to_last_letter_y(word)
  word.gsub!(/y$/, "yn")
end

def append_y_to_last_letter_vowel(word)
  word.gsub!(/([aeiou])$/, '\1y')
end

def recapitalize(word)
  word.capitalize! if word.downcase!
end

Context

StackExchange Code Review Q#40189, answer score: 3

Revisions (0)

No revisions yet.