May 12, 2021 Ruby
The Ruby method is similar to functions in other programming languages. The Ruby method is used to bundle one or more duplicate statements into a cell.
The method name should begin with a lowercase letter. If you start with a capital letter as the method name, Ruby might treat it as a constant, causing the call to be resolved incorrectly.
Methods should be defined before the call, otherwise Ruby will produce an undefined method call exception.
def method_name [( [arg [= default]]...[, * arg [, &expr ]])] expr.. end
So, you can define a simple approach, as follows:
def method_name expr.. end
You can define a way to accept parameters, as follows:
def method_name (var1, var2) expr.. end
You can set default values for parameters, which are 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, you only need to use the method name, as follows:
method_name
However, when you call a method with parameters, you write the method name with parameters, such as:
method_name 25, 30
The biggest disadvantage of using a method 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 parameters, Ruby displays an error.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test(a1="Ruby", a2="Perl") puts "编程语言为 #{a1}" puts "编程语言为 #{a2}" end test "C", "C++" test
This results in the following:
编程语言为 C 编程语言为 C++ 编程语言为 Ruby 编程语言为 Perl
Each method in Ruby returns a value by default. T he 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 is returned.
The return statement in Ruby is used to return one or more values from the Ruby method.
return [expr[`,' expr...]]
If more than two expressions are given, the array containing these values will be the return value. If no expression is given, nil will be the return value.
return 或 return 12 或 return 1,2,3
Take a look at the following example:
#!/usr/bin/ruby def test i = 100 j = 200 k = 300 return i, j, k end var = test puts varTry it out . . .
This results in the following:
100 200 300
Suppose you declare a method with two parameters, and when you call the method, you also need to pass two parameters.
However, Ruby allows you to declare methods where the number of parameters is variable. Let's look at the following example:
#!/usr/bin/ruby def sample (*test) puts "The number of parameters is #{test.length}" for i in 0...test.length puts "The parameters are #{test[i]}" end end sample "Zara", "6", "F" sample "Mac", "36", "M", "MCA"Try it out . . .
In this code, you have declared a method sample and accepted a parameter test. H owever, this argument is a variable argument. T his means that arguments can have a different number of variables. So the code above will produce the following results:
The number of parameters is 3 The parameters are Zara The parameters are 6 The parameters are F The number of parameters is 4 The parameters are Mac The parameters are 36 The parameters are M The parameters are MCA
When a method definition is outside the class definition, the method is marked private by default. O n the other hand, methods that define in a class definition are marked public by default. The default visibility and private tag of the method can be changed by the public or private of the Module.
When you want to access a class's methods, you first need to instantiate the class. Then, with objects, you can access any member of the class.
Ruby provides a way to access methods without instantiated classes. Let's look at how to declare and access class methods:
class Accounts def reading_charge end def Accounts.return_date end end
We already know how return_date methods are declared. I t 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
To access this method, you do not need to create an object for class Accounts.
This statement is used to alias a method or global variable. A lias cannot be defined within a method body. Even if the method is overrided, the alias of the method maintains the current definition of the method.
Global variable numbered ($1, $2,... A liasing is prohibited. Rewriting the built-in global variables can cause serious problems.
alias 方法名 方法名 alias 全局变量 全局变量
alias foo bar alias $MATCH $&
Here, we've defined the alias foo for bar, and the alias $MATCH.
This statement is used to cancel the method definition. undef cannot appear in the method body.
By using undef and alias, the interface of the class can be modified independently from the parent class, but be aware that it can break the program when called by its own internal methods.
undef 方法名
The following instance cancels the method definition named bar:
undef bar