Ruby 101: Object Oriented Programming part 1

Part III: Classes, Objects, Attributes & Methods

It is the third part of Beautiful Ruby series by Tk. If you are a totally beginner developer learning Ruby, you should start with part I & part II of this series. The first part we learn about the history about Ruby & very basic stuff like variables, control flow (if-else statements) and looping / iterator. For the second part we talk more about Ruby Data Structures, how it works & how we can iterate through it.

Classes & Objects

A little bit of theory:

Objects are a representation of the real world objects like cars, dogs, bike, etc. The objects share two main characteristics: data and behavior.

Cars have data like number of wheels, number of doors, seating capacity and also have behavior: accelerate, stop, show how much fuel is missing and so many other.

We call data as attributes and behavior as methods in object oriented programming. Again:

Data → Attributes & Behavior → Methods

And a Class is the blueprint from which individual objects are created. In the real world we often find many objects with all the same type. Like cars. All the same make and model (have an engine, wheels, doors, …). Each car was built from the same set of blueprints and has the same components.

Ruby Object Oriented Programming mode: ON

Ruby as an Object Oriented programming language has these concepts: class & object.

A class is a blueprint, a model for its objects.

So again, a class it is just a model, a way to define attributes and behavior (as we talk in the theory section). As an example, a Vehicle class has its own attributes that defines what is a Vehicle object. Number of wheels, type of tank, seating capacity and maximum velocity are all attributes of a vehicle.

With this in mind, let’s understand Ruby syntax for classes:

We define classes with class statement and finish with end. Easy!

And objects are instances of a class. We create an instance by calling .new method.

Here vehicle is an object (or instance) of the class Vehicle.

“A vehicle is a instance of the class of objects known as vehicles.”

Remember that our Vehicle class has 4 attributes: Number of wheels, type of tank, seating capacity and maximum velocity. We set all this attributes when creating a vehicle object. So here we define our class to receive data when instantiates it.

We use the initialize method. We call it a constructor method. So when create the vehicle object, we can define this attributes. Imagine that we love the Tesla Model S and we want to create this kind of object. It has 4 wheels, it is an electric car so the tank’s type is electric energy, it has space for 5 seats and the maximum velocity is 250km/hour (155 mph). Let’s create this object! :)

4 wheels + Electric type of tank + 5 seats + 250km/hour maximum speed.

All attributes set. But how can we access this attributes values? We send a message to the object asking about them. We call it a method. It’s the object’s behavior. Let’s implement it!

This is an implementation of two methods: number_of_wheels and set_number_of_wheels. We call it getter & setter. Because the first get the attribute value, and the second set a new value for the attribute.

In Ruby, we can do that without this methods. It is the attr_reader, attr_writer and attr_accessor. Let’s see it with code!

  • attr_reader: implements the getter method
  • attr_writer: implements the setter method
  • attr_accessor: implements both methods

So for now we learnt how to get attributes values, implement getter and setter methods & use attr (reader, writer and accessor).

But we can also use methods to other things like “make_noise” method. Let’s see it!

When we call this method, it just returns a string “VRRRRUUUUM”.

That’s it!

We learnt a lot of things about Ruby object oriented programming:

  • Objects & Classes
  • Attributes as objects’ data
  • Methods as objects’ behavior
  • Using Ruby getters and setters

I hope you guys can appreciate the content and learn more about how Ruby object oriented programming works. This is just the third part of Beautiful Ruby series. For part 2 of Ruby OOP we are going deep in other concepts!

If you didn’t see the part I & part II, get the role series!

If you want a complete course, learn real-world coding skills and build projects, try One Month Ruby Bootcamp. See you there! ☺

Have fun, keep learning & always coding!

I hope you liked this content. Support my work on Ko-Fi

My Twitter & Github. ☺



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store