Ruby Methods
Ruby methods are similar to functions in other programming languages. Ruby methods are used to bundle one or more repeated statements into a single unit.
A method name should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby may treat it as a constant, causing the call to be parsed incorrectly.
A method should be defined before it is called, otherwise Ruby will produce 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 are used if a required argument is not passed when the method is called:
def
method_name
(
var1
=
value1
,
var2
=
value2
)
expr
..
end
When you want to call a method, you only need to use the method name, as follows:
method_name
However, when you call a method with parameters, you need to include the arguments when writing the method name, for example:
method_name
25
,
30
The biggest drawback of using methods with parameters is that you must remember the number of arguments when calling the method. For example, if you pass only two arguments to a method that accepts three, Ruby will display 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
Try it »
The above example produces the following output:
编程语言为 C
编程语言为 C++
编程语言为 Ruby
编程语言为 Perl
Returning Values from Methods
Every method in Ruby returns a value by default. This returned value is the value of the last statement. For example:
Example
def
test
i
=
100
j
=
10
k
=
0
end
When calling this method, the last declared variable k will be returned.
Ruby return Statement
In Ruby, the return statement 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
Look at 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
Try it »
The above example produces the following output:
100
200
300
Variable Number of Arguments
Suppose you declare a method with two parameters; when you call the method, you also need to pass two arguments.
However, Ruby allows you to declare methods with a variable number of arguments. Let us 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
..
test
.
length
puts
"
The argument value is #{test[i]}
"
end
end
sample
"
Zara
"
,
"
6
"
,
"
F
"
sample
"
Mac
"
,
"
36
"
,
"
M
"
,
"
MCA
"
Try it »
In this code, you have declared a method sample that accepts one parameter test. However, this parameter is a variable parameter. This means the parameter can take a variable number of arguments. The output of running the above example is:
参数个数为 3
参数值为 Zara
参数值为 6
参数值为 F
参数个数为 4
参数值为 Mac
参数值为 36
参数值为 M
参数值为 MCA
Class Methods
When a method is defined outside a class, the method is marked as privateby default. On the other hand, if a method is defined inside a class, it is marked as public by default.
The default visibility of methods and the private marking can be changed through the Module's public or private method.
When you want to access a class's methods, 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 instantiation. Let us see how to declare and access class methods:
class
Accounts
def
reading_charge
end
def
Accounts
.
return_date
end
end
We already know how the method return_date is declared. It is declared by following the class name with a dot, and the dot followed by the method name. You can access the class method directly, as follows:
Accounts
.
return_date
To access this method, you do not need to create an object of the class Accounts.
Ruby alias Statement
This statement is used to create an alias for a method or global variable. An alias cannot be defined within a method body. Even if a method is overridden, the alias of the method retains the method's current definition.
Creating aliases for 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 the alias foo for bar, and the alias $MATCH for $&.
Ruby undef Statement
This statement is used to cancel a method definition.undef cannot appear within a method body.
By using undef and alias, the class's interface can be modified independently from the parent class, but note that with internal method calls it may break the program.
undef
Method Names
Example
The following example cancels the method definition named bar:
undef
bar