Project Ramon

A learning journey from a Ruby noob perspective

Ruby classes pt.2 – You’ll never guess what herbie can do!


In my last post, I briefly went over some basics about classes. That post was about setting up a class, adding an ‘initialize’ method so that objects have the ability to come stock with defaults, and it closed with brief descriptions of Ruby’s four variable scopes and their differences.

Today, I’m going to start of by actually creating an automobile object, from our Automobile class or factory. First though, here’s a quick glance at what our Automobile class from yesterday looked like.

To create an object of your class, you use the ‘new’ class method along with the class name, which in our case is the constant ‘Automobile’. It is typical to store objects in a variable that is named in a way that makes sense, usually a descriptive name of the object. Here’s an example.

Above I assigned the ‘’ to a variable called herbie, and passed in a string parameter called “car” for the initialize’s @type instance variable. We can now use this object in our program.

Before I move on, lets briefly discuss how to give your objects access to the attributes of its class. In Ruby there are reader methods and writer methods also known as getter and setter methods (respectively). The manual way of allowing your attributes to be seen and set is to do something like this:

The first method in the example above allows the value stored inside of the instance variable to be read accessible, meaning you can show the value being held in the variable returned inside the ‘type’ method.

The second method in the above example is longhand for for the instance variable @type to be set or updated, meaning that if there is a value currently being stored in @type, that old value can be overwritten with this method.

There are three shortcut methods to using the long-hand form from the above example. These methods are called ‘attr_reader’, ‘attr_writer’ and ‘attr_accessor’. Using ‘attr_reader’ and ‘attr_writer’ allows Ruby to create either read access methods or write access methods for you automatically. This works by passing symbols as parameters to any of these methods like so:

The code above demonstrates the short-cuts available to keep from having to write getter and setter code that you saw in the example preceding this one.

However, there is one more powerful shortcut. If you know that you want read and write access to your attribute, and you see the advantage of writing one line of code for this privilege instead of typing out two-lines. Then ‘attr-accessor’ is  the solution to your dilemma.

Here’s an example of both the long and concise ways of adding read/write accessibility:

So wait.. We have an object made from our Automobile class, we have allowed the only attribute for our class be accessible to both read out the attribute value and update that attribute value. But currently our class doesn’t have any functionality built into it. So our herbie object can’t do anything at all yet, although it does have an attribute to describe its type. Lets add to our class, so our ‘herbie’ object can do something.

In Ruby functions are called methods, and methods are the action’s we can build into our classes for our objects to use once created. I’m going to build 3 methods into our Automobile class, ‘start_engine’, ‘stop_engine’, and ‘drive’. I could go into better detail than just these methods, as a typical car has many more actions it would need to take for you or I to feel safe enough to drive out on the road, but this is a bare bones demonstration of how Ruby member functions or methods work. Here’s an example of how to set up methods:

And this is how we can let our new ‘herbie’ object use these methods to prove that ‘herbie’ isn’t just some stationary object:

This is how we add functionality to our objects, and have our objects utilize that functionality at our request.


Categories: AirPair, Newbie, Ruby, Ruby on rails

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s