Ruby methods, procs, Lambdas, and closures

Source: Internet
Author: User

 

Define simple method

The keyword def is used for method definition, followed by a list of method names and optional parameter names. The list of parameter names is enclosed by a pair of parentheses. The code that constitutes the method subject is placed after the parameter list, and end is used to end the method definition.

#define a methoddef    factorial(n)    if n<1        raise "argument must be >0"    elsif n==1        1    else         n*factorial(n-1)    endendputs factorial(5)

 

Method Return Value

The method may end normally or abnormally. When an exception is thrown, it ends abnormally.
If the method ends normally, the value of the last expression in the method code is used as the value of the method call expression.

The return keyword is forcibly returned before reaching the last expression of the method. If there is no expression after return, Ni is returned.

 

def fact2(n)    raise "bad argument " if n<1    return 1 if n==1    n*fact2(n-1)endputs fact2(5)

You can also use the return keyword for the last row of the method, which can emphasize that this expression is used as the return value of this method. However, in general programming practices, if not necessary, return is omitted.

The ruby method can return multiple values. You must use the return statement explicitly and separate the values to be returned with commas:
Def polar (x, y)
Reutrn maht. hypot (Y, x), math. atan2 (Y, X)
End

 

Call method invoking a method on a object

Methods are always invoked on an object. (This object is sometimes called
Extends er in a reference to an object-oriented paradigm in which methods are called
"Messages" and are "sent to" caller objects.) within the body of a method, the keyword
Self refers to the object on which the method was invoked. If we don't specify
An object when invoking a method, then the method is implicitly invoked on self.

You'll learn how to define methods for classes of objects in Chapter 7. Notice, however,
That you 've already seen examples of invoking methods on objects, in code like this:
First = text. Index (pattern)

Like most object-oriented versions, Ruby uses. to separate the object from the method
To be invoked on it. This Code passes the value of the variable pattern to the Method
Named index of the object stored in the variable text, and stores the return value in
Variable first.

 

Define the single-key method define a singleton Method

The methods we have defined so far are global methods. If you place the preceding def statements in a class statement, these methods become instance methods of the class, which are defined on all instance objects of the class.

However, you can also use the def statement to define a method for a specific object. Simply add an expression that evaluates the result as an object after the def keyword, after this expression, a period sign and the method name to be defined are required. The method defined in this way is called the single-key method, because it is only available on a single object:

O = "message" def O. printme puts selfendo. printme # Output Message

If the single-key method is defined in fixnum, the following error occurs: <main> ': Can't define Singleton method "printme" for fixnum (typeerror)

 

UNDEF Method

Methods are defined with the def statement and may be undefined with the UNDEF
Statement:

def sum(x,y)    x+yendputs sum(1,2)undef sum

In this Code, the def statement defines a global method, and UNDEF undefines it. UNDEF
Also works within classes (which are the subject of chapter 7) to undefine the instance
Methods of the class. Interestingly, UNDEF can be used to undefine inherited methods,
Without affecting the definition of the method in the class from which it is inherited.
Suppose Class A defines a method M, and Class B is a subclass of A and therefore inherits
M. (subclasses and inheritance are also explained in Chapter 7.) If you don't want

Allow instances of Class B to be able to invoke M, you can use UNDEF m within the body
Of the subclass.
UNDEF is not a commonly used statement. In practice, it is much more common
Redefine a method with a new def statement than it is to undefine or delete the method.
Note that the UNDEF statement must be followed by a single identifier that specifies
Method Name. It cannot be used to undefine a singleton method in the way that Def
Can be used to define such a method.
Within a class or module, you can also use undef_method (a private method of module)
To undefine methods. Pass a symbol representing the name of the method to be
Undefined.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.