6.18. Ruby method

发布时间 : 2025-10-25 13:34:04 UTC      

Page Views: 9 views

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 unit.

The method name should begin with a lowercase letter. If you start a method name with an uppercase letter, Ruby may treat it as a constant, resulting inincorrect parsing of the call.

The method should be defined before the call, otherwise Ruby will generate an undefined method call exception.

6.18.1. Grammar #

defmethod_name[([arg[=default]]...[, *arg[, &expr]])]expr..end 

So, you can define a simple method, as follows:

defmethod_nameexpr..end 

You can define a method that accepts parameters, as follows:

defmethod_name(var1,var2)expr..end 

You can set the default value for the parameter, and use the default value if the necessary parameters are not passed when the method is called:

defmethod_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 also need to write the method name with parameters, such as:

method_name25,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.

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftest(a1="Ruby",a2="Perl")puts"The programming language is #{a1}"puts"The programming language is #{a2}"endtest"C","C++"test 

The output of the above instance is as follows:

The programming language is C The programming language is C++ The programming language is Ruby The programming language is Perl 

6.18.2. Return a value from a method #

Each method in Ruby returns a value by default. The value returned is the value of the last statement. For example:

Example #

deftesti=100j=10k=0end 

When this method is called, the last declared variable is returned k .

6.18.3. Ruby return statement #

In Ruby return statement is used to return one or more values from the Ruby method.

Grammar #

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.

Example #

return or return12 or return1,2,3 

Look at the following example:

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftesti=100j=200k=300returni,j,kendvar=testputsvar 

The output of the above instance is as follows:

100 200 300 

6.18.4. Variable number of parameters #

Suppose you declare a method with two parameters, and when you call the method, you need to pass two parameters at the same time.

However, Ruby allows you to declare methods with a variable number of parameters. Let’s look at the following example:

Example #

#!/usr/bin/ruby#-*- coding: UTF-8 -*-defsample(*test)puts"The number of parameters is #{test.length}"foriin0...test.lengthputs"The parameter value is #{test[i]}"endendsample"Zara","6","F"sample"Mac","36","M","MCA" 

In this code, you have declared a method sample to accept a parameter test . However, this parameter is a variable parameter. This means that parameters can have a different number of variables. The output of the aboveinstance is as follows:

The number of parameters is 3 The parameter value is Zara The parameter value is 6 The parameter value is F The number of parameters is 4 Parameter value is Mac The parameter value is 36 The parameter value is M The parameter value is MCA 

6.18.5. Class method #

When a method is defined outside the class, the method is marked by default as private . On the other hand, if the method is defined in the class, the default tag is public .

Method default visibility and private tags can be passed through the module (Module) public or private change.

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 look at how to declare and access class methods:

classAccountsdefreading_chargeenddefAccounts.return_dateendend 

We already know the way return_date . How it was declared. It is declared by following the class name with a period followed by the method name. You can access class methods directly, as follows:

Accounts.return_date 

To access this method, you do not need to create a class Accounts the object.

6.18.6. Ruby alias statement #

This statement is used to alias a method or global variable. Aliases cannot be defined within the method body. Even if the method is overridden, the alias of the method maintains the current definition of the method.

Is a numbered global variable ( $1,$2,... ) aliases are forbidden. Overriding built-in global variables can cause serious problems.

Grammar #

Alias method name method name alias global variable global variable 

Example #

aliasfoobaralias$MATCH$& 

Here, we have been working for bar defines an alias as foo for $& defines an alias as $MATCH .

6.18.7. Ruby undef statement #

This statement is used to undefine a method. undef cannot appear in themethod body.

By using the undef and alias the interface of the class can be modified independently from the parent class, but be aware that it can breakthe program when calling its own internal methods

undef method name 

6.18.8. Example #

The following example cancels the name bar the method definition:

undefbar 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。