Table of contents
1.
Introduction 🙏🏻
2.
Defining a Simple Class in Ruby🧑🏻‍💻
3.
Creating the Class👻
3.1.
Syntax
3.2.
Example
4.
Instantiating a Point🎯
4.1.
Syntax
4.2.
Example
5.
Initializing a Point👇🏻
5.1.
Initializing Point Without Parameters
5.1.1.
Syntax
5.1.2.
Example
5.1.3.
Output
5.2.
Initializing Point With Parameters
5.2.1.
Example
5.2.2.
Output
6.
Defining to_s Method🥸
6.1.
Example
7.
Frequently Asked Questions
7.1.
What is ruby?
7.2.
What are all the editor which provides support for ruby?
7.3.
Can we repeat the definition of class in ruby?
7.4.
How are class variables defined in ruby?
7.5.
What is a singleton class in ruby?
8.
Conclusion 🙋🏻‍♀️
Last Updated: Mar 27, 2024
Easy

Defining A Simple Class In Ruby

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 🙏🏻

Ruby is a powerful yet simple object-oriented programming language. It is good at text processing. Ruby can be used to write servers and experiment with prototypes. It has simple syntax, can handle exceptions, etc.

Defining a Simple Class in Ruby

We will go through the topic of defining a simple class in ruby and much more in this blog.

Are you ready to take this knowledgeable ride with us? 

Defining a Simple Class in Ruby🧑🏻‍💻

First, you need to learn how to create objects and classes in Ruby for implementing object-oriented programming using ruby. So, we will go through each step in the following topics to completely understand defining a simple class in Ruby.

Creating the Class👻

In ruby, you can easily create a class and object. You need to add a keyword "class" followed by the class's name, and the class's first letter should be in the capital. For terminating a class, we use the keyword "end". We add all the data members between class definition and end keyword to include them in class.

Syntax

class Point 
end
You can also try this code with Online Ruby Compiler
Run Code

Example

# Name of class is ‘Point’

class Point

# Class variable
@@no_of_points = 4

end
You can also try this code with Online Ruby Compiler
Run Code

The keyword "class" creates a new constant to refer to the class in addition to defining a simple class in ruby where the name of the class and constant is the same.

Instantiating a Point🎯

The most crucial part of ruby is classes and objects. We can also create objects easily like classes. We can create multiple objects from one class as well. We use a new method to create objects in ruby.

Syntax

Object_name = class_name.new

Example

class Point

@@no_of_points =4

end

Object1 = Point.new
Object2 = Point.new 
You can also try this code with Online Ruby Compiler
Run Code

Here we use the unique " new " method predefined in the Ruby library. ‘Object1’ and ‘Object2’ are two objects that we created for class ‘Point’.

As we have not defined any method for the class ‘Point’, we can not do anything with newly created Point objects. We have stored in local variables ‘Object1’ and ‘Object2’. But we can ask what kind of object the new object is by using the following syntax.

Object1.class   # Ans: Point
Object1.is_a? Point # Ans: true

# Similarly for Object2
Object2.class   # Ans: Point
Object2.is_a? Point # Ans: true
You can also try this code with Online Ruby Compiler
Run Code

Initializing a Point👇🏻

Initializing Point Without Parameters

Methods are called member functions in ruby. We define every method using the keyword "def" followed by the method name. We write the name of the method in lowercase and the methods terminates with the keyword "end".

Syntax

def method_name
    # Add the code to be executed
end

Example

class Point
def ninja
    var1=9
puts "Hello Ninjas!"
puts var1
end
end
Object1 = Point.new
Object1.ninja
You can also try this code with Online Ruby Compiler
Run Code

Output

Hello Ninjas!
9

Initializing Point With Parameters

We can use as many parameters as possible to a new method to initialize the class variables. It is important to declare and initialize the method while passing parameters to the new method at the time of class creation. When the new method is called with parameters, the initialize method executes.

Example

class Point 
    def initialize (var1, var2) # var1 and var2 are local variables
@var1, @var2 = var1, var2 # @var1 and @var2 are instance variables that are assigned with values of local variables var1 and var2
puts "var1: #@var1"
puts "var2: #@var2"
    end 
    end
    Object1 = Point.new(3,4)
You can also try this code with Online Ruby Compiler
Run Code

Output

var1: 3
var2: 4

So whenever a new object is created initialize method is called. Here initialize method is behaving like a constructor. The object invokes initialize method when it is declared, the initialize method automatically becomes private. Because of this, we can not explicitly call the initialize method by ourselves, and only objects can call initialize method automatically.

Defining to_s Method🥸

A ‘to_s’ instance method should be for any class you have defined. This instance method is used to return a string representation of the object. When debugging, this ability proves invaluable. Following is an example of how we will do it for the Point class.

Example

class Point
    def initialize(var1,var2)
@var1, @var2 = var1, var2
    end
    def to_s # The string that represents this point is returned
"(#@var1,#@var2)" # Here, we are interpolating the instance variables into a string
    end
end
You can also try this code with Online Ruby Compiler
Run Code

Now with this new method defined, we can create points and then print them

Object1 = Point.new(4,5) # A new Point class object is created here
puts Object1 # print "(4,5)"
You can also try this code with Online Ruby Compiler
Run Code

So here, we have completed everything about defining a simple class in Ruby. We hope you learned something from this blog! 😁

Frequently Asked Questions

What is ruby?

Ruby is a powerful yet simple object-oriented programming language. It is good at text processing. Ruby can be used to write servers and experiment with prototypes. It has simple syntax, can handle exceptions, etc.

What are all the editor which provides support for ruby?

Following are some of the editors that provide support for Ruby- Emacs or XEmacs, Vim, Jedit, Nedit.

Can we repeat the definition of class in ruby?

Yes, we can define a class repeatedly in ruby as each definition is added to that particular class's last definition. 

How are class variables defined in ruby?

We define class variables by adding two "at" signs (@@) before the variable's name.

What is a singleton class in ruby?

It is an anonymous class created by subclassing the class associated with a particular object.

Conclusion 🙋🏻‍♀️

This blog covered the topic of defining a simple class in Ruby. Furthermore, we learned how to create the class, instantiate, initialize a Point, and define a to_s Method. 
If you would like to learn more, check out our articles on-

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Coding Ninjas

Happy Learning Ninja! 🥷

Live masterclass