Ruby methods
Ruby methods are similar to functions in other programming languages. Ruby method for bundling one or more repeated statements into a unit.
Method names should start with a lowercase letter. If you start a method name with an uppercase letter, Ruby may treat it as a constant, causing the call to be parsed incorrectly.
Methods should be defined before calling, otherwise Ruby will generate 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. If the required parameters are not passed when the method is called, the default values will be used:
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 shown below:
method_name
However, when you call a method with parameters, you also need to bring the parameters when writing the method name, for example:
method_name 25, 30
The biggest disadvantage of using methods with parameters is when calling the method Need to remember the number of parameters. For example, if you pass only two parameters to a method that accepts three parameters, Ruby will display an error.
Example
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test(a1="Ruby", a2="Perl") puts "编程语言为 #{a1}" puts "编程语言为 #{a2}" end test "C", "C++" test
The output result of the above example is:
编程语言为 C 编程语言为 C++ 编程语言为 Ruby 编程语言为 Perl
Return value from method
Ruby Every method returns a value by default. The value returned is the value of the last statement. For example:
def test i = 100 j = 10 k = 0 end
When this method is called, the last declared variable k will be returned.
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 those values will be the return value. If no expression is given, nil will be the return value.
Example
return 或 return 12 或 return 1,2,3
Look at the following example:
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test i = 100 j = 200 k = 300 return i, j, k end var = test puts var
100 200 300Variable number of parametersSuppose you declare a method with two parameters. When you call the method, you also need to pass two parameters. However, Ruby allows you to declare methods with a variable number of parameters. Let's look at the following example:
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def sample (*test) puts "参数个数为 #{test.length}" for i in 0...test.length puts "参数值为 #{test[i]}" end end sample "Zara", "6", "F" sample "Mac", "36", "M", "MCA"
参数个数为 3 参数值为 Zara 参数值为 6 参数值为 F 参数个数为 4 参数值为 Mac 参数值为 36 参数值为 M 参数值为 MCAClass methodWhen the method is defined outside the class, the method is marked
private by default. On the other hand, if the method is defined in a class, it is marked public by default.
The default visibility andprivate flag of a method can be changed through the module's public or private
. When you want to access the methods of a class, 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's see how to declare and access class methods:class Accounts def reading_charge end def Accounts.return_date end endWe already know how the method return_date is declared. It is declared by following the class name with a period, followed by the method name. You can access the class method directly as follows:
Accounts.return_dateTo access this method, you do not need to create an object of class Accounts. ###
Ruby alias Statement
This statement is used to alias a method or global variable. Aliases cannot be defined within a method body. A method's alias maintains the method's current definition even if the method is overridden.
Aliasing numbered global variables ($1, $2,...) is prohibited. Overriding built-in global variables can cause serious problems.
Syntax
alias 方法名 方法名 alias 全局变量 全局变量
Example
alias foo bar alias $MATCH $&
Here, we have defined an alias for bar as foo and an alias for $& as $MATCH.
Ruby undef Statement
This statement is used to undefine the method. undef cannot appear within the method body.
By using undef and alias, the interface of a class can be modified independently from the parent class, but please note that it may break the program when calling methods within itself .
Syntax
undef 方法名
Examples
The following example cancels the definition of a method named bar:
undef bar