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.
This class is as yet incomplete. For a class to justify its existence, it needs to have two distinct features:
-
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.
-
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.