Learn Ruby

Car maker class definition

Understanding what class is challenging. So if you don’t fully understand the concept the very first time, don’t worry about it. Please read the next fews lessons where we try to go over the concepts again. It will take some time but you will understand it.

Let’s start with cars. How do car manufacturing companies make cars. All car making companies have a giant assembly line where cars are made. We will call that giant assembly line a “car making machine”. The job of this “car making machine” is to make cars.

Think of a class like a “car making machine”. We will call our class Carmaker. Here is how in Ruby we declare our class.

class Carmaker

We got a class which is like a “car making machine”. Now the question is how do we make a car. Ruby allows us to use new to make a car out of Carmaker class. new is truly a magical thing in Ruby. It can make a car out of a Carmaker.

Let’s try it out.

Notice that that Carmaker starts with a capital letter. So far we have never used anything that starts with a capital letter. This is because so far we have been dealing with variables. In Ruby all variables must start with a small letter. A class is a special thing. A class must start with a capital letter.

Let’s go over what we did here.

First we created a class named Carmaker.

Then we used new to create a car out of the CarMaker. We need a word to described what is created out of the class. That word is instance. Here we created an instance of car from the class Car.

An instance is like a real car that is built out of the “car making machine”. When we say we have an instance of class Carmaker then what we mean is that we have a car that is created from the class Carmaker.

I know all this class and instance things are confusing. But please keep reading. We will go over these concepts again in the next few lessons. Trust me you will understand all this very soon.