Ruby – Method Invocation

Method invocation refers to how a method is called in a program. The process of invoking a method in Ruby is quite easy since the use of parenthesis is optional. The use of parenthesis plays an important role when the invocation is embedded inside an expression consisting of other function calls or operators.

The Ruby interpreter makes use of the process known as method lookup or method resolution. In this method, when the Ruby interpreter has the method’s name and the object on which it is to be invoked, it searches for the method definition.

A Method Invocation Expression Comprises of 4 Components:

  • The expression whose value is the object on which the method is call. Now, this expression is followed by either . or :: which is used to separate expression from the method name. This separator and expression is optional. If the separator and expression are absent, then method is invoked on the ‘self’.
  • The name of the method being call.
  • The parameter values is passed to the method. The list of comma-separated arguments can be passed to the method enclosed within parenthesis if the method contain more than one argument. These arguments are optional.
  • An optional block of code enclosed in curly braces or by a ‘do/end’ pair. This code can be invoked this block of code with the help of the ‘yield’ keyword.

Example 1: In this example, we invoke a method with no arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate 
# how to invoke a method 
example_str ='GeeksforGeeks'
  
# "length" invoked on object 
# example_str having no args
puts example_str.length   

chevron_right


Output:



13

Example 2: In this example, we invoke a method with one argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate 
# how to invoke a method 
  
# "sqrt" invoked on object 
# Math with one argument
number = Math.sqrt(36)     
puts "Square Root of 36:" 
puts number 

chevron_right


Output:

Square Root of 36:
6.0

Example 3: In this example, we’re going to define a method with two arguments. Then we will call this method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate 
# how to invoke a method 
def add(a, b)   
      
# ’add’ method definition
  a + b
end
  
# Calling method ‘add’
c = add(20, 30
d = add(45, 90
puts c 
puts d 

chevron_right


Output:

50
135

Example 4: In this example, we are going to invoke class methods.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate 
# how to invoke a method 
  
# ’Animal’ class definition
class Animal                                          
      
   # ’dog’ function definition
   # having no arguments
   def dog                                            
       puts "Dog is barking" 
   end
     
   # ’enternames’ function definition
   # having two arguments
   def  enternames(one, two)           
           puts one
           puts two
   end
end
  
# Create a new instance of Animal
animal = Animal.new   
  
# Instance method invocation
animal.dog 
  
# Instance method invocation 
# having two arguments
animal.enternames("Cat", "Elephant"
  
# Class method call using send method
animal.send :dog  
  
# Class method call using send method 
animal.send :enternames, "Cat", "Elephant"   

chevron_right


Output:

Dog is barking
Cat
Elephant
Dog is barking
Cat
Elephant



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.