Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - METHOD


A Method in Ruby is a chunk of Code that is used to conduct a particular task. And that chunk is only executed when it is called.


Say if you want to add two numbers, you can have an add Method that would be dedicated to add the numbers.


Similarly, if you want to divide two numbers, you can have a divide Method that would divide the numbers.


So, instead of placing all the codes in a single place. You can distribute the work among different Methods. So, that your code looks more structured and clean.


How a Method in Ruby is created?


Below are the rules to create a Method :

  1. A Method should begin with a def keyword - (i.e. def).

  2. Following the def keyword, a Method should have a name - (i.e. def my_method).

  3. And the name of the Method can/cannot have a starting and ending bracket () - (i.e. def my_method()).

  4. There can be(Or cannot be) something called as Arguments inside the bracket () - (i.e. def my_method(myarg)).

  5. As said, a Method is a chunk of Code used to conduct a task. Then starts the block of code.

    def my_method(myarg)
    	puts "This is my first Method")

  6. Then the method should end with the end statement.

    def my_method(myarg)
    	puts "This is my first Method")
    end

And thats how we define a Method.


But it was mentioned in the Method description that a Method only executes only when it is called.


So, how a Method is called?


Let us see in the below example.


Note : Let us omit the argument for now.

Example :



def my_method()
   	puts "This is my first Method")
end   	
   	
my_method()


Output :



  This is my first Method

So, in the above code, there are two parts,

  1. The Method definition.

    def my_method()
    	puts "This is my first Method")
    end

  2. And the Method, call,

    my_method()

So, in the above code we have defined a Method, my_method(). You can give any name to the Method(In this case we have given the name my_method(). Just don't forget to write the def keyword before it.


def my_method()
	puts "This is my first Method")
end
java_Collections


And the work of the Method, my_method() is, just to print This is my first Method.


Now, just remember one thing, the above Method, my_method() will never execute until it is called.


And thus we have called the Method, my_method() in the next line.


my_method()

And the Method, my_method() is called, printing,


This is my first Method

Although, the Method, my_method() is defined in the first line in the code.


def my_method()
	puts "This is my first Method")
end

But it will never be executed. Ruby will ignore the the above Method definition.


And come to the next line, where it finds the Method, my_method() is called.


my_method()

And only then the Method executes.


Just remember that the brackets() after the method is not not mandatory. It is upto you. If you want to place a bracket, it's fine. If you don't specify, it's also fine.


Let us see the method in the below example without brackets.


Example :



def my_method
   	puts "This is my Method without brackets"
end   	
   	
my_method


Output :



  This is my Method without brackets

Now, let us look at another example of adding two numbers and returning the result using Methods.


Example :



def add(first_number, second_number)
   	result = first_number + second_number
   	return result
end   	

first_num = 5
second_num = 4   	
value = add(first_num, second_num)
puts "The added result is #{value}"


Output :



  The added result is 9

Let us explain the above example with a practical scenario.


Say you are given a task of adding two numbers. And you would follow the below steps to achieve it :

  1. You would ask the First Number and Second Number from the person who has given you this task.

    And the above Ruby Method expects the same thing.

    def add(first_number, second_number)


    Here, first_number and second_number are the two numbers which a Ruby Method expects just like you(first_number and second_number are called as arguments that we will be learning in a separate tutorial).

    And add is the name of the Method. And just like the above example we have used the def keyword to define a Method.

  2. The next thing you would do is, add the numbers and give the added value to the caller.

    Same thing is done by the Above Method.

    It adds first_number and second_number and stores the added value in a variable called result.

    result = first_number + second_number


    It then returns the result to the caller(We will see next, who is a caller).

    return result

So far, we have seen how a Method works. Now, let us see who is the caller?


When the program execution begins, at first Ruby comes to the 5th line (As Ruby ignores the Method unless it is called).

java_Collections

And we initialise the first variable first_num with the value 5.


first_num = 5
java_Collections


Then in the next line, we have initialised the second variable second_num with the value 4.


second_num = 4
java_Collections


Then we call the add(first_num, second_num) Method.


value = add(first_num, second_num)

And Ruby searches for a Method with two arguments.


When it finds the Method, it assigns the value of first_num to first_number and second_num to second_number.

java_Collections
java_Collections
java_Collections

And gets into the block of the def add(first_number, second_number): Method.

java_Collections

This is where the numbers are added,


result = first_number + second_number

And the result is stored in a variable result.

java_Collections

And in the next line we return the added value(i.e. result).


return result

And the returned value goes back to the line, where the Method was called.


value = add(first_num, second_num)

And the variable value gets the added result(i.e. 9) from the variable result.

java_Collections

And in the next line the added value is printed as output.


puts "The added result is #{value}"

Nested Method


Nested Method is a Method inside a Method.


Let us see Nested Method in the below example.


Example :



def outer_method()
	def inner_method()
		puts "We are inside the inner Method"
	end
	inner_method()
end	
	
outer_method()


Output :



  We are inside the inner Method

So, in the above code, we have defined two Methods outer_func() and inner_func(). Since, a Method only executes only when it is called. The control comes to the fifth line directly, where the outer_func() is called.

java_Collections

And the control goes to the outer_method() for execution.


def outer_method()
	def inner_method()
		puts "We are inside the inner Method"
	end
inner_method()
end

And the control finds a Method named inner_method() defined inside the outer_method().


def inner_method()
	puts "We are inside the inner Method"
end

Now, the Method inner_method() doesn't gets executed but goes to the next line, where the call to inner_method() is made.


inner_method()

And then the Method inner_method() executes.


def inner_method()
	puts "We are inside the inner Method"
end

Printing the below output.

Output :



  We are inside the inner Method