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




RUBY - METHOD ARGUMENTS


Say, you want to pass some information to a Method.


For example, if you want a Method to add two numbers. You need to pass those two numbers to the Method.


Say, in the below example, you want the add Method to add two numbers for you. And you have passed those numbers in between the brackets () of the add Method(i.e. first_number and second_number).


And those elements inside the brackets () of the add Method is called as Parameters and the actual value passed to them is called as Argument.


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

So, the variables inside the add Method is called as Parameter.

java_Collections

And the actual values (i.e. 5 and 4) to be sent to the Method are the Arguments.


Throughout the tutorial we will be calling the Parameters as Arguments to avoid unnecessary confusion.


Now, what if, you have defined a Method with two Argument but made the Method call with one argument.


What if there is an Argument mismatch?


Now, what if, you have defined a Method with two Argument but made the Method call with one argument.


Example :



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

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


Output :



  :in add': wrong number of arguments (given 2, expected 3) (ArgumentError)
  from add.rb:8:in
<main>'

So, in the above code we have the add Method with three Arguments.


def add(first_number, second_number, third_number)
java_Collections


But when the add Method is called, it is called with just two Arguments.

java_Collections

And there is a mismatch of Arguments. And we end up with the below error.


:in add': wrong number of arguments (given 2, expected 3) (ArgumentError)
	from add.rb:8:in <main>'

Let us see in the next example, how to fix it.


Fixing Argument mismatch with *args


To solve the problem of Argument mismatch, we can just add an asterisk * in front of the Argument.


Let us see in the below example.


Example :



def add(*number)
   	result = number[0] + number[1]
   	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

So, to avoid the Argument mismatch problem. We have replaced the arguments first_number and second_number with *number.


'def add(*number)'

And no matter how many Arguments you pass, *number will make an intelligent guess and create a Array out of it.


Now, if you see the complete execution, we have initialised the first and second number in the variables, first_num and second_num.


first_num = 5
second_num = 4
java_Collections

java_Collections

And called the add Method, passing above two values(i.e. first_num and second_num) as Arguments.


value = add(first_num, second_num)

And the execution of add Method begins.


def add(*number)
	result = number[0] + number[1]
	return result
end

So, the arguments first_num and second_num gets assigned to *number.

java_Collections

And internally an Array is created for *number that has both the values of first_num and second_num.

java_Collections

And the variable number is an Array holding the values 5 and 4. And we know an Array can be accessed using indexes.


result = number[0] + number[1]

So, number[0] would access the first element of the Tuple (i.e. 5) and number[1] would access the second element of the Tuple (i.e. 4).


And the added value is returned to the caller.


return result

Default Argument


Say, you want to pass two Arguments to add two numbers. But what if you don't pass any 'Argument' while calling the Method. You would end up with an error.


Let us see in the below example, how can we avoid that error with Default Argument.


Example :



def add(first_number = 0, second_number = 0)
   	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}"
value = add()
puts "The added result is #{value}"


Output :



  The added result is 9
  The added result is 0

So, in the above code, you have initialised the variables, first_num and second_num with 5 and 4.


And called the add Method passing, first_num and second_num as Arguments.


value = add(first_num, second_num)

And the add Method executes, adding first_number and second_number returning the result.


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

And the added value is printed using the print statement.


puts "The added result is #{value}"

We can see that the arguments, first_number and second_number is initialised with 0, i.e. first_number = 0, second_number = 0. Which is called Default Arguments. It is ignored if Arguments are passed to it. But comes into effect when no values are passed.


Now, in the next Line, we have called the add Method.


value = add()

Without passing any Arguments.


Now, since the Default Arguments is there in place (i.e. first_number = 0, second_number = 0).


def add(first_number = 0, second_number = 0)

Even though no Arguments are passed, the default values first_number = 0 and second_number = 0 are initialised and returned.

java_Collections

And the added value is 0 now.


result = first_number + second_number

And 0 is returned,


return result

Can we pass an Array as Argument?


Yes, we can pass an Array as Argument.


Let us see with the below example, passing List as Argument.


Example :



def add(number_list)
   	result = number_list[0] + number_list[1] + number_list[2]
   	return result
end

numbers = [5, 7, 8]  	
value = add(numbers)
puts "The added result is #{value}"


Output :



  The added result is 20

So, we have created an Array with three values,


numbers = [5, 7, 8]

And we are calling the add Method passing the Array as argument.


value = add(numbers)

And the add Method executes, adding all the elements of the Array, returning the result.


return result

Method Argument without bracket


Once again, the brackets are not mandatory in a method. We can declare the below method without Argument.


Let us rewrite the add method without brackets.


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