DEV Community

Jasmin Song
Jasmin Song

Posted on • Edited on

Ruby Object Oriented Programming

Ruby is an object oriented programming language. Everything in Ruby is an object. Objects contain data, such as attributes and methods. In other words, objects are the basic building-blocks of Ruby code. In Ruby, we are sending messages to objects.

Object oriented programming structures code so that its functionality can be used throughout the application. It also is beneficial for when programs change and grow. Object oriented programming allows us to organize our code to have classes and create specific instances of those classes.

In this blog post, we will talk about the principles of object oriented programming. Hopefully you leave with a better idea of why it is helpful, and how it relates to the real world!

Classes

Classes are a way we construct objects in object oriented programming. We are encapsulating what something is. For example, a cat class would look like this:

Image description
Here, we are building the Cat class. The keyword end declares that the block is over. We can create a Cat now. Now we can add lines 5 through 7 and when we puts Udon, we see an object of a new cat. We also see a memory address of where the object exists.

Image description

Instances

If we make another cat, we get a unique address for the new object. We are making specific instances of a cat.

Image description

The class is a blueprint and the Udon and Minari objects are specific instances of our Cat class. However, our Cats class should not be blank. A class is like a blueprint that defines how to build an object. Our cat class should have specific methods, or functions, to describe what the cats do. So let's add some methods!

Initialize

If we want instances of our class to be created with data, we need to define our initialize method. This ensures that whenever .new is used, #initialize is called.

If we want to define the initial property of the cat, we need to do that in a constructor of a class. In Ruby, the constructor is a function called initialize.

Image description

Instance Methods

Instance methods are responsible for doing something for each cat instance.

Here, we are stating that each cat has four legs, two ears, and has a tail. If we want to access these properties, we are unable to because by default, these instance properties are private.

Getters and Setters

Getter and setter methods enable us to set attributes to our objects and get the value of those attributes.

In order to get and set the values in our initialize method, we need to use getter and setter methods.

The setter method is defined with an equal sign and followed by the parameter. The equal sign is part of the method's name. The getter belongs to every instance of our Cat class.

Image description

Now that we have set the setter and getter methods, when we puts Udon.legs and Minari.legs, we get the value of legs. We are also able to set the cats' legs, by saying Udon now has 3 legs.

Image description

Local Variables

Our initialize method can take in arguments. If it has a name property, we can say @name=name. without the @ symbol, it is a local variable. With the @ symbol, it is available for each cat.

Image description

So now if we want to add a name for each cat, we add an instance property. The instance of Udon has a different instance than Minari. So each cat has its own name.

Now if we want it to have a variable that is available to both cats, then we create a class variable using two @ signs. @@

Here, we will add a class variable @@total_cats and make it equal to zero. Then in our initialize method, we will add @@total_cats += 1. We will also need a class method, a method that belongs to cats. Here, I called it Cat.total on lines 13 through 15.

Image description

Now we see the total cats as two.

Cat.new and Cat.total are class methods because we are calling it on the Cat class. As mentioned before, in Ruby, the class in itself is an object.

Object oriented programming uses objects that contain data, attributes, and functions (or methods). Objects are the building blocks of Ruby since everything in Ruby is an object. Object oriented programming allows programmers to form models of real world problems/concepts.

Top comments (0)