5.1 Building your own class

Getting classy

Let us start by building a class of our very own. Of course, a class needs a job or a role that its instances fulfill, or we have no reason to create one. Let us start by creating a class that represents a simple geometric Rectangle.

As you may have noticed already, classes in Ruby have names beginning with a capital letter. This convention has other implications that we shall go into later - for now, all our classes shall have names that begin with a capital letter.

Example Code:

Output Window

This class is as yet incomplete. For a class to justify its existence, it needs to have two distinct features:

  1. State

    A class must have some kind of state that defines the attributes of its instances. In the case of a simple rectangle, this could simply be its length and breadth.

  2. Behaviour

    A class must also do something meaningful. This is achieved by the programmer adding methods to the class that interact with its state to give us meaningful results.

State and behaviour

"What is your perimeter?" and "What is your area?" sound like an interesting questions to ask of a rectangle. This behaviour will be defined as methods on the class Rectangle. Starting from the behaviour and working backward is an excellent way to understand exactly what state we need, something we shall address in the next section. Since this is the first time you're creating a method, let me demonstrate this for you with the perimeter, and then you can try adding the area yourself.

Example Code:

Output Window

The keyword def is what we use to create a method called perimeter. You will observe that Ruby follows a two-space indentation convention and sections of code are typically closed using the keyword end. Note that both the class and the method are closed in this manner. Of course, this class still does nothing, because all we have is a stateless class with an empty method. Let us now try to fill up this empty method with the formula for the perimeter of a rectangle, which is 2 * (length + breadth).

Example Code:

Output Window

You'll notice that the variable names length and breadth have an @ symbol placed in front of them. This is a convention which designates them as being a part of the state of the class, or to use jargon, they are the "instance variables of the class." This means that every instance of the class Rectangle will have its own unique copies of these variables and is in effect, a distinct rectangle.

If you actually run this code, though, you'll see that it fails. Try adding Rectangle.new.perimeter to the end of the previous example. See what happens? @length and @breadth don't have values yet because we have no way to initialize them. Lets remedy this now.

Example Code:

Output Window

Observe the tests for this piece of code carefully - you'll notice that we can now create instances of Rectangle that correctly tell us their perimeter.

It's your turn now - extend this class to add a method that calculates area using the formula length * breadth.

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Sidu Ponnappa and Jasim A Basheer