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

Class Definition

class classname[<superexpr

]

body

end

class << anObject

body

end

A Ruby class definition creates or extends an object of class Classby executing the code inbody. In the first form, a named class is created or extended. The resultingClassobject is assigned to a global constant namedclassname. This name should start with an uppercase letter. In the second form, an anonymous (singleton) class is associated with the specific object.

If present, superexprshould be an expression that evaluates to aClassobject that will be installed as the superclass of the class being defined. If omitted, it defaults to classObject.

Within body, most Ruby expressions are simply executed as the definition is read. However:

  • Method definitions will register the methods in a table in the class object.

  • Nested class and module definitions will be stored in constants within the class, not as global constants. These nested classes and modules can be accessed from outside the defining class using ``::'' to qualify their names.

    module NameSpace

      class Example

        CONST = 123

      end

    end

    obj = NameSpace::Example.new

    a = NameSpace::Example::CONST

  • The Module#include method will add the named modules as anonymous superclasses of the class being defined.

It is worth emphasizing that a class definition is executable code. Many of the directives used in class definition (such as attrandinclude) are actually simply private instance methods of classModule(documented starting on page 344).

Chapter 19, which begins on page 237, describes in more detail how Classobjects interact with the rest of the environment.

Creating Objects from Classes

obj=classexpr.new[([

args

]*

) ]

Class Classdefines the instance methodClass#new , which:

  • Creates an object of the class of the receiver (classexprin the syntax example).

  • Sets that object's type to be the receiver.

  • Invokes the instance method initializein the newly created object, passing it any arguments originally passed tonew.

If a class definition overrides the class method newwithout callingsuper, no objects of that class can be created.

Class Attribute Declarations

Class attribute declarations are technically not part of the Ruby language: they are simply methods defined in class Modulethat create accessor methods automatically.

class name

attr attribute[,writable

]

attr_reader [

attribute

]+

attr_writer [

attribute

]+

attr_accessor [

attribute

]+

end

Module Definitions

module name

body

end

A module is basically a class that cannot be instantiated. Like a class, its body is executed during definition and the resulting Moduleobject is stored in a constant. A module may contain class and instance methods and may define constants and class variables. As with classes, module methods are invoked using theModuleobject as a receiver, and constants are accessed using the ``::'' scope resolution operator.

module Mod

  CONST = 1

  def Mod.method1    # module method

    CONST + 1

  end

end

Mod::CONST

»

1

Mod.method1

»

2

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