Updated April 3, 2023
Introduction to Ruby Methods
In Ruby to accomplish any set of the task or to write a certain set of expression (It may be for calculation of the percentage of number or to finding addition for the two numbers), we use the method, the method returns a value after performing its specific task which we have written inside it in the form of expression, to create any method in Ruby we use the def command (def add or def percentage) and we can pass arguments to these methods after the method names like (def add (a,b)), after writing all the expressions we need to end the method by writing end at the bottom of the method.
How does Methods Work in Ruby
Below is the syntax of method and from below syntax we will do little discussion step by step for the working of the method in Ruby:
- When we write code def and name of the method it means we are instructing the system about the method, the system takes it as the instruction for defining a method.
- After the name of the method we are passing the parameters, here parameter can be on or multiple like def method example (a,b,c). Hence we are giving instructions to the system about the number of parameters.
- Next thing we are going to perform some activity inside the function, this activity can be anything like addition, multiplication or any other mathematical or logical calculations.
- Whatever we will write at the end of the method expression it will be returned, so do not require to return anything manually.
- And the step of closing of the method, for this we use the end command at the end of the method. Once the Ruby compiler reads the end it understands that this is the end of the method.
- Finally the time comes to call the method which we have defined here, to call the method we simply use the name of the method and by doing this simply we are calling the address where the method is available and it will perform all the related operations.
Code:
#Start of the method with parameters
def methodExample(parameters)
#Here we perform the operation or the task which we needed #No need to return anything as in it default return the last line of the expression .
end
#Calling of the method methodExample
methodExample
How to define Methods?
To define any method we will use the def command here, there can be two types of method one is the normal method and another one is the class-based method.
Let’s discuss both the types here:
1. Without class and params based method
Here in the below example we are working on the method which does not belong to any class and also they are not getting any params or arguments in the method call. We can explain the below example in the following steps. We have defined a method without any params and inside the method we are printing some messages for the general. In the next step we are calling this method with its name which we have defined. Once we call the method with its name it will print all message and once it reads the end command it will close the method. Please see the below example along with a screen of output.
Code:
def greetingForAll
puts "Welcome to all of in the Ruby programing world"
puts "This example is for the method without params and class"
end
greetingForAll
Output:
2. With Class and without params Method
Here in the below example we are working on the method which belongs to the Student class and they are not getting params or arguments in the method call. We can explain the below example in the following steps :
We defined a class with the name of the Student. Inside the class Student we have defined a method without any params and inside the method we are printing some messages for the general. In the next step we are calling this method with the object which we have created from the Student class (greeting for all). Once we call the method with its name it will print all message and once it reads the end command it will close the method. Please see the below example along with a screen of output.
Code:
class Student
def greetingForAll
puts "Welcome to all of in the Ruby programing world"
puts "This example is for the method without params and class"
end
end
studentObjec =Student.new
studentObjec.greetingForAll
Output:
How to define Methods with Parameters?
Below are the methods:
1. Method without class and with params
Here in the below example we are working on the method which does not belong to any class and it is getting params or arguments in the method call. We can explain the below example in the following steps:
We have defined a method with some params and inside the method we are printing some messages for the students’ details passed in the form of params. In the next step we are calling the method with the name which we have defined and passing required params to it. Once we call the method with its name it will print all messages related to a specific student passed in the form of params and once it reads the end command it will close the method. Please see the below example along with a screen of output.
Code:
def greetingForAll (name ,designation)
puts "Welcome to organisation mr #{name} you are working here as #{designation}"
end
greetingForAll("ranjan","Software Engineer")
greetingForAll("Ajat","Civil Engineer")
Output:
2. The method with class and with params
Here in the below example we are working on the method which belongs to the Student class and it is getting params or arguments in the method call. We can explain the below example in the following steps:
We defined a class with the name of the Student. Inside the class Student we have defined a method with some params as the student information. In the next step we are calling this method with the object which we have created from the Student class (greeting for all(passing params)). Once we call the method with its name on the object we created it will print all messages related to passed params for the student. Please see the below example along with a screen of output.
Code:
class Student
def greetingForAll (name ,designation)
puts "Welcome to organisation mr #{name} you are working here as #{designation}"
end
end
studentObject = Student.new
studentObject.greetingForAll("ranjan","Software Engineer")
studentObject.greetingForAll("Ajat","Civil Engineer")
Output:
Conclusion
From this tutorial we learned about the concept of method in Ruby, we learned about various types of method used in the Ruby-like method with params, with class and the method without params without class, we have also given little focus on the working and execution steps for the Methods.
Recommended Articles
We hope that this EDUCBA information on “Ruby Methods” was beneficial to you. You can view EDUCBA’s recommended articles for more information.