The pace of class is accelerating really fast now. We started diving into Ruby this past week, and I have learned more in that short time period than I did in several months of self-study. We had a couple interesting challenges to complete over the weekend, the first of which I’ll talk about in this post.

Pig Latin Challenge

The first weekend challenge that Jeff gave us entailed creating a Ruby program that could translate any string into Pig Latin. The instructions were as follows:

PIG LATIN CHALLENGE

For this challenge, you’ll need to encode sentences into pig latin, and also decode them.

You’ll need to write two methods: one to encode, and one to decode.

Examples:

“ruby rules” becomes “ubyray ulesray” “go blackhawks” becomes “ogay lackhawksbay” “apples are happy fruit” becomes “applesay areay appyhay ruitfay”

Encoding rules are widely debated, so here are some simple ones to get us started:

  1. If a word starts with a consonant, move it to the end of the word, and then add “ay”
  2. If a word starts with a vowel, simply add “ay” to the end of the word.

After you can encode, try to decode!


If you want to get fancy, try one or more of these:

  1. If a word starts with a consonant cluster, like “challenge”, move the cluster: “allengechay”
  2. If a word starts with a Y, consider it to be a consonant, but all other Ys are vowels.
  3. The entire rulebook at http://en.wikipedia.org/wiki/Pig_Latin Rules

Here are some sentences to get you started:

address = “Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.” ruby = “Ruby gives us lots of data containers to choose from”

oodgay ucklay!

Here’s what I came up with: ``` ruby pig_latin.rb puts “Give me something to translate” string = gets.chomp.downcase

def translate_to_pig_latin(string) string.split.each do |word| if /[aeiou]/.match(word[0]) print word + “ay” + “ “ elsif word[0] !~ /[aeiou]/ && word[1] !~ /[aeiou]/ first_letter = word[0] second_letter = word[1] word.slice!(0, 2) print word + first_letter + second_letter + “ay” + “ “ else first_letter = word[0] word.slice!(0) print word + first_letter + “ay” + “ “ end end end

translate_to_pig_latin(string) ``` The translate_to_pig_latin method begins by splitting up the input into individual words. Then for each word, it checks the if the word begins with a vowel using the match method. This method allows you to check any regular expression against an object, returning nil if there is no match.

Accounting for vowels wasn’t tough, but what was difficult to wrap my head around was what to do with consonants. The original challenge says that if the first letter or first two letters are consonants, you move them to the end, then append “ay”. I thought this would be simple: just find a method like chop or chomp that would remove the first letter instead of the last. Unfortunately, I couldn’t find anything that did that. So, I used slice which removes a specified character or characters from a string. With slice, you can specify a starting position and the length of the string to slice off. Hence, word.slice(0, 2) starts at the first character, and slices two characters. Although this solved the problem of removing letters, it presented a new issue: I could no longer use those letters once they were sliced, meaning I couldn’t append them to the end of the word. What I decided to use were placeholder variables. I defined them before slice did its work, so I could essentially save the letters before they were completely lost.

That’s all there was to it. This is definitely an ugly solution and I’m going to keep looking for better ways to do this.