Greetings, dear readers. As many of you know, I'm learning Ruby, one of many programming languages. Ruby is an object-oriented language, which doesn't mean much if you don't know what an object is in terms of programming. So let's talk about it. An object is just a thing. Any thing. Objects are the nouns of a programming language. A method is what objects do; methods are the verbs. Just like a person walks or a bird flies, an object does a method. It is important to note that not all objects can perform all methods. For instance, a person cannot fly (at least not without the assistance of some other object), but a bird can. So how does a program keep track of which objects can perform which methods? Enter: the class.
A class creates objects, which we call instances of that class. Each time we create a new object, we say that it is instantiated; this is the most important function of the class. The class also contains the definitions of the methods these objects can perform. Let's look at an example of some real-world objects and describe how they would behave if they were objects of a class. In case you don't know yet, I love to crochet. So for now, we will focus on crochet hooks as our objects.
In Ruby, our classes are capitalized, while the instances are lowercase. So we will call our example the Hook
class. Crochet hooks come in various sizes and colors. Although the color may not be the most important attribute, it can sometimes help us to differentiate between different hooks. Much more importantly, different crochet patterns call for different hook sizes. So, each instance of the Hook
class, will have a size and color associated with it; these will be our instance variables. Every time we instantiate a new hook
object, it must be assigned both a size and a color. We indicate our instance variables with an @
symbol, like this:
If we want to access this information regarding our hook
object, we will need methods for this. Fittingly, we will call these methods size
and color
. Just for fun, let's also create a method that will give us both attributes at once; we'll call it the show_hook
method.
Yikes! That seems like a lot of methods just to inspect our object. There is a shortcut for creating those first two methods, which will also allow us to access these instance variables from outside the class. For example, if we create a Yarn
class, a given yarn
object could read the @size
and @color
variables from a hook
object. In order to accomplish this, we will create an attribute reader (attr_reader
). An attr_reader
will return the instance variable. There are also attribute writers (attr_writer
), which allow objects outside the class to overwrite these variables. Finally, attribute accessors (attr_accessor
) perform both functions.
It will be important that these variables and the others we will create stay inside of the class; we don't want them to be manipulatable by anything outside of our Hook
class. Once our blanket is made, no one should be able to alter the number of loops in a stitch or the number of stitches that have been made. (For the sake of our metaphor, let's pretend that there's no such thing as scissors or creatively destructive children). Since we do not want other objects to be able to overwrite our variable values for @size
or @color
, let's stick to the attr_reader
:
Notice that we used symbols for our attribute readers, which we indicate with a colon at the beginning of the word. Also, our size
and color
methods are no longer needed. Assigning our attribute readers comes with these methods automatically. Thanks, Ruby!
As we have mentioned, our class should contain all of the methods a crochet hook needs to be able to carry out. So let's talk about what a crochet hook actually does. Perhaps we want to be able to write a program that can make a blanket. The hook will need to be able to complete various types of stitches. But how can we differentiate between the various types of stitches? Each stitch is made up of three actions: insert
s, yarn_over
s (yo), and draw_through
s. Normally, we would create methods for each of these, but for the sake of time, I will just name each one as a String
variable (which we indicate with quotation marks). Each stitch will be an Array
(indicated with square brackets), containing the various action strings in a particular order. For example, let's try the single crochet stitch shown below:
single_crochet
method would require one insert
, one yarn_over
, one draw_through
, another yarn_over
, and then two draw_through
s.
We also might want to build in a way to create multiple stitches in a row. We can have our single_crochet
method accept an argument that gives the number of stitches. Just to be safe, let's set the default to 1, so if we don't pass an argument, only one stitch is created.
Once we have all of our methods defined in the class, we are ready to create objects and ask them to carry out methods. So let's work on making our blanket! First things first: we must instantiate (create) a new hook
object. This one will have a size of "F"
and the color "green"
. From there, we can ask our hook
object to carry out any of the methods defined in the Hook
class. Let's call our show_hook
method to make sure things have been set up correctly. Finally, we'll carry out 20 single crochet stitches using our single_crochet
method.
With the right methods called in the right order, we'll end up with a beautiful blanket. And since I'm sure you've all been wondering, here is the first pattern I ever designed myself:
All this with a singlehook
object performing its pre-defined methods.
P.S. My maiden name is Onayemi, and I made this blanket for a family member with the same surname.