logo codesdope

OOP


You have stepped into the second part of Ruby - The object-oriented programming (OOP).

Wait for a while: You came up to here. Have you celebrated this? If not, then go and take a break, eat whatever you like because you have put a milestone in your life. Believe me!

Now after your break, let's talk about classes. To understand this, let's take an example.

You are a student
You have a roll number

Your friend is also a student
He also has a roll number and a name.

In the programming world, it's like student is a class having attributes of name and roll number and you and your friends are objects of the student class. This means that both of you will have a name and a roll number.

classes and objects in ruby

Here is an another example. Think of a book having a serial number and some number of pages. Now, your science book is a book as is your computer book. Suppose the serial number of Science book is SC12 and that of the computer book is CS34 and the number of pages are 200 and 250 respectively. It is like in the following picture below:

object and class in ruby

Here, book is a class having attributes 'Page' and 'Serial number' with 'Science' and 'Computer' as its objects(instances) of it.

So, now you understand that class is something which acts like a base, having some definitions. And objects are instances of the class following those definitions.

One more example of class and objects you can think of is a bank account. Think of 'Account' as a class having attributes 'min_balance' and 'rate_of_interest' and 'Saving' and 'Current' as its objects.

object and class in ruby

Hopefully, you must have got the feel of classes and objects. We will deal with its programming parts in later sections.

Variables inside class


There can be two kinds of variables inside a class. They are:

1. Instance variables -> Instance variables are for objects of a class. E.g.- If you are an object of Student class, then your name and roll number are instance variables because they will be defined differently for different objects. This means that your name will be defined only for you and there will be nothing to do with your friend ( another object of the Student class ) because he will have his/her own name. So, instance variables are different for different objects of Student class.

2. Class variables -> Class variables are defined for class. This means that it will be same for every object of the class. If the Student class will have a variable 'no_of_students' for storing the number of students, then it will be a class variable because it will be defined for the whole Student class and not differently for different objects.

Like $ is used for global variable, @ is used for instance variable and @@ for class variable.

Methods


Methods are the functions defined in a class. Let there be a 'Square' class and a method 'get_perimeter' defined in the class to return the perimeter of a square. So, we can make different objects of the 'Square' class and by the method 'get_perimeter', we can get their perimeters.

This chapter was only to make you aware of object-oriented terms of Ruby. From the next chapter, we will continue with the programming parts.

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.