Ruby is a powerful programming language that offers modern features and tools. One such feature is support for object-oriented programming, including encapsulation polymorphism, abstraction, inheritance, and more.

This tutorial will discuss a key concept in object-oriented programming: object and classes.

What are Ruby Classes and O bjects?

Let us start with a class.

A class is a blueprint for creating objects and providing values implementations, including variables, methods, attributes, and more.

An object, on the other hand, is a single instance of a class. Take a car, for example. We can create a class that describes the blueprints of building a car. The attributes of the class can be all the things cars have in common:

  1. An engine
  2. A door
  3. A model
  4. Manufacturer

The car class can also include methods which are actions that the class objects can perform. An example would be that the car can ignite, accelerate, stop, and such.

Classes are very handy when you need to create similar objects with only varying aspects. Instead of declaring a block of code for a Mercedes car, a Bugatti ar, a Tesla, and such, you can create a single class referenced by each model.

How to Create a Class and Object in Ruby

Ruby is an incredible language that’s also very easy to use. It provides a generally simple syntax for creating a class.

The general syntax is:

class ClassName

  # BLOCK

end

We start by calling the class keyword followed by the name of the class we wish to create. The name of the class starts with an Uppercase letter and uses CamelCase for the following names.

Inside the class, we define all the variables and methods the class will require.

We close the Class code using the end keyword.

Example

Let us take a look at an example of a car class.

class Car


    def initialize(engine, name, year)


        @engine = engine


        @name = name


        @year = year


    end


    def ignite


        puts “Ignition!”


    end


    def stop


        puts “Stopping vehicle”

end


    def accelerate(target_speed)


        @target_speed = target_speed


        puts “Car accelerating at #{target_speed} m/h”


    end

end

f = Car.new(“a108d”, “F A-Class”, 2010 )


g = Car.new(“00Ca”, “Zero G-8”, 2011)


h = Car.new(“777d”, “G Potato”, 2022)

In the above code, we start by defining the car class. Inside the class, we define the initialize method, a special type of method in Ruby used to create the called instance of the class with the required arguments. The initialize method acts as a constructor for new.

Inside the initialize method, we pass the car’s engine, name, and year of manufacture. We then perform a few actions with the car, including ignition, acceleration, and stopping.

Finally, we create an instance of the car class. We call this instantiation because we create an instance of the class.

We pass the parameters of the initialize method, which creates three unique car types.

Using Class Methods

In the above example, we create a class with various methods but haven’t used them yet. To use a method, we simply enter the name of the object followed by a period and the name of the method.

The syntax is:

Let us take the previous example; to ignite the f car, we can do:

// Refer code above


f = Car.new(“a108d”, “F A-Class”, 2010 )


g = Car.new(“00Ca”, “Zero G-8”, 2011)


h = Car.new(“777d”, “G Potato”, 2022)


f.ignite

If we run the code above, we should see the car ignite successfully by printing the string “Ignition!”

Each part from the initialize method works like a command that defines the actions the object created from the class can do.

Get the Source Class

Each type in Ruby is an object of a class. You can view from which class the object is created using the class method.

For example:

class Car


    def initialize(engine, name, year)


        @engine = engine


        @name = name


        @year = year


    end


    def ignite


        puts “Ignition!”


    end


    def stop


        puts “Stopping vehicle”


    end


    def accelerate(target_speed)


        @target_speed = target_speed


        puts “Car accelerating at #{target_speed} m/h”


    end

end

f = Car.new(“a108d”, “F A-Class”, 2010 )

puts “hello”.class

puts 10.class

puts 11.023.class

puts f.class

Once we execute the code, we should see the classes for the string, integer, float, and car as:

Conclusion

In this tutorial, we learned the basics of Object-oriented programming in Ruby using classes and objects. It is good to note that OOP is a massive topic. Check out other resources to learn more.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png61213d2fc1b16.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list