pig_it: Transform English to Pig Latin with Ruby Case Statements
Published: 2021-08-18 04:54:52


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](codewars.com), a martial-arts-themed site, you may have encountered the Simple Pig Latin challenge:

![Simple Pig Latin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1vo6goy1i7axexgw039.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avvx8n05uqt30lk7m863.png)

And, hey! According to Codewars, the solution passes muster.
![passed test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8n5rp2z0bvyyd770g14x.png)

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](https://www.getblend.com/blog/how-to-speak-pig-latin/)), 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](https://ruby-doc.org/docs/keywords/1.9/Object.html))

![Case Switch Ruby](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qji9le6yzoms953hkmw7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6utylqc56g4k70ivj45.png)


### 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](https://www.getblend.com/blog/how-to-speak-pig-latin/).

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cktj9dg2l7691abpbtgw.png)

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](https://media4.giphy.com/media/jzHFPlw89eTqU/giphy.gif?cid=790b7611c5f6f7d6faf780041b413476ec916015a42482c5&rid=giphy.gif&ct=g)








####References:
[CodeWars](codewars.com)
[How to Speak Pig Latin](https://www.getblend.com/blog/how-to-speak-pig-latin/)
[ruby-doc.org](https://ruby-doc.org/docs/keywords/1.9/Object.html)