DEV Community

Cover image for A short intro into classes in Ruby
Merdan Durdyyev
Merdan Durdyyev

Posted on

A short intro into classes in Ruby

Welcome, dear friends, readers, and coders! This article will be dedicated to classes in the Ruby programming language.

OOP (Object Oriented Programming)

Before starting classes, let’s define what OOP is. In short, OOP, or Object Oriented Programming is a concept used in almost all programming languages that let us define anything in the digital world by making it resemble a real-world object. So, what does that mean?

As we all know, any object in the real-world has attributes and movements. Otherwise, the attributes of an object are its properties, and its movements are its actions or methods. This way, we can model any type of object in a digital world by providing it with real-world object features.

Let’s try to define a car:

Properties :

  • color
  • price
  • brand
  • model
  • manufacture date
  • and etc.

Methods:

  • start
  • stop
  • turn
  • accelerate
  • brake
  • and etc.

This way we defined a real-world object with its features and now we can model it in a digital world


Classes/Objects and Methods

As we mentioned earlier, we clarify its properties/attributes and actions/methods when we define objects. We have two conflicting words here. They are “Class” and “Object”.

  • “Class”: a drawing, a draft project of an object, a block of code defining and object.
  • “Object”: A working, a living model of a block of code that was defined as a “Class”.

We can compare it to preparing drafts before manufacturing a car, or preparing drawings before building a building, or writing draft notes before writing a novel itself.


Examples of classes and objects

So now, let’s define some classes and start using them as objects.

class Car

   def initialize(car_brand, car_model)
      @brand = car_brand
      @model = car_model
   end   

   def info
      puts "Car info"
      puts "Brand : " + @brand
      puts "Model : " + @model
   end   

   def start
      puts "Car was started!"
   end

   def stop
      puts "Car was stopped!"
   end

end
Enter fullscreen mode Exit fullscreen mode

You can see, in the code above, that we have defined a class named “Car”, with its properties and methods.

Properties of the class are “brand” and “model”. You might have noticed that they are brought to use with the help of “initialize” method.

Besides those, we have several methods named “info”, “start”, and “stop” respectively. All of them are performing different tasks. Now, if we want to use this, we need to create an instance of the class, and it becomes a new Object:

tesla_model_s = Car.new("Tesla", "Model S")
opel_astra = Car.new("Opel", "Astra")
toyota_camry = Car.new("Toyota", "Camry")

toyota_camry.info
# "Car info"
# "Brand : Toyota"
# "Model : Camry"

opel_astra.start
# "Car was started!"

opel_astra.stop
# "Car was stopped!"
Enter fullscreen mode Exit fullscreen mode

I think now it is clear what we wanted to explain earlier about classes and objects. “Car” class was put into production and several new objects were created. This means, “tesla_model_s”, “opel_astra”, “toyota_camry” are unique objects. All of them have properties of different values and methods.


“Built-in” and “User-defined” classes

In one of my previous articles about Ruby, we have mentioned built-in and user-defined functions, in an article about functions.
The same as in functions, there are built-in classes. If they are not covering our requirements, and if we need our own classes (like “Car” class), then we write our own user-defined classes.

Here are some examples of built-in classes:

  • String
  • Math
  • Float
  • Array
  • Time

Goodbye

Here we finalize our short article about classes and OOP in Ruby. Hundreds of pages can be written about OOP, classes, objects, and the object-oriented architecture in whole. But we restrict ourselves with the minimum written here. You can get tons of extra info on the web if you want to dive deeper. So, never stop learning. Good luck!

Top comments (0)