Methods are set of statements which collectively perform some task. Methods are important because we can use the code inside the methods any time. In other words, these help us to make code more reusable. For example, in authentication system (like facebook) we may need code for login multiple times so we do not need to write code again and again. The code will be written once in a method and we will call this method for using the code.
Simple Methods:
The syntax of simple method is:
- # defining a method
- # method start with def followed by method's name
- def method_name
- # code inside method
- puts "code inside the method"
- # method ends with keyword end
- end
The code inside the method executes when we call this method using its name.
- # defining a method
- # method start with def followed by method's name
- def method_name
- # code inside method
- puts "code inside the method"
- # method ends with keyword end
- end
-
- # calling a method using its name
- method_name
Output:
- code inside the method
Methods with parameters:
Sometimes methods prerequire some extra information from outside. This parameters are used for it.
- # method with parameters
- def method_name(age, name)
- # code inside method
- puts "your name is #{name} and your age is #{age}"
- # method ends with keyword end
- end
-
- # calling a method using its name and agruments
- method_name(20, 'harish')
-
- # another call
- method_name(18,'your_name')
Output:
- your name is harish and your age is 20
- your name is your_name and your age is 18
NOTE:
- These parameters are the local variables of the methods and can only be accessible inside the methods only.
- These parameters can have default value.
- # method with default parameters values
- def method_name(country = 'INDIA', age, name)
- # code inside method
- puts "your name is #{name} from #{country} and your age is #{age}"
- # method ends with keyword end
- end
-
- # calling a method using its name and agruments
- method_name(20, 'harish')
-
- # another call
- method_name(18,'your_name')
-
- # passing all arguments
- method_name('CHINA',18,'myname')
Output:
- your name is harish from INDIA and your age is 20
- your name is your_name from INDIA and your age is 18
- your name is myname from CHINA and your age is 18
Return type methods:
Methods can return some data after execution.
- # defining a global varible which is accessible in whole file
- $value = 0
-
- # method with default parameters values
- def method_name(country = 'INDIA', age, name)
- # code inside method
- puts "your name is #{name} from #{country} and your age is #{age}"
- # method ends with keyword end
- $value+=1
- # return the data using keyword return
- return $value
- end
-
- # printing returned data
- puts method_name(20, 'harish')
-
- # another use
- myvar = method_name(18,'your_name')
- puts myvar
Output:
- your name is harish from INDIA and your age is 20
- 1
- your name is your_name from INDIA and your age is 18
- 2
Note that return statement must be last line of the method.
Methods with variable parameters:
We can create such methods which take variable arguments.
- def sum(*values)
- result = 0
- for v in values
- result+=v
- end
- return result
- end
-
- # sending variable arguments
- puts sum(2,334,4,23,12)
-
- puts sum(32,43,4,32)
-
- puts sum(23,3)
Output:
- 375
- 111
- 26