Ruby Day 2

My experience with day two of exploring Ruby through Seven Languages in Seven Weeks.

Day 2 of Ruby covered classes, objects and collections. The key building blocks. In this post I’ll take a quick run through them.


Handily in Ruby you can define functions in the console without having to create a class. This allows you to test out things with minimum code. A ket thing to get into your head is that every function returns a value. This can be something you explicitly define using a return statement or the value of the last expression processed before exiting. In Ruby functions are objects and therefore can be passed as parameters to other functions which is a powerful feature.


Defining and accessing the Array elements feel very familiar for me having primarily programmed in Java but Ruby offers some nice extras like the ability to select elements starting at the end of the collection and select ranges. Trying to access an element which does not exist returns nil rather than an exception.

Arrays can contain a mixture of types e.g.

Array has a very rich API which allows it to be used like other well known collections such as queues, stacks and sets. More about this later with some sample code.

Nothing too new with hashes either.

Code Blocks

A useful feature of Ruby are code blocks which are functions without a name that can be used as a parameter to a function or method. The example below shows how succinctly you can express things. It iterates over the elementsand prints them out

In the following example we have a method which handles beginning and ending a transaction. We can put the code for a particular transaction within a code block and associate it with the method. When the yield is reached in the method the code block is executed.

Classes and Inheritence

Like Java Ruby has the concept of classes and objects. It supports single inheritence from a superclass. You can find the superclass using the superclass method. You can chain multiple calls to this method to find further up the inheritence tree.


A Mixin is a way of describing behavior where you define common behaviors and constants in a module and then add them to classes by including the module. They play a similar role to interfaces in Java. In the example below the module includes methods to create a file and write an object to the file using its to string method. The behavior defined in this module could apply to lots of different types of objects. You can see the module has no to_s method itself. It will use the one in the class. Any subclass of the class will have the capabilities defined in the mixin without needing to know about the mixin’s implementation. i.e. Single inheritence defines essence, modules define additional capabilties.

Other mixins include enumerable and comparable.A class wanting to be enumerable must implement each, and a class wanting to be comparable must implement <=>.Enumerable and comparable provide many convenience methods for collections.



First we had to look at accessing a file with and without a code block. When you use a code block Ruby closes the file for you when it is finished without you needing to explicitly ask it to. It guarantees that it will close the file even if an exception occurs.

The next task was translating a hash to an array which is very simple because the Hash API has a to_a method which does this.

Next we had to translate the array back to a hash.

Then we had to iterate through a hash. I used the each method.

Finally some examples of using an Array like other common data structures.


Print the contents of an array of sixteen numbers, four numbers at a time, using just each. Now, do the same with each_slice in Enumerable.

The Tree class was interesting, but it did not allow you to specify a new tree with a clean user interface. Let the initializer accept a nested structure of hashes. You should be able to specify a tree like this: {‘grandpa’ => { ‘dad’ => {‘child 1’ => {}, ‘child 2’ => {} }, ‘uncle’ => {‘child 3’ => {}, ‘child 4’ => {} } } }.

Write a simple grep that will print the lines of a file having any occurrences of a phrase anywhere in that line. You will need to do a simple regular expression match and read lines from a file.

Check out my code at github