Table of contents
1.
Introduction
2.
What are Ruby Methods
3.
Defining Method
4.
Methods Naming 
5.
Arguments
6.
Default Arguments
7.
Call a Method
7.1.
With parentheses
7.2.
Without parentheses
8.
Overriding 
9.
Frequently Asked Questions
9.1.
How do you define Ruby Methods?
9.2.
How to use Ruby methods.
9.3.
What are some fundamental naming conventions for methods?
9.4.
When calling methods with or without parenthesis, what is the syntax?
9.5.
What is Method overriding in Ruby?
10.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Methods in Ruby

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

Introduction

We'll learn about Ruby Methods, how to name them, and how to call them with and without parenthesis in this blog. We'll start with the basics and work our way up to Ruby Methods.

Source: educba.com

Also Read, procedure call in compiler design

What are Ruby Methods

In Ruby, a method is a collection of expressions that returns a value.

Ruby methods make it so that we don't have to write the same code in a program over and over again. It's a collection of expressions that produce a result. You can break down your code into subroutines that can be called from anywhere in the program within a method.

Methods in Ruby are equivalent to functions in other programming languages. They combine one or more phrases that are repeated into a single bundle.

Defining Method

We must first define a method before we can utilize it. The def keyword is used to define a Ruby method, followed by the method name. Finally, we must use the end keyword to indicate that the Method has been declared.

Syntax:

Def methodName
    Code…………………
End
You can also try this code with Online Ruby Compiler
Run Code

 

Now that we've grasped the syntax of methods let's talk about some naming conventions.

Methods Naming 

The name of a method should always begin with a lowercase alphabet. Or else, it may be mistaken for a constant.

It might include letters, numbers, a low line, an underscore, or a character. In a multiword method name, there is a convention in which underscores are used to divide the words.

  • A! (exclamation mark), a? (question mark), or a= equals sign can be used at the end of a method name.
  • The bang methods (! at the end of the method name) are invoked and performed in the same way as any other method.
  • By convention, methods that conclude in a question mark return boolean. However, they don't necessarily return true or false; instead, they frequently return an object to signify a true value.
  • The equals sign at the end of a method indicates that it is an assignment method. The return value of assignment methods is disregarded, and the arguments are returned alternatively.

Arguments

A method in Ruby can take arguments.

  • The method name is followed by a list of arguments.
  • The use of parentheses around the arguments is not necessary.
  • A comma is used to separate multiple arguments.
  • The arguments are positional.

 

For example: -

Def add_values(num1, num2)
    Return num1 + num2
End
You can also try this code with Online Ruby Compiler
Run Code

 

When calling the Method in the example above, you must give two parameters. The arguments are local variables within the method body. After that, the procedure will take two parameters and return the result. If you feed this method 10 and 30, it will return 40.

When defining methods, you may wish to structure them so that they always work, regardless of whether or not they are provided arguments. With default arguments, this is achievable.

Default Arguments

Argument default values can be specified. The default value does not have to come first;      rather, default arguments must be grouped collectively.

Def add_values(num1,num2= 10)
 num1 + num2
End
You can also try this code with Online Ruby Compiler
Run Code

 

We've learned about arguments and default arguments so far. Let's look at how to call methods now.

Call a Method

Methods that belong to (are defined on) objects in Ruby can be used (called) by preceding the method name with a dot, as in:

Object.method
You can also try this code with Online Ruby Compiler
Run Code

 

There are two ways to call a method, 

  1. With parentheses
  2. Without parentheses

 

You can use the following syntax to call methods with parentheses:

UserMethod()
You can also try this code with Online Ruby Compiler
Run Code

 

You can use the following syntax to call methods without parentheses:

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

 

Let's understand more with an example:- 

With parentheses

Def add_values(num1, num2)
   Return num1 + num2
End 
puts(add_values(10, 20))
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

30

 

In this example, we used the parenthesis approach to combine two integers, resulting in an output of 30.

Without parentheses

Def print_string()
   puts(“Without parentheses”)
end
 print_string() 
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

Without parentheses

 

We utilized a straightforward way (without parenthesis) in this example and obtained the statement we wanted as a result which is “ Without parentheses” in this example.

Overriding 

Ruby merely redefines the def keyword when it is used (whether the Method already exists or not). This is referred to as overriding.

Let's understand it with an example.

def language
 puts("We are doing Integration ")
end

def language
 puts("We are doing Probability ")
end

# Now call the Method
language
You can also try this code with Online Ruby Compiler
Run Code

 

Output:

We are doing Probability

 

In this example, the method from the corresponding class came into action, which means that one overrode the other. If a method in the superclass has the same name as a method in its subclass, then running the method will execute the method of the corresponding class, Which is “ We are doing Probability” in this case.

Frequently Asked Questions

How do you define Ruby Methods?

The Ruby approach prevents us from retyping the same code in the software. Methods in Ruby are equivalent to functions in other programming languages.

How to use Ruby methods.

We must first define a Ruby method before we can utilize it. The keywords def and end are used to define it. The name of the Method should always begin with a lowercase letter.

What are some fundamental naming conventions for methods?

The following are some fundamental naming conventions to keep in mind while naming:

  • The name of a method should always begin with a lowercase alphabet. Or else, it may be mistaken for a constant.
  • It might include letters, numbers, a low line, an underscore, or a character. In a multiword method name, there is a convention in which underscores are used to divide the words.

When calling methods with or without parenthesis, what is the syntax?

While calling methods with parentheses, we should use the following syntax:          

UserMethod()
You can also try this code with Online Ruby Compiler
Run Code

 

While calling methods without parentheses, we should use the following syntax:  

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

What is Method overriding in Ruby?

Method overriding simply means that two methods are declared inside the same scope and are utilized to do distinct tasks. This capability is available in an Object-Oriented Language with Inheritance support.

Overriding takes place in the manner that the Method of derived class or subclass replaces or overrides the implementation of the Derived class method.

Conclusion 

In this article, we learned about Ruby Methods, including what they are, how to name them, and how to call them with and without parenthesis. After reading about Ruby Methods, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Object Marshalling in RubyTainting Objects in Ruby, and Object References in Ruby.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Live masterclass