Homework for 5A

Review

Topics Covered: Referring to self; types of methods


Exercises

E1: The Lone Arraynger

In Ruby, it is often quite powerful to be able to refer to the self. However, "self" can be used in a variety of ways, so let's walk through an exercise to make sure we understand the different use cases.

We'll be adding some functionality to the Array class. For the following exercises, ask yourself whether your method sounds like an instance method or a class method and how this will affect how you define or implement it.

  1. First off, add a method naturals that takes an argument n, and returns an array of the first n natural numbers.
  2. Next, add a method increment to the same Array class that increments each element of an array by one.
  3. Finally, give the Array class a method increment_by_n that takes an argument n and increments each element in an array by n. Do this by calling your increment method on the array n times. Make sure to use the syntax for calling methods in the same class that we learned about today.

Hint: We want the increment method to change the array we are manipulating "in place". This means that we are not passing in the array to manipulate as an argument, but instead, we can only refer to the array being manipulated as "self". Not only does this avoid the need to pass the array in as an argument, it also avoids having to return it, as our method will alter its elements directly, for example with self[0] += 1. Way cool!

Array.naturals(3)
# => [1, 2, 3]

Array.naturals(5)
# => [1, 2, 3, 4, 5]

[1, 6, 8].increment
# => [2, 7, 9]
# Not increment([1, 6, 8]), don't 
# pass in the array as an argument

[5, 7, 10, 14].increment_by_n(6)
# => [11, 13, 16, 20]

After writing these methods, you should notice that you are using "self" to mean a variety of things. Add a comment to your code wherever you use "self" that explains exactly what "self" is referring to i.e. a class, or an instance.

E2: String Theory

Iglatinpay

Do you speak piglatin? You may be familiar with this constructed language game where the first letter of a word is put at the end and followed by "ay". Using these simple rules we can piglatinify "piglatin" into "iglatinpay". We can go even further and piglatinify "iglatinpay" into "glatinpayiay". And so on and so forth.

Your mission, should you choose to accept it (you have to accept it, the Piglatin community is depending on you) is to write a method piglatinify to get this behavior. Be careful to watch out for capital letters. If the original word is capitalzied, this characteristic should persist through the piglatinify process, e.g. "Ruby" becomes "Ubyray".

Histay Siay Iglatinpay

Now that you've successfully mastered the art of piglatinifying a word, let's see if we can take it one step further. Write a method piglatinify_sentence that piglatinifies each word in a sentence.

No piglatin for you, Array!

Piglatinify is obviously targeted towards strings. However, what would happen if you tried to call the piglatinify method on another class, e.g. Array? What do you think would happen?

For this reason, we would like to include the piglatinify method within the String class, so that it can only be called on strings. Extend the string class to include your piglatinify and piglatinify_sentence methods and test to make sure that they can only be called on strings. Note: you should not be passing in the string to piglatinify in as an argument, instead refer to it as "self" in your method implementations.

E3: The Final Countdown!

For inspiration, listen to The Final Countdown on loop as you work. Aside from just being an awesome song, this is supposed to remind you that we are on the final stretch of learning Ruby! Because of this, it is vital that you use the lighter workload of this morning and afternoon to finish all of the Ruby exercises so that you have a solid understanding of Ruby before starting Rails tomorrow. Good luck, we're rooting for you!