programming.nbk: Home | Index | Next Page: Ruby: Comments | Previous Page: Ruby: Class


 Ruby: Classes

    class class_name {< superclass}
        code
    end

The class name must be a constant, which means it must start with a capital letter.

Data in a class are called attributes.

Methods are defined within the class definition.

Accessors are shortcuts to read and write the values of an object's attributes:

    Shortcut                Effect  
    attr_reader :v          def v; @v; end  
    attr_writer :v          def v=(value); @v=value; end  
    attr_accessor :v        attr_reader :v; attr_writer :v  
    attr_accessor :v, :w    attr_accessor :v; attr_accessor :w

Classes can be derived from other classes:

    class dog < animal...

Classes can only be derived from a single parent class. See also [Mixin's].


programming.nbk: Home | Index | Next Page: Ruby: Comments | Previous Page: Ruby: Class


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time