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

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.