Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
122
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Method Definition

def defname[([

arg[=val

]

]*

[, *vararg

][, &blockarg

])]

body

end

defnameis both the name of the method and optionally the context in which it is valid.

defname

<-

methodname

expr.methodname

A methodnameis either a redefinable operator (see Table 18.4 on page 219) or a name. Ifmethodnameis a name, it should start with a lowercase letter (or underscore) optionally followed by upper- and lowercase letters, underscores, and digits. Amethodnamemay optionally end with a question mark (``?''), exclamation point (``!''), or equals sign (``=''). The question mark and exclamation point are simply part of the name. The equals sign is also part of the name but additionally signals that this method is an attribute setter (described on page 23).

A method definition using an unadorned method name within a class or module definition creates an instance method. An instance method may be invoked only by sending its name to a receiver that is an instance of the class that defined it (or one of that class's subclasses).

Outside a class or module definition, a definition with an unadorned method name is added as a private method to class Object, and hence may be called in any context without an explicit receiver.

A definition using a method name of the form expr.methodnamecreates a method associated with the object that is the value of the expression; the method will be callable only by supplying the object referenced by the expression as a receiver. Other Ruby documentation calls these methodssingleton methods.

class MyClass

  def MyClass.method      # definition

  end

end

MyClass.method            # call

anObject = Object.new

def anObject.method       # definition

end

anObject.method           # call

def (1.class).fred        # receiver may be an expression

end

Fixnum.fred               # call

Method definitions may not contain class, module, or instance method definitions. They may contain nested singleton method definitions. The body of a method acts as if it were a begin/endblock, in that it may contain exception handling statements (rescue,else, andensure).

Method Arguments

A method definition may have zero or more regular arguments, an optional array argument, and an optional block argument. Arguments are separated by commas, and the argument list may be enclosed in parentheses.

A regular argument is a local variable name, optionally followed by an equals sign and an expression giving a default value. The expression is evaluated at the time the method is called. The expressions are evaluated from left to right. An expression may reference a parameter that precedes it in the argument list.

def options(a=99, b=a+1)

  [ a, b ]

end

options

»

[99, 100]

options 1

»

[1, 2]

options 2, 4

»

[2, 4]

The optional array argument must follow any regular arguments and may not have a default.

When the method is invoked, Ruby sets the array argument to reference a new object of class Array. If the method call specifies any parameters in excess of the regular argument count, all these extra parameters will be collected into this newly created array.

def varargs(a, *b)

  [ a, b ]

end

varargs 1

»

[1, []]

varargs 1, 2

»

[1, [2]]

varargs 1, 2, 3

»

[1, [2, 3]]

If an array argument follows arguments with default values, parameters will first be used to override the defaults. The remainder will then be used to populate the array.

def mixed(a, b=99, *c)

  [ a, b, c]

end

mixed 1

»

[1, 99, []]

mixed 1, 2

»

[1, 2, []]

mixed 1, 2, 3

»

[1, 2, [3]]

mixed 1, 2, 3, 4

»

[1, 2, [3, 4]]

The optional block argument must be the last in the list. Whenever the method is called, Ruby checks for an associated block. If a block is present, it is converted to an object of class Procand assigned to the block argument. If no block is present, the argument is set tonil.

Соседние файлы в папке Ruby