Ruby Methods


Ruby methods are used to bundle one or more repeatable statements into a single unit.

Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.

Methods should be defined before calling them otherwise Ruby will raise an exception for undefined method invoking.

 

Syntax:

      def method_name [( [arg [= default]]...[, *arg [, &expr ]])]
         expr..
      end
      

So you can define a simple method as follows:

      def method_name 
         expr..
      end
      

You can represent a method that accepts parameters like this:

      def method_name(var1, var2)
         expr..
      end
      

You can set default values for the parameters which will be used if method is called without passing required parameters:

      def method_name(var1=value1, var2=value2)
         expr..
      end
      

Whenever you call the simple method, you write only the method name as follows:

      method_name
      

However, when you call a method with parameters, you write the method name along with the parameters, such as:

      method_name 25, 30
      

The most important drawback to using methods with parameters is that you need to remember the number of parameters whenever you call such methods. For example, if a method accepts three parameters and you pass only two, then Ruby displays an error.

Example:

      def test(a1="Ruby", a2="Perl")
         puts "The programming language is #{a1}"
         puts "The programming language is #{a2}"
      end
      test "C", "C++"
      test
      

This will produce following result:

      The programming language is C
      The programming language is C++
      The programming language is Ruby
      The programming language is Perl
      

Returning Values from Methods:

Every method in Ruby returns a value by default. This returned value will be the value of the last statement evaluated. For example:

      def test
         i = 100
         j = 10
         k = 0
      end
      

This method, when called, will return the last declared variable k.


Ruby return Statement:

The return statement in ruby is used to return one value from a Ruby Method.

Syntax:

      def sample1
        return
      end
          
      def sample2
        return 12
      end
      
      def sample3      
        return 1,2,3
      end
      

If more than one expressions is given, an array containing these values will be the return value. If no expression given, nil will be the return value.

Example:

      def sample
         return 'A', 3+2, [4,3,2]
      end
      puts sample.inspect  # ["A", 5, [4, 3, 2]]
      

Have a look at this example:

      def test
         i = 100
         j = 200
         k = 300
      return i, j, k
      end
      var = test
      puts var.inspect
      

This will produce following result:

      [100, 200, 300]
      

Variable Number of Parameters:

Suppose that you declare a method that takes two parameters. Whenever you call this method, you need to pass two parameters along with it.

However, Ruby allows you to declare methods that work with a variable number of parameters. Let us examine a sample of this:

      def sample(*test)
      	puts "The number of parameters given is #{test.size}"
      	puts "The parameters are:"
      	test.each do |parameter|
      		puts parameter.to_s
      	end
      end
      sample "Zara", "6", "F"
      sample "Mac", "36", Time.now, [1, 2, 3, 4]
      

In this code, you have declared a method sample that accepts one parameter test. However, this parameter is a variable parameter. This means that this parameter can take in any number of variables. So the above code will produce following result:

      The number of parameters given is 3
      The parameters are:
      Zara
      6
      F
      The number of parameters given is 4
      The parameters are:
      Mac
      36
      Mon May 31 02:00:00 -0300 2010
      1234
      

Class Methods:

When a method is defined outside of the class definition, the method is marked as private by default. On the other hand, the methods defined in the class definition are marked as public by default. The default visibility and the private mark of the methods can be changed by public or private of the Module.

Whenever you want to access a method of a class, you first need to instantiate the class. Then, using the object, you can access any member of the class.

The exception to this is the "imediate value" classes like: Fixnum, Bignum, etc.


Ruby alias Statement:

This assigns an alias to methods or global variables. The aliase of the method keep the current definition of the method, even when methods are overridden.

Making aliases for the numbered global variables ($1, $2,...) is prohibited. Overriding the builtin global variables may cause serious problems.

Synatx:

      alias new_name old_name
      

Example:

      alias foo bar
      alias $MATCH $&
      

Here we have defined foo alias for bar and $MATCH is an alias for $&


Ruby undef Statement:

This cancels the method definition. An undef can not appear in the method body.

Synatx:

      undef method_name
      

Example:

To undefine a method called bar do the following:

      undef bar