Methods in Ruby

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:

  1. # defining a method
  2. # method start with def followed by method's name
  3. def method_name
  4. # code inside method
  5. puts "code inside the method"
  6. # method ends with keyword end
  7. end

The code inside the method executes when we call this method using its name.

  1. # defining a method
  2. # method start with def followed by method's name
  3. def method_name
  4. # code inside method
  5. puts "code inside the method"
  6. # method ends with keyword end
  7. end
  8. # calling a method using its name
  9. method_name

Output:

  1. code inside the method

Methods with parameters:

Sometimes methods prerequire some extra information from outside. This parameters are used for it.

  1. # method with parameters
  2. def method_name(age, name)
  3. # code inside method
  4. puts "your name is #{name} and your age is #{age}"
  5. # method ends with keyword end
  6. end
  7. # calling a method using its name and agruments
  8. method_name(20, 'harish')
  9. # another call
  10. method_name(18,'your_name')

Output:

  1. your name is harish and your age is 20
  2. 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.
  1. # method with default parameters values
  2. def method_name(country = 'INDIA', age, name)
  3. # code inside method
  4. puts "your name is #{name} from #{country} and your age is #{age}"
  5. # method ends with keyword end
  6. end
  7. # calling a method using its name and agruments
  8. method_name(20, 'harish')
  9. # another call
  10. method_name(18,'your_name')
  11. # passing all arguments
  12. method_name('CHINA',18,'myname')

Output:

  1. your name is harish from INDIA and your age is 20
  2. your name is your_name from INDIA and your age is 18
  3. your name is myname from CHINA and your age is 18

Return type methods:

Methods can return some data after execution.

  1. # defining a global varible which is accessible in whole file
  2. $value = 0
  3. # method with default parameters values
  4. def method_name(country = 'INDIA', age, name)
  5. # code inside method
  6. puts "your name is #{name} from #{country} and your age is #{age}"
  7. # method ends with keyword end
  8. $value+=1
  9. # return the data using keyword return
  10. return $value
  11. end
  12. # printing returned data
  13. puts method_name(20, 'harish')
  14. # another use
  15. myvar = method_name(18,'your_name')
  16. puts myvar

Output:

  1. your name is harish from INDIA and your age is 20
  2. 1
  3. your name is your_name from INDIA and your age is 18
  4. 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.

  1. def sum(*values)
  2. result = 0
  3. for v in values
  4. result+=v
  5. end
  6. return result
  7. end
  8. # sending variable arguments
  9. puts sum(2,334,4,23,12)
  10. puts sum(32,43,4,32)
  11. puts sum(23,3)

Output:

  1. 375
  2. 111
  3. 26



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel