Ruby Methods
Ruby methods are similar to functions in other programming languages. Ruby methods are used to bundle one or more repeatable statements into a single unit.
Method names should start with a lowercase letter. If a method name starts with an uppercase letter, Ruby might treat it as a constant, leading to incorrect parsing of the call.
Methods should be defined before they are called, otherwise, Ruby will raise an undefined method call exception.
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 define a method that accepts parameters as follows:
def method_name (var1, var2)
expr..
end
You can set default values for parameters, which will be used if the required parameters are not passed when the method is called:
def method_name (var1=value1, var2=value2)
expr..
end
When you want to call a method, simply use the method name as follows:
method_name
However, when you call a method with parameters, you also need to include the parameters with the method name, for example:
method_name 25, 30
The biggest drawback of using methods with parameters is that you need to remember the number of parameters when calling the method. For example, if you pass only two parameters to a method that accepts three, Ruby will show an error.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
def test(a1="Ruby", a2="Perl")
puts "The programming language is #{a1}"
puts "The programming language is #{a2}"
end
test "C", "C++"
test
The output of the above example is:
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. For example:
Example
def test
i = 100
j = 10
k = 0
end
When you call this method, it will return the value of the last declared variable, k.
Ruby return Statement
The return statement in Ruby is used to return one or more values from a Ruby method.
Syntax
return [expr[`,' expr...]]
If more than two expressions are given, an array containing these values will be the return value. If no expression is given, nil will be the return value.
Example
return
or
return 12
or
return 1,2,3
Consider the following example:
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
def test
i = 100
j = 200
k = 300
return i, j, k
end
var = test
puts var
The output of the above example is:
100
200
300
Variable-Length Arguments
Suppose you declare a method with two parameters. When you call this method, you need to pass two parameters.
However, Ruby allows you to declare methods with variable-length arguments. Let's look at the following example:
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
def sample (*test)
puts "The number of arguments is #{test.length}"
for i in 0...test.length
puts "The argument value is #{test[i]}"
end
end
sample "Zara", "6", "F"
sample "Mac", "36", "M", "MCA"
In this code, you have declared a method sample that accepts a variable-length argument test. This means the method can accept a variable number of arguments. The output of the above example is:
The number of arguments is 3
The argument value is Zara
The argument value is 6
The argument value is F
The number of arguments is 4
The argument value is Mac
The argument value is 36
The argument value is M
The argument value is MCA
Class Methods
When a method is defined outside of a class, it is marked as private by default. On the other hand, if a method is defined within a class, it is marked as public by default.
The default visibility and the private marker can be changed by the public or private methods of the Module.
To access a class method, you first need to instantiate the class. Then, using the object, you can access any member of the class.
Ruby provides a way to access methods without instantiating a class. Let's see how to declare and access class methods:
class Accounts
def reading_charge
end
def Accounts.return_date
end
end
We have seen how the method return_date is declared. It is declared by following the class name with a dot, followed by the method name. You can access the class method directly as follows:
Accounts.return_date
You do not need to create an object of the class Accounts to access this method.
Ruby alias Statement
This statement is used to alias a method or a global variable. Aliases cannot be defined within the method body. Even if the method is overridden, the alias remains the current definition of the method.
Aliasing numbered global variables ($1, $2, ...) is prohibited. Overriding built-in global variables may cause serious problems.
Syntax
alias method_name method_name
alias global_variable global_variable
Example
alias foo bar
alias $MATCH $&
Here, we have defined an alias foo for bar and an alias $MATCH for $&.
Ruby undef Statement
This statement is used to cancel a method definition. undef cannot appear inside the method body.
By using undef and alias, the interface of a class can be modified independently from the superclass, but be aware that it may break the program if method calls within it are made.
undef method_name
Example
The following example cancels the method definition for bar:
undef bar