English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Ruby methods are similar to functions in other programming languages. Ruby methods are used to bind one or more repeated statements into a unit.
The method name should start with a lowercase letter. If you start the method name with an uppercase letter, Ruby may treat it as a constant, resulting in incorrect parsing of the call.
The method should be defined before calling, otherwise Ruby will produce an undefined method call exception.
def method_name([(arg[= default]]...[, * arg[, &expr]])] expr.. end
So, you can define a simple method, as shown below:
def method_name expr.. end
You can define a method that accepts parameters, as shown below:
def method_name(var1, var2) expr.. end
You can set default values for parameters, and use default values if necessary 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 shown below:
method_name
However, when you call a method with parameters, you also need to include the parameters when writing the method name, for example:
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 will display an error.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test(a1="Ruby", a2="Perl") puts "Programming language is #{a"1" puts "Programming language is #{a"2" end test "C", "C"++" test
The output of the above example is as follows:
Programming language is C Programming language is C++ Programming language is Ruby Programming language is Perl
By default, each method in Ruby will return a value. The returned value is the value of the last statement. For example:
def test i = 100 j = 10 k = 0 end
When calling this method, the last declared variable k will be returned.
In Ruby, return The statement is used to return one or more values from a 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 or return 12 or return 1,2,3
See 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
The output of the above example is as follows:
100 200 300
Suppose you declare a method with two parameters, and when you call this method, you also need to pass two parameters at the same time.
However, Ruby allows you to declare methods with a variable number of parameters. Let's see the following example:
#!/usr/bin/ruby # -*- coding: UTF-8 -*- def sample (*test) puts "Number of parameters is #{test.length}" for i in 0...test.length puts "Parameter value is #{test[i]}" end end sample "Zara", ""6"F" sample "Mac", ""36"M", "MCA"
In this piece of code, you have declared a method sample that accepts a parameter test. However, this parameter is a variable parameter. This means that the parameter can have a different number of variables. The output of the above example is as follows:
with a number of parameters 3 with a parameter value of Zara with a parameter value 6 with a parameter value of F with a number of parameters 4 with a parameter value of Mac with a parameter value 36 with a parameter value of M with a parameter value of MCA
When a method is defined outside the class, the method is marked as private. On the other hand, if the method is defined within the class, it is marked as public by default.
default visibility of methods private marked via the public or private Change.
When you want 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 instantiation. Let's 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 then the method name. You can directly access class methods as shown below:
Accounts.return_date
To access this method, you do not need to create an object of the class Accounts.
This statement is used to create aliases for methods or global variables. Aliases cannot be defined within the method body. Even if the method is overridden, the alias retains the current definition of the method.
为编号的全局变量($1For numbered global variables ($2, $
Syntax alias method_name method_name
alias global_variable global_variable alias foo bar
alias $MATCH $&
Statementundef This statement is used to cancel the method definition.
Cannot appear in the method body. undef and aliasThe interface of the class can be independently modified from the parent class, but please note that it may break the program when calling methods internally.
undef method_name
The following example cancels the named barMethod definition:
undef bar