Ruby on Rails – Lesson 1: Classes and Objects

 

This tutorial is written for the person who is an absolute beginner to programming. I will make frequent analogies to real-world things, outside of programming as a means to illustrate important programming concepts in general and object-oriented concepts in particular. Too often, tutorials for a language assume a working knowledge of programming in another language. That is not the case here. If you’ve never written a line of code in your life, you should feel completely comfortable with these lessons.

This course assumes you already have Ruby installed on your computer.

Classes and Objects

Before we can get into Rails, we really need to understand Ruby. There are those who disagree with this statement, but it is helpful, in my opinion, to understand where Ruby, the language, ends and Rails, the framework, begins. We need to get over one other MAJOR mental concept.. We need to understand classes… and objects.

Ruby is what is called an object-oriented language. As such, classes and objects are very central to the design and use of the language. The beginner might ask “What’s a class? What’s an object??”

An analogy (or two) is in order.  Consider a cookie cutter. A cookie cutter is a kind of specification, or blueprint for.. cookies. Given cookie dough and a Christmas tree-shaped cookie cutter, one could make zero, one or many cookies using the cookie cutter. In some respects, the cookies will have certain characteristics that will always be the same across all cookies made from it, namely, the outline/shape. They will all be identical. However, it is also possible to use different kinds of dough, sprinkles on some cookies, not others, frosting on some, not others. In this regard, each individual cookie can differ from all the other cookies.  In programming terms, we call each cookie an object… and the cookie cutter is the class (blueprint) used to create the objects.  Another term you should learn is instance. An instance is merely an individual cookie. Multiple cookies are referred to as “instances”.

The “things” that can vary between objects are called properties. Cookies are a little sloppy. Let’s consider a better example. An automobile assembly line…. and the cars produced by it. Of course, the assembly line is like a “class” and the cars produced are “objects” or “instances of the class” (these are just words.. don’t get too hung up on them….).

Some cars have black interiors, others blue, still others tan. So, “interior color” is something that can vary between vehicles… therefore, interior color is considered a property of the class. So is engine size, so is exterior color, VIN, etc. While every car produced by the assembly line is largely similar, each “instance” can vary  in many ways.

Let’s take a look at how this might look in Ruby code:

Declaring a class

 

In the code above, the line that reads “# Class definition: Person” is a special kind of statement in Ruby.. it is called a comment. A comment does not actually do anything… rather it serves as additional information, beyond what merely reading the code alone provides. Why write a comment in our code? Because in the world of professional programming, chances are very good that someday a programmer other than the programmer who originally wrote this code will have to make changes to it. Comments are merely a way of leaving “notes” behind for the next programmer, as two what the original author was thinking at the time they wrote the code.

The next line, “class Person” begins our declaration of the class we’ve decided to name Person. The line that reads “end” ends our class definition. All the lines between “class” and “end” constitute our specification of what we want to the class to do.  For now, we’re going to ignore the lines between “class” and “end”, but rest assured they will be thoroughly explained.

Now that we’ve declared or defined a class.. it’s time to use it. Let’s create three instances (objects) of type Person.

Creating instances of a class

a = Person.new
b = Person.new
c = Person.new

This is an important piece of vernacular to get your head around: We create objects of a type, and we say that as “creating objects of type x” (whatever class x happens to be).

Now, you may  be asking “What’s up with the letters a, b and c as well as the equals sign in the above examples. Also, what’s up with “.new” at the end of Person? Worthy questions all.  Let’s take just one of these lines and examine it in detail.

Creating an object, step by step

a = Person.new

In the code above, “Person.new” is our way of telling the Ruby interpreter we want to create a new instance of the Person class. More about the interpreter later. Presumably once we create the new instance, we’ll want to do something with it. Just about the only way we can do something with an object is to save a reference to it in something called a variable. A variable is a name we declare simply by typing it and assigning something to it. So then, the code above does three things: 1) creates a new instance of the Person class, 2) declares a variable named “a” and 3) assigns a reference to the newly created Person instance to our variable “a”.

 

Term: Instantiate
When we speak of creating instances of an object from a class, one verb we use to describe this process is “instantiate”. We instantiate objects from a class.

Leave a Reply

Your email address will not be published. Required fields are marked *