Ruby | Methods

Method is a collection of statements that perform some specific task and return the result. Methods allow the user to reuse the code without retyping the code. Methods are time savers and help the user to reuse the code without retyping the code.

Defining & Calling the method: In Ruby, the method defines with the help of def keyword followed by method_name and end with end keyword. A method must be defined before calling and the name of the method should be in lowercase. Methods are simply called by its name. You can simply write the name of method whenever you call a method.

Syntax:

def method_name
# Statement 1
# Statement 2
.
.
end

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the defining 
# and calling of method
  
#!/usr/bin/ruby
  
# Here geeks is the method name
def geeks
  
# statements to be displayed
puts "Welcome to GFG portal"
  
# keyword to end method
end
  
# calling of the method
geeks

chevron_right


Output:



Welcome to GFG portal


Passing parameters to methods: In Ruby, parameter passing is similar to other programming language’s parameter passing i.e simply write the parameters in the brackets ().

Syntax:

def method_name(var1, var2, var3)
# Statement 1
# Statement 2
.
.
end

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the parameter
# passing to methods
  
#!/usr/bin/ruby
  
# geeks is the method name
# var1 and var2 are the parameters
def geeks (var1 = "GFG", var2 = "G4G")
  
     #  statements to be executed
     puts "First parameter is #{var1}"
     puts "First parameter is #{var2}"
end
  
# calling method with parameters
geeks "GeeksforGeeks", "Sudo"
  
puts ""
  
puts "Without Parameters"
puts ""
  
# calling method without passing parameters
geeks

chevron_right


Output:

First parameter is GeeksforGeeks
First parameter is Sudo

Without Parameters

First parameter is GFG
First parameter is G4G


Variable Number of Parameters: Ruby allows the programmer to define a method that can take the variable number of arguments. It is useful when the user doesn’t know the number of parameters to be passed while defining the method.

Syntax:

def method_name(*variable_name)
# Statement 1
# Statement 2
.
.
end

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate the method 
# that takes variables number of arguments
  
#!/usr/bin/ruby
  
# defining method geeks that can 
# take any number of arguments
def geeks (*var)
      
   # to display the total number of parameters
   puts "Number of parameters is: #{var.length}"
     
   # using for loop
   for i in 0...var.length
      puts "Parameters are: #{var[i]}"
   end
end
  
# calling method by passing 
# variable number of arguments
geeks "GFG", "G4G"
geeks "GeeksforGeeks"

chevron_right


Output:

Number of parameters is: 2
Parameters are: GFG
Parameters are: G4G
Number of parameters is: 1
Parameters are: GeeksforGeeks


Return statement in Methods: Return statement used to returns one or more values. By default, a method always returns the last statement that was evaluated in the body of the method. ‘return’ keyword is used to return the statements.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate method return statement
  
#!/usr/bin/ruby
  
# geeks is the method name
def num
  
# variables of method
a = 10
b = 39
  
sum = a + b
  
# return the value of the sum
return sum
  
end
  
# calling of num method
puts "The result is: #{num}"

chevron_right


Output:

The result is: 49


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.