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

Mixins---Including Modules

class|module name

include expr

end

A module may be included within the definition of another module or class using the includemethod. The module or class definition containing theincludegains access to the constants, class variables, and instance methods of the module it includes.

If a module is included within a class definition, the module's constants, class variables, and instance methods are effectively bundled into an anonymous (and inaccessible) superclass for that class. In particular, objects of the class will respond to messages sent to the module's instance methods.

A module may also be included at the top level, in which case the module's constants, class variables, and instance methods become available at the top level.

Module Functions

Although includeis useful for providing mixin functionality, it is also a way of bringing the constants, class variables, and instance methods of a module into another namespace. However, functionality defined in an instance method will not be available as a module method.

module Math

  def sin(x)

    #

  end

end

# Only way to access Math.sin is...

include Math

sin(1)

The method Module#module_function solves this problem by taking one or more module instance methods and copying their definitions into corresponding module methods.

module Math

  def sin(x)

    #

  end

  module_function :sin

end

Math.sin(1)

include Math

sin(1)

The instance method and module method are two different methods: the method definition is copied by module_function, not aliased.

Access Control

Ruby defines three levels of protection for module and class constants and methods:

  • Public. Accessible to anyone.

  • Protected. Can be invoked only by objects of the defining class and its subclasses.

  • Private. Can be called only in functional form (that is, with an implicitselfas the receiver). Private methods therefore can be called only in the defining class and by direct descendents within the same object.

private [

aSymbol

]*

protected [

aSymbol

]*

public [

aSymbol

]*

Each function can be used in two different ways.

  1. If used with no arguments, the three functions set the default access control of subsequently defined methods.

  2. With arguments, the functions set the access control of the named methods and constants.

Access control is enforced when a method is invoked.

Blocks, Closures, and Proc Objects

A code block is a set of Ruby statements and expressions between braces or a do/endpair. The block may start with an argument list between vertical bars. A code block may appear only immediately after a method invocation. The start of the block must be on the same logical line as the end of the invocation.

invocationdo | a1, a2, ... |

end

invocation{ | a1, a2, ... |

}

Braces have a high precedence; dohas a low precedence. If the method invocation has parameters that are not enclosed in parentheses, the brace form of a block will bind to the last parameter, not to the overall invocation. Thedoform will bind to the invocation.

Within the body of the invoked method, the code block may be called using the yieldmethod. Parameters passed to theyieldwill be assigned to arguments in the block using the rules of parallel assignment described starting on page 219. The return value of theyieldis the value of the last expression evaluated in the block.

A code block remembers the environment in which it was defined, and it uses that environment whenever it is called.

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