DEV Community

Cover image for pig_it: Transform English to Pig Latin with Ruby Case Statements
Whiting
Whiting

Posted on

pig_it: Transform English to Pig Latin with Ruby Case Statements

There are more than a few sites out there dedicated to helping coders improve their problem-solving skills through programming puzzles. If you have ever visited Codewars.com, a martial-arts-themed site, you may have encountered the Simple Pig Latin challenge:

Simple Pig Latin

The test asks us to write a function that will take any string of words and Pig-Latin-ify it - chop off the first letters and add the suffix 'ay'. Let's sharpen up our samurai swords and dive into this problem.

Getting the test to pass: Split, Map, Slice, Join

First, let's write out some pseudo code to plan an approach.

// 1. Transform the string into an array of words (split method).
// 2. Iterate over each word in our new array to perform our transformations (map method).
// 3. Transform each word by by slicing the first letter off and adding it to the end with our 'ay' suffix (slice method).
// 4. Join our words together into our solution, a transformed string of Pig Latin! (join method).

With these methods at hand, our solution code might look something like this:

solution 1

And, hey! According to Codewars, the solution passes muster.
passed test

But wait, what is that word in the first test? It looks like "is" was translated into "siay." That doesn't sound right. As any true scholar will know, Pig Latin has special rules for words that start with vowels.

Indeed, according to the internet (at least one blogger), words in Pig Latin are not always chopped off at the first letter:

When a word begins with a vowel, simply leave the word as is and add the suffix ‘-hay’ to the end of the word."

It sounds like we have some special cases to account for...time to bring in the case switch statement.

The Case Switch statement

The case switch statement allows you to define a condition against which you can test multiple variables, or cases.

The first when clause to match the condition (or to evaluate to Boolean truth, if the condition is null) “wins”, and its code stanza is executed.

(source)

Case Switch Ruby

In the case of our pig_it function, we need to test whether the first letter of each word is a vowel to determine whether we need to chop it off or not. Helpfully, we can test multiple conditions on the same line separated by commas, as in line 28 below:

vowels

A Case within a Case

Not so fast. What about words that start with consonant clusters, like 'shower' and 'brush'? Our function as it stands would transform them into 'howersay' and 'rushbay', splitting up the 'sh' sound 'br' sounds...Let's revisit our linguistic documentation.

When a word begins with a consonant (such as dog) or a consonant cluster (such as brush), simply take the consonant/consonant cluster and move it to the end of the word, adding the suffix ‘-ay’ to the end of the word.

Ok, let's test for some consonant clusters, slicing the first two letters of each word, and then move on to the vowels test:

final

And what does this function do to the following boobie-trapped sentence?

"Everyone should think twice when taking a photo in theaters."

"yWhay everyoneHay ouldShay inkThay iceTway eforeBay akingTay aHay otoPhay inHay eatersThay."

Impeccable Pig Latin.

samurai

References:

CodeWars
How to Speak Pig Latin
ruby-doc.org

Top comments (0)