Thursday, February 4, 2010

Iterators with Complex State













Programming in Lua
Part I. The Language
            
Chapter 7. Iterators and the Generic for



7.4 - Iterators with Complex State



Frequently, an iterator needs to keep more state than fits into
a single invariant state and a control variable.
The simplest solution is to use closures.
An alternative solution is to pack all it needs into a table
and use this table as the invariant state for the iteration.
Using a table, an iterator can keep as much data as it needs
along the loop.
Moreover, it can change that data as it goes.
Although the state is always the same table
(and therefore invariant),
the table contents change along the loop.
Because such iterators have all their data in the state,
they typically discard the second argument
provided by the generic for
(the iterator variable).

As an example of this technique,
we will rewrite the iterator allwords,
which traverses all the words from the current input file.
This time, we will keep its state using a table with two fields,
line and pos.

The function that starts the iteration is simple.
It must return the iterator function and the initial state:


local iterator -- to be defined later

function allwords ()
local state = {line = io.read(), pos = 1}
return iterator, state
end

The iterator function does the real work:

function iterator (state)
while state.line do -- repeat while there are lines
-- search for next word
local s, e = string.find(state.line, "%w+", state.pos)
if s then -- found a word?
-- update next position (after this word)
state.pos = e + 1
return string.sub(state.line, s, e)
else -- word not found
state.line = io.read() -- try next line...
state.pos = 1 -- ... from first position
end
end
return nil -- no more lines: end loop
end






Whenever it is possible,
you should try to write stateless iterators,
those that keep all their state in the for variables.
With them, you do not create new objects when you start a loop.
If you cannot fit your iteration into that model,
then you should try closures.
Besides being more elegant,
typically a closure is more efficient
than an iterator using tables:
First, it is cheaper to create a closure than a table;
second, access to upvalues is faster than
access to table fields.
Later we will see yet another way to write iterators,
with coroutines.
This is the most powerful solution,
but a little more expensive.










Programming in Lua



8.4 Additional Documentation Sources



[ Team LiB ]





8.4 Additional Documentation Sources


When looking for source code documentation, consider nontraditional sources such as comments, standards, publications, test cases, mailing lists, newsgroups, revision logs, issue-tracking databases, marketing material, and the source code itself. When investigating a large body of code, it is natural to miss documentation embedded in comments in the quest for more formal sources such as requirements and design documents. Yet source code comments are often better maintained than the corresponding formal documents and often hide gems of information, sometimes even including elaborate ASCII diagrams. As an example, the diagrams in Figure 8.3 and the formal mathematical proof in Figure 8.4[44] are all excerpts from actual source code comments. The ASCII diagrams depict the block structure of an audio interface hardware[45] (top left), the logical channel reshuffling procedure in X.25 networking code[46] (top right), the m4 macro processor stack-based data structure[47] (bottom left), and the page format employed in the hashed db database implementation[48] (bottom right). However, keep in mind that elaborate diagrams such as these are rarely kept up-to-date when the code changes.

[44] netbsdsrc/sys/kern/kern_synch.c:102�135

[45] netbsdsrc/sys/dev/ic/cs4231reg.h:44�75

[46] netbsdsrc/sys/netccitt/pk_subr.c:567�591

[47] netbsdsrc/usr.bin/m4/mdef.h:155�174

[48] netbsdsrc/lib/libc/db/hash/page.h:48�59


Figure 8.3. ASCII drawings in source code comments.


Always view documentation with a critical mind. Since documentation is never executed and rarely tested or formally reviewed to the extent code is, it can often be misleading or outright wrong. As an example, consider some of the problems[49] of the comment in Figure 8.4.

[49] Contributed by Guy Steele.


  • It inexplicably uses the symbol ~= the first three times to indicate "approximately equal to" and thereafter uses =~.

  • It uses both loadavg and loadav to refer to the same quantity. The surrounding code, not shown in the figure, uses loadav and a structure field named ldavg!

  • The approximations are very sloppy and there is no justification that they are close enough. For example, the result of the division 5/2.3 is approximated as 2. This has the effect of replacing the constant .1 in the original equation with 0.08, a 20% error. The comment does not justify the correctness of this approximation.

    Figure 8.4 A mathematical proof in a source code comment.


    * We wish to prove that the system's computation of decay
    * will always fulfill the equation:
    * decay ** (5 * loadavg) ~= .1
    *
    * If we compute b as:
    * b = 2 * loadavg
    * then
    * decay = b / (b + 1)
    *
    * We now need to prove two things:
    * 1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
    * 2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
    *
    * Facts:
    * For x close to zero, exp(x) =~ 1+ x, since
    * exp(x) = 0! + x**1/1! + x**2/2! + ... .
    * therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
    * For x close to zero, ln(1+x) =~ x, since
    * ln(1+x) = x - x**2/2 + x**3/3 - ... -1<x<1
    * therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
    * ln(.1) =~ -2.30
    *
    * Proof of (1):
    * Solve (factor)**(power) =~ .1given power (5*loadav):
    * solving for factor,
    * ln(factor) =~ (-2.30/5*loadav), or
    * factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
    * exp(-1/b) =~ (b-1)/b =~ b/(b+1). QED
    *
    * Proof of (2):
    * Solve (factor)**(power) =~ .1given factor == (b/(b+1)):
    * solving for power,
    * power*ln(b/(b+1)) =~ -2.30, or
    * power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav. QED
  • The approximations used for exp(x) and ln(1+x) depend on x being "close to zero," but there is no explanation of how close is close enough and no evidence or explanation provided to justify the assumption that x will indeed be close enough to zero in the actual application at hand. (A little analysis shows that for x to be "close to zero," loadavg needs to be "large," but there is no discussion of this, either.)

  • Before the proofs, the comment methodically lays out two useful "facts" about approximations on which the proofs rely. This is good. But the proofs also rely on a third fact about approximations that is not laid out ahead of time: (b-1)/b=~b/(b+1) if b is "large enough." The QED line of the first proof simply pulls a fast one.

  • Finally, the comment is actually much more verbose than it needs to be because the two proofs are redundant! They prove essentially the same mathematical fact from two different directions.


A nontraditional documentation element, intimately bound to the source code, is the revision control system. This repository contains a detailed history of the source code's evolution and, often, comments justifying each change. In Section 6.5 we examine how you can benefit from using such a system when reading code. Associated with a revision control system is also often an issue-tracking database. There you will find details of bug reports, change requests, and other maintenance documentation. When these originate from inside the development organization, they may provide background on design and implementation issues related to the code you are reading.


The tautology may appear to be an oxymoron, but the source code can sometimes be its own documentation. Apart from the obvious case of self-documenting code, sometimes you can read code between the lines as a specification, even if the actual code does not implement the underlying intention of what the code should actually do. Consider the following (trivial) shell-script excerpt.[50]

[50] netbsdsrc/usr.bin/lorder/lorder.sh:82



for file in $* ; do echo $file":" ; done

The code will display each one of the space-separated arguments appearing in the $* list on a separate line. It is obvious, however, from the loop structure that the intention of the code is to display each file name in the $* list on a separate line. Although the file variable should more appropriately have been named filename, and the code will not work correctly when file names contain whitespace, you can still read the code as the specification of what it should do, rather than what it actually claims it does.


Finally, you can often find additional documentation on the periphery or outside the development organization. Standards documents can be treated as the functional specification for software that implements a specific standard (for example, an MP3 player).Similarly, you can often find a description of a given design, system, algorithm, or implementation in journal or conference publications. If verification and validation are handled by a separate test group, you can use their test cases as a substitute or a supplement of a functional specification. When desperate, even marketing material can provide you with an (inflated) list of a system's features. And you can always search the Web for discussions, unofficial information, FAQ pages, and user experiences; archived newsgroups and mailing lists may sometimes reveal the rationale behind the design of the code you are reading. When the code is open-source, a particularly effective search strategy is to use three or four nonword identifiers from the code part you are reading (for example, bbp, indouble, addch) as search terms in a major search engine. Open-source code also provides you with the option to contact the code's original author. Try not to abuse this privilege, and be sure to give something back to the community for any help you receive in this way. Remember: most open-source projects are developed and maintained by (typically overworked) volunteers.


Exercise 8.13
Create an annotated list of documentation sources for the apache Web server or Perl. Categorize the sources based on the type of information they provide. Strive for wide coverage of areas (for example, specifications, design, user documentation) rather than completeness.





    [ Team LiB ]



    Non-Preemptive Multithreading













    Programming in Lua
    Part I. The Language
                
    Chapter 9. Coroutines



    9.4 - Non-Preemptive Multithreading



    As we saw earlier, coroutines are a kind of
    collaborative multithreading.
    Each coroutine is equivalent to a thread.
    A pair yield-resume switches control from one thread to another.
    However, unlike "real" multithreading,
    coroutines are non preemptive.
    While a coroutine is running, it cannot be stopped from the outside.
    It only suspends execution when it explicitly requests so
    (through a call to yield).
    For several applications this is not a problem,
    quite the opposite.
    Programming is much easier in the absence of preemption.
    You do not need to be paranoid about synchronization bugs,
    because all synchronization among threads is explicit in the program.
    You only have to ensure that
    a coroutine only yields when it is outside a critical region.

    However, with non-preemptive multithreading,
    whenever any thread calls a blocking operation,
    the whole program blocks until the operation completes.
    For most applications, this is an unacceptable behavior,
    which leads many programmers to disregard coroutines as a real
    alternative to conventional multithreading.
    As we will see here, that problem has an interesting
    (and obvious, with hindsight) solution.

    Let us assume a typical multithreading situation:
    We want to download several remote files through HTTP.
    Of course, to download several remote files,
    we must know how to download one remote file.
    In this example, we will use the LuaSocket library,
    developed by Diego Nehab.
    To download a file, we must open a connection to its site,
    send a request to the file,
    receive the file (in blocks), and close the connection.
    In Lua, we can write this task as follows.
    First, we load the LuaSocket library:


    require "luasocket"


    Then, we define the host and the file we want to download.
    In this example, we will download the HTML 3.2 Reference Specification
    from the World Wide Web Consortium site:


    host = "www.w3.org"
    file = "/TR/REC-html32.html"


    Then, we open a TCP connection to port 80
    (the standard port for HTTP connections) of that site:


    c = assert(socket.connect(host, 80))

    The operation returns a connection object,
    which we use to send the file request:

    c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")



    The receive method always returns a string with
    what it read plus another string with the status of the operation.
    When the host closes the connection we break the receive loop.

    Finally, we close the connection:


    c:close()


    Now that we know how to download one file,
    let us return to the problem of downloading several files.
    The trivial approach is to download one at a time.
    However, this sequential approach,
    where we only start reading a file after finishing the previous one,
    is too slow.
    When reading a remote file,
    a program spends most of its time waiting for data to arrive.
    More specifically, it spends most of its time blocked
    in the call to receive.
    So, the program could run much faster if it downloaded all files
    simultaneously.
    Then, while a connection has no data available,
    the program can read from another connection.
    Clearly, coroutines offer a convenient way to structure those
    simultaneous downloads.
    We create a new thread for each download task.
    When a thread has no data available,
    it yields control to a simple dispatcher,
    which invokes another thread.

    To rewrite the program with coroutines,
    let us first rewrite the previous download code
    as a function:


    function download (host, file)
    local c = assert(socket.connect(host, 80))
    local count = 0 -- counts number of bytes read
    c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
    while true do
    local s, status = receive(c)
    count = count + string.len(s)
    if status == "closed" then break end
    end
    c:close()
    print(file, count)
    end

    Because we are not interested in the remote file contents,
    this function only counts the file size,
    instead of writing the file to the standard output.
    (With several threads reading several files,
    the output would intermix all files.)
    In this new code, we use an auxiliary function (receive)
    to receive data from the connection.
    In the sequential approach, its code would be like this:

    function receive (connection)
    return connection:receive(2^10)
    end

    For the concurrent implementation,
    this function must receive data without blocking.
    Instead, if there is not enough data available, it yields.
    The new code is like this:

    function receive (connection)
    connection:timeout(0) -- do not block
    local s, status = connection:receive(2^10)
    if status == "timeout" then
    coroutine.yield(connection)
    end
    return s, status
    end

    The call to timeout(0) makes any operation over the connection
    a non-blocking operation.
    When the operation status is "timeout",
    it means that the operation returned without completion.
    In this case, the thread yields.
    The non-false argument passed to yield
    signals to the dispatcher that
    the thread is still performing its task.
    (Later we will see another version where the dispatcher
    needs the timed-out connection.)
    Notice that, even in case of a timeout,
    the connection returns what it read until the timeout,
    so receive always returns s to its caller.

    The next function ensures that each download runs
    in an individual thread:


    threads = {} -- list of all live threads
    function get (host, file)
    -- create coroutine
    local co = coroutine.create(function ()
    download(host, file)
    end)
    -- insert it in the list
    table.insert(threads, co)
    end

    The table threads keeps a list of all live threads,
    for the dispatcher.

    The dispatcher is simple.
    It is mainly a loop that goes through all threads, calling one by one.
    It must also remove from the list the threads that finish their tasks.
    It stops the loop when there are no more threads to run:


    function dispatcher ()
    while true do
    local n = table.getn(threads)
    if n == 0 then break end -- no more threads to run
    for i=1,n do
    local status, res = coroutine.resume(threads[i])
    if not res then -- thread finished its task?
    table.remove(threads, i)
    break
    end
    end
    end
    end


    Finally, the main program creates the threads it needs
    and calls the dispatcher.
    For instance, to download four documents from the W3C site,
    the main program could be like this:


    host = "www.w3.org"

    get(host, "/TR/html401/html40.txt")
    get(host,"/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
    get(host,"/TR/REC-html32.html")
    get(host,
    "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")

    dispatcher() -- main loop

    My machine takes six seconds to download those four files using coroutines.
    With the sequential implementation,
    it takes more than twice that time (15 seconds).

    Despite the speedup, this last implementation is far from optimal.
    Everything goes fine while at least one thread has something to read.
    However, when no thread has data to read,
    the dispatcher does a busy wait,
    going from thread to thread only to check that they still have no data.
    As a result, this coroutine implementation uses almost 30 times
    more CPU than the sequential solution.

    To avoid this behavior, we can use the select function from LuaSocket.
    It allows a program to block while waiting for a status change in
    a group of sockets.
    The changes in our implementation are small.
    We only have to change the dispatcher.
    The new version is like this:


    function dispatcher ()
    while true do
    local n = table.getn(threads)
    if n == 0 then break end -- no more threads to run
    local connections = {}
    for i=1,n do
    local status, res = coroutine.resume(threads[i])
    if not res then -- thread finished its task?
    table.remove(threads, i)
    break
    else -- timeout
    table.insert(connections, res)
    end
    end
    if table.getn(connections) == n then
    socket.select(connections)
    end
    end
    end

    Along the inner loop, this new dispatcher
    collects the timed-out connections in
    table connections.
    Remember that receive passes such
    connections to yield;
    thus resume returns them.
    When all connections time out,
    the dispatcher calls select to wait for any of those connections
    to change status.
    This final implementation runs as fast as the first implementation
    with coroutines.
    Moreover, as it does no busy waits,
    it uses just a little more CPU than the sequential implementation.











    Programming in Lua



    Recipe 8.6. Building a Query String










    Recipe 8.6. Building a Query String



    8.6.1. Problem


    You


    need to construct a link that includes name/value pairs in a query string.




    8.6.2. Solution


    Use the http_build_query( )
    function, as in Example 8-14.


    Building a query string



    <?php
    $vars = array('name' => 'Oscar the Grouch',
    'color' => 'green',
    'favorite_punctuation' => '#');
    $query_string = http_build_query($vars);
    $url = '/muppet/select.php?' . $query_string;
    ?>






    8.6.3. Discussion


    The URL built in Example 8-14 is:


    /muppet/select.php?name=Oscar+the+Grouch&color=green&favorite_punctuation=%23



    The query string has spaces encoded as

    +. Special characters such as # are hex encoded as %23 because the ASCII value of # is 35, which is 23 in
    hexadecimal.


    Although the encoding that http_build_query( ) does prevents any special characters in the variable names or values from disrupting the constructed URL, you may have problems if your variable names begin with the names of HTML entities. Consider this partial URL for retrieving information about a stereo system:


    /stereo.php?speakers=12&cdplayer=52&amp=10



    The HTML entity for

    ampersand (&) is &amp; so a browser may interpret that URL as:


    /stereo.php?speakers=12&cdplayer=52&=10



    To prevent embedded
    entities from corrupting your URLs, you have three choices. The first is to choose variable names that can't be confused with entities, such as _amp instead of amp. The second is to convert characters with HTML entity equivalents to those entities before printing out the URL. Use
    htmlentities( ):


    $url = '/muppet/select.php?' . htmlentities($query_string);



    The resulting URL is:


    /muppet/select.php?name=Oscar+the+Grouch&amp;color=green&amp;favorite_punctuation=%23



    Your third choice is to change the argument separator from & to &amp; by setting the configuration directive arg_separator.input to &amp;. Then, http_build_query( ) joins the different name=value pairs with &amp;:


    /muppet/select.php?name=Oscar+the+Grouch&amp;color=green&amp;favorite_punctuation=%23





    8.6.4. See Also


    Documentation on http_build_query( ) at http://www.php.net/http_build_query and htmlentities( ) at http://www.php.net/htmlentities.













    Introduction












    Introduction

    Pain is a wonderful learning tool. It's nature's way of saying, "Don't do that!" If you are a programmer, you've had your share of pain. It usually occurs about 2:00 in the morning as you finally find that one last bug that has been tormenting you for the past two weeks.


    The book is filled with buggy programs. This allows you to learn from the misfortunes of others. It contains bugs that I've found, bugs found by my friends and other programmers. Each program is a learning experience.


    The programs presented here are designed to be as close to real world programs as possible. Each of the programs tries to accomplish a simple task or perform a simple C++ language feature. The bad news is that they don't work. The good news is that each is contained in a relatively short program, so you you don't have to muck through a 750,000 line program trying to discover the problem.


    Some people believe that with the new compiler technology out there, that the compiler can catch most of these errors. Unfortunately, there are lots of errors that a compiler can't catch.


    As an analogy, spell checkers are supposed to eliminate spelling errors. But can you spot the spelling error in this word: CAT [1]? Smelling cockers or a god think because other side this block would be fuel of arrows. (Hey, it passed the spell checker.)


    So have fun spotting the errors. If you run into trouble, we've provided a number of hints to help you out (and a couple that are no help at all). There are also answers in the back of the book.


    This is in contrast to real life, where there are no hints, and the answers aren't in the back of the book.


    This book is dedicated to my wife, Chi Mui Wong. If she hadn't taken CS101 and learned that she's not a programmer, this book wouldn't exist (although it's her instructor who's responsible for the first broken "Hello World" in this book).


    But the real dedication is to all those working programmers out there who labor day in and day out with complex, buggy, really rotten code and have to make sense of it. Good luck and have fun.





    [1]The word is "DOG."














    Section 7.1. Defining a Simple Class








    Chapter 7. Classes and Modules












    Ruby is an object-oriented language in a very pure sense: every value
    in Ruby is (or at least behaves like) an object. Every object is an instance
    of a class. A class defines a set of methods that an object responds to.
    Classes may extend or subclass other classes, and inherit or override the
    methods of their superclass. Classes can also include—or inherit methods
    from—modules.


    Ruby's objects are strictly encapsulated: their state can be accessed
    only through the methods they define. The instance variables manipulated by
    those methods cannot be directly accessed from outside of the object. It is
    possible to define getter and setter accessor methods that appear to access
    object state directly. These pairs of accessor methods are known as attributes and are distinct from
    instance variables. The methods defined by a class may have "public,"
    "protected," or "private" visibility, which affects how and where they may
    be invoked.


    In contrast to the strict encapsulation of object state, Ruby's
    classes are very open. Any Ruby program can add methods to existing classes,
    and it is even possible to add "singleton methods" to individual
    objects.


    Much of Ruby's OO architecture is part of the core language. Other
    parts, such as the creation of attributes and the declaration of method
    visibility, are done with methods rather than true language keywords. This
    chapter begins with an extended tutorial that demonstrates how to define a
    class and add methods to it. This tutorial is followed by sections on more
    advanced topics, including:


    • Method visibility

    • Subclassing and inheritance

    • Object creation and initialization

    • Modules, both as namespaces and as includable "mixins"

    • Singleton methods and the eigenclass

    • The method name resolution algorithm

    • The constant name resolution algorithm




    7.1. Defining a Simple Class


    We begin our coverage of classes with an extended tutorial
    that develops a class named Point to represent a
    geometric point with X and Y coordinates. The subsections that follow
    demonstrate how to:


    • Define a new class

    • Create instances of that class

    • Write an initializer method for the class

    • Add attribute accessor methods to the class

    • Define operators for the class

    • Define an iterator method and make the class
      Enumerable

    • Override important Object methods such as
      to_s, ==,
      hash, and <=>

    • Define class methods, class variables, class instance variables,
      and constants



    7.1.1. Creating the Class


    Classes are created in Ruby with the class keyword:


    class Point
    end



    Like most Ruby constructs, a class definition is delimited with an
    end. In addition to defining a new class, the
    class keyword creates a new constant to refer to the
    class. The class name and the constant name are the same, so all class
    names must begin with a capital letter.


    Within the body of a class, but outside of any
    instance methods defined by the class, the self
    keyword refers to the class being defined.


    Like most statements in Ruby, class is an
    expression. The value of a class expression is the
    value of the last expression within the class body.
    Typically, the last expression within a class is a
    def statement that defines a method. The value of a
    def statement is always
    nil.




    7.1.2. Instantiating a Point


    Even though we haven't put anything in our
    Point class yet, we can still instantiate it:


    p = Point.new



    The constant Point holds a class object that
    represents our new class. All class objects have a method named
    new that creates a new instance.


    We can't do anything very interesting with the newly created
    Point object we've stored in the local variable
    p, because we haven't yet defined any methods for the
    class. We can, however, ask the new object what kind of object it
    is:


    p.class       # => Point
    p.is_a? Point # => true





    7.1.3. Initializing a Point


    When we create new Point objects, we want to
    initialize them with two numbers that represent their X and Y
    coordinates. In many object-oriented languages, this is done with a "constructor." In Ruby, it is done with an
    initialize method:


    class Point
    def initialize(x,y)
    @x, @y = x, y
    end
    end



    This is only three new lines of code, but there are a couple of
    important things to point out here. We explained the
    def keyword in detail in Chapter 6.
    But that chapter focused on defining global functions that could be used
    from anywhere. When def is used like this with an
    unqualified method name inside of a class definition,
    it defines an instance method for the class. An
    instance method is a method that is invoked on an instance of the class.
    When an instance method is called, the value of self
    is an instance of the class in which the method is defined.


    The next point to understand is that the
    initialize method has a special purpose in Ruby. The
    new method of the class object creates a new instance
    object, and then it automatically
    invokes the initialize method on that instance.
    Whatever arguments you passed to new are passed on to
    initialize. Because our initialize
    method expects two arguments, we must now supply two values when we
    invoke Point.new:


    p = Point.new(0,0)



    In addition to being automatically invoked by
    Point.new, the initialize method
    is automatically made private. An object can call
    initialize on itself, but you cannot explicitly call
    initialize on p to reinitialize
    its state.


    Now, let's look at the body of the initialize
    method. It takes the two values we've passed it, stored in local
    variables x and y, and assigns
    them to instance variables @x and
    @y. Instance variables always begin with
    @, and they always "belong to" whatever object
    self refers to. Each instance of our
    Point class has its own copy of these two variables,
    which hold its own X and Y coordinates.



    Instance Variable Encapsulation


    The instance variables of an object can only be accessed by the instance
    methods of that object. Code that is not inside an instance method
    cannot read or set the value of an instance variable (unless it uses
    one of the reflective techniques that are described in Chapter 8).




    Finally, a caution for programmers who are used to Java and
    related languages. In statically typed languages, you must declare your
    variables, including instance variables. You know that Ruby variables
    don't need to be declared, but you might still feel that you have to
    write something like this:


    # Incorrect code!
    class Point
    @x = 0 # Create instance variable @x and assign a default. WRONG!
    @y = 0 # Create instance variable @y and assign a default. WRONG!

    def initialize(x,y)
    @x, @y = x, y # Now initialize previously created @x and @y.
    end
    end



    This code does not do at all what a Java programmer expects.
    Instance variables are always resolved in the context of
    self. When the initialize method
    is invoked, self holds an instance of the
    Point class. But the code outside of that method is
    executed as part of the definition of the Point
    class. When those first two assignments are executed,
    self refers to the Point class
    itself, not to an instance of the class. The @x and
    @y variables inside the initialize
    method are completely different from those outside it.




    7.1.4. Defining a to_s Method


    Just about any class you define should have a
    to_s instance method to return a string representation of the
    object. This ability proves invaluable when debugging. Here's how we
    might do this for Point:


    class Point
    def initialize(x,y)
    @x, @y = x, y
    end

    def to_s # Return a String that represents this point
    "(#@x,#@y)" # Just interpolate the instance variables into a string
    end
    end



    With this new method defined, we can create points and print them
    out:


    p = new Point(1,2)   # Create a new Point object
    puts p # Displays "(1,2)"





    7.1.5. Accessors and Attributes


    Our Point class uses two instance variables. As we've noted, however, the value of
    these variables are only accessible to other instance methods. If we
    want users of the Point class to be able to use the X
    and Y coordinates of a point, we've got to provide accessor methods that
    return the value of the variables:


    class Point
    def initialize(x,y)
    @x, @y = x, y
    end

    def x # The accessor (or getter) method for @x
    @x
    end

    def y # The accessor method for @y
    @y
    end
    end



    With these methods defined, we can write code like this:


    p = Point.new(1,2)
    q = Point.new(p.x*2, p.y*3)



    The expressions p.x and p.y
    may look like variable references, but they are, in fact, method
    invocations without parentheses.


    If we wanted our Point class to be mutable
    (which is probably not a good idea), we would also add setter methods to
    set the value of the instance variables:


    class MutablePoint
    def initialize(x,y); @x, @y = x, y; end

    def x; @x; end # The getter method for @x
    def y; @y; end # The getter method for @y

    def x=(value) # The setter method for @x
    @x = value
    end

    def y=(value) # The setter method for @y
    @y = value
    end
    end



    Recall that assignment expressions can be used to invoke setter
    methods like these. So with these methods defined, we can write:


    p = Point.new(1,1)
    p.x = 0
    p.y = 0




    Using Setters Inside a Class


    Once you've defined a setter method like x= for your class,
    you might be tempted to use it within other instance methods of your
    class. That is, instead of writing @x=2, you might
    write x=2, intending to invoke
    x=(2) implicitly on self. It
    doesn't work, of course; x=2 simply creates a new
    local variable.


    This is a not-uncommon mistake for novices who are just learning
    about setter methods and assignment in Ruby. The rule is that
    assignment expressions will only invoke a setter method when invoked
    through an object. If you want to use a setter from within the class
    that defines it, invoke it explicitly through self.
    For example: self.x=2.




    This combination of instance variable with trivial getter and
    setter methods is so common that Ruby provides a way to automate it. The
    attr_reader and attr_accessor
    methods are defined by the Module class. All classes are
    modules, (the Class class is a subclass of
    Module) so you can invoke these method inside any class definition. Both methods
    take any number of symbols naming attributes.
    attr_reader creates trivial getter methods for the
    instance variables with the same name. attr_accessor
    creates getter and setter methods. Thus, if we were defining a mutable
    Point class, we could write:


    class Point
    attr_accessor :x, :y # Define accessor methods for our instance variables
    end



    And if we were defining an immutable version of the class, we'd
    write:


    class Point
    attr_reader :x, :y # Define reader methods for our instance variables
    end



    Each of these methods can accept an attribute name or names as a
    string rather than as a symbol. The accepted style is to use symbols,
    but we can also write code like this:


    attr_reader "x", "y"



    attr is a similar method with a shorter name
    but with behavior that differs in Ruby 1.8 and Ruby 1.9.
    In 1.8, attr can define only a single attribute at a
    time. With a single symbol argument, it defines a getter method. If the
    symbol is followed by the value true, then it defines
    a setter method as well:


    attr :x        # Define a trivial getter method x for @x
    attr :y, true # Define getter and setter methods for @y



    In Ruby 1.9, attr can be used as it is in 1.8,
    or it can be used as a synonym for
    attr_reader.


    The attr, attr_reader, and
    attr_accessor methods create instance methods for us.
    This is an example of metaprogramming, and
    the ability to do it is a powerful feature of Ruby. There
    are more examples of metaprogramming in Chapter 8. Note
    that attr and its related methods are invoked within
    a class definition but outside of any method
    definitions. They are only executed once, when the class is being
    defined. There are no efficiency concerns here: the getter and setter
    methods they create are just as fast as handcoded ones. Remember that
    these methods are only able to create trivial getters and setters that
    map directly to the value of an instance variable with the same name. If
    you need more complicated accessors, such as setters that set a
    differently named variable, or getters that return a value computed from
    two different variables, then you'll have to define those
    yourself.




    7.1.6. Defining Operators


    We'd like the + operator to perform vector
    addition of two Point objects, the
    * operator to multiply a Point by
    a scalar, and the unary operator to do the
    equivalent of multiplying by –1. Method-based
    operators such as + are simply methods with
    punctuation for names. Because there are unary and binary forms of the
    operator, Ruby uses the method name
    –@ for unary minus. Here is a version of the
    Point class with mathematical operators defined:


    class Point
    attr_reader :x, :y # Define accessor methods for our instance variables

    def initialize(x,y)
    @x,@y = x, y
    end

    def +(other) # Define + to do vector addition
    Point.new(@x + other.x, @y + other.y)
    end

    def -@ # Define unary minus to negate both coordinates
    Point.new(-@x, -@y)
    end

    def *(scalar) # Define * to perform scalar multiplication
    Point.new(@x*scalar, @y*scalar)
    end
    end



    Take a look at the body of the + method. It is
    able to use the @x instance variable of
    self—the object that the method is invoked on. But it
    cannot access @x in the other
    Point object. Ruby simply does not have a syntax for
    this; all instance variable references implicitly use
    self. Our + method, therefore, is
    dependent on the x and y getter
    methods. (We'll see later that it is possible to restrict the visibility
    of methods so that objects of the same class can use each other's
    methods, but code outside the class cannot use them.)



    Type Checking and Duck Typing


    Our + method does not do any type checking; it simply assumes that it has been passed a
    suitable object. It is fairly common in Ruby programming to be loose
    about the definition of "suitable." In the case of our
    + method, any object that has methods named
    x and y will do, as long as
    those methods expect no arguments and return a number of some sort. We
    don't care if the argument actually is a point,
    as long as it looks and behaves like a point. This approach is
    sometimes called "duck typing," after the adage "if it walks like a
    duck and quacks like a duck, it must be a duck."


    If we pass an object to + that is not
    suitable, Ruby will raise an exception. Attempting to add
    3 to a point, for example, results in this error
    message:


    NoMethodError: undefined method `x' for 3:Fixnum
    from ./point.rb:37:in `+'



    Translated, this tells us that the Fixnum
    3 does not have a method named
    x, and that this error arose in the
    + method of the Point class.
    This is all the information we need to figure out the source of the
    problem, but it is somewhat obscure. Checking the class of method
    arguments may make it easier to debug code that uses that method. Here
    is a version of the method with class verification:


    def +(other)
    raise TypeError, "Point argument expected" unless other.is_a? Point
    Point.new(@x + other.x, @y + other.y)
    end



    Here is a looser version of type checking that provides improved
    error messages but still allows duck typing:


    def +(other)
    raise TypeError, "Point-like argument expected" unless
    other.respond_to? :x and other.respond_to? :y
    Point.new(@x + other.x, @y + other.y)
    end



    Note that this version of the method still assumes that the
    x and y methods return numbers.
    We'd get an obscure error message if one of these methods returned a
    string, for example.


    Another approach to type checking occurs after the fact. We can
    simply handle any exceptions that occur during execution of the method
    and raise a more appropriate exception of our own:


    def +(other)         # Assume that other looks like a Point
    Point.new(@x + other.x, @y + other.y)
    rescue # If anything goes wrong above
    raise TypeError, # Then raise our own exception
    "Point addition with an argument that does not quack like a Point!"
    end





    Note that our * method expects a numeric
    operand, not a Point. If p is
    point, then we can write p*2. As our class is
    written, however, we cannot write 2*p. That second
    expression invokes the
    * method of the Integer class,
    which doesn't know how to work with Point objects.
    Because the Integer class doesn't know how to
    multiply by a Point, it asks the point for help by
    calling its coerce method. (See Section 3.8.7.4 for more details.) If we want the expression
    2*p to return the same result as
    p*2, we can define a coerce
    method:


    # If we try passing a Point to the * method of an Integer, it will call
    # this method on the Point and then will try to multiply the elements of
    # the array. Instead of doing type conversion, we switch the order of
    # the operands, so that we invoke the * method defined above.
    def coerce(other)
    [self, other]
    end





    7.1.7. Array and Hash Access with [ ]


    Ruby uses square brackets for array and hash access, and allows any class
    to define a [] method and use these brackets itself.
    Let's define a [] method for our class to allow
    Point objects to be treated as read-only arrays of
    length 2, or as read-only hashes with keys
    :x and :y:


    # Define [] method to allow a Point to look like an array or
    # a hash with keys :x and :y
    def [](index)
    case index
    when 0, -2: @x # Index 0 (or -2) is the X coordinate
    when 1, -1: @y # Index 1 (or -1) is the Y coordinate
    when :x, "x": @x # Hash keys as symbol or string for X
    when :y, "y": @y # Hash keys as symbol or string for Y
    else nil # Arrays and hashes just return nil on bad indexes
    end
    end





    7.1.8. Enumerating Coordinates


    If a Point object can behave like an array with
    two elements, then perhaps we ought to be able to iterate through those
    elements as we can with a true array. Here is a definition of the
    each iterator for our Point class.
    Because a Point always has exactly two elements, our
    iterator doesn't have to loop; it can simply call yield twice:


    # This iterator passes the X coordinate to the associated block, and then
    # passes the Y coordinate, and then returns. It allows us to enumerate
    # a point as if it were an array with two elements. This each method is
    # required by the Enumerable module.
    def each
    yield @x
    yield @y
    end



    With this iterator defined, we can write code like this:


    p = Point.new(1,2)
    p.each {|x| print x } # Prints "12"



    More importantly, defining the each iterator
    allows us to mix in the methods of the Enumerable
    module, all of which are defined in terms of each.
    Our class gains over 20 iterators by adding a single line:


    include Enumerable



    If we do this, then we can write interesting code like
    this:


    # Is the point P at the origin?
    p.all? {|x| x == 0 } # True if the block is true for all elements





    7.1.9. Point Equality


    As our class is currently defined, two distinct
    Point instances are never equal to each other, even
    if their X and Y coordinates are the same. To remedy this, we must
    provide an implementation of the == operator. (You may want to reread
    Section 3.8.5 in Chapter 3 to refresh
    your memory about Ruby's various notions of equality.)


    Here is an == method for
    Point:


    def ==(o)               # Is self == o?
    if o.is_a? Point # If o is a Point object
    @x==o.x && @y==o.y # then compare the fields.
    elsif # If o is not a Point
    false # then, by definition, self != o.
    end
    end




    Duck Typing and Equality


    The + operator we defined earlier did no type checking at all: it
    works with any argument object with x and
    y methods that return numbers. This
    == method is implemented differently; instead of
    allowing duck typing, it requires that the argument is a
    Point. This is an implementation choice. The
    implementation of == above chooses to define
    equality so that an object
    cannot be equal to a Point unless it is itself a
    Point.


    Implementations may be stricter or more liberal than this. The
    implementation above uses the is_a? predicate to
    test the class of the argument. This allows an instance of a subclass
    of Point to be equal to a Point.
    A stricter implementation would use instance_of? to
    disallow subclass instances. Similarly, the implementation above uses
    == to compare the X and Y coordinates. For numbers,
    the == operator allows type conversion, which means
    that the point (1,1) is equal to
    (1.0,1.0). This is probably as it should be, but a
    stricter definition of equality could use eql? to
    compare the coordinates.


    A more liberal definition of equality would support duck typing.
    Some caution is required,
    however. Our == method should not raise a
    NoMethodError if the argument object does not have
    x and y methods. Instead, it
    should simply return false:


    def ==(o)                  # Is self == o?
    @x == o.x && @y == o.y # Assume o has proper x and y methods
    rescue # If that assumption fails
    false # Then self != o
    end





    Recall from Section 3.8.5 that Ruby objects also
    define an eql? method for testing equality. By
    default, the eql? method, like the
    == operator, tests object identity rather than
    equality of object content. Often, we want eql? to
    work just like the == operator, and we can accomplish
    this with an alias:


    class Point
    alias eql? ==
    end



    On the other hand, there are two reasons we might want
    eql? to be different from ==.
    First, some classes define eql? to perform a stricter
    comparison than ==. In Numeric and
    its subclasses, for example, == allows type
    conversion and eql? does not. If we believe that the
    users of our Point class might want to be able to
    compare instances in two different ways, then we might follow this
    example. Because points are just two numbers, it would make sense to
    follow the example set by Numeric here. Our
    eql? method would look much like the
    == method, but it would use eql?
    to compare point coordinates instead of ==:


    def eql?(o)             
    if o.instance_of? Point
    @x.eql?(o.x) && @y.eql?(o.y)
    elsif
    false
    end
    end



    As an aside, note that this is the right approach for any classes
    that implement collections (sets, lists, trees) of arbitrary objects.
    The == operator should compare the members of the
    collection using their == operators, and the
    eql? method should compare the members using their
    eql? methods.


    The second reason to implement an eql?
    method that is different from the == operator
    is if you want instances of your class to behave specially when used as
    a hash key. The Hash class uses
    eql? to compare hash keys (but not values). If you
    leave eql? undefined, then hashes will compare
    instances of your class by object identity. This means that if you
    associate a value with a key p, you will only be able
    to retrieve that value with the exact same object p.
    An object q won't work, even if p ==
    q
    . Mutable objects do not work well as hash keys, but leaving
    eql? undefined neatly sidesteps the problem. (See
    Section 3.4.2 for more on hashes and mutable
    keys.)


    Because eql? is used for hashes, you must never
    implement this method by itself. If you define an
    eql? method, you must also define a
    hash method to compute a hashcode for your object. If
    two objects are equal according to eql?, then their
    hash methods must return the
    same value. (Two unequal objects may return the same hashcode, but you
    should avoid this to the extent possible.)


    Implementing optimal hash methods can be very
    tricky. Fortunately, there is a simple way to compute perfectly adequate
    hashcodes for just about any class: simply combine the hashcodes of all
    the objects referenced by your class. (More precisely: combine the
    hashcodes of all the objects compared by your eql?
    method.) The trick is to combine the hashcodes in the proper way. The
    following hash method is not a
    good one:


    def hash
    @x.hash + @y.hash
    end



    The problem with this method is that it returns the same hashcode
    for the point (1,0) as it does for the point
    (0,1). This is legal, but it leads to poor
    performance when points are used as hash keys. Instead, we should mix
    things up a bit:


    def hash
    code = 17
    code = 37*code + @x.hash
    code = 37*code + @y.hash
    # Add lines like this for each significant instance variable
    code # Return the resulting code
    end



    This general-purpose hashcode recipe should be suitable for most
    Ruby classes. It, and its constants 17 and
    37, are adapted from the book Effective
    Java
    by Joshua Bloch (Prentice Hall).




    7.1.10. Ordering Points


    Suppose we wish to define an ordering for Point
    objects so that we can compare them and sort them. There are a number of
    ways to order points, but we'll chose to arrange them based on their
    distance from the origin. This distance (or magnitude) is computed by
    the Pythagorean theorem: the square root of the sum of the squares of
    the X and Y coordinates.


    To define this ordering for Point objects, we
    need only define the <=> operator (see Section 4.6.6) and include the
    Comparable module. Doing this mixes in
    implementations of the equality and relational operators that are based
    on our implementation of the general <=>
    operator we defined. The <=> operator should
    compare self to the object it is passed. If
    self is less than that object (closer to the origin,
    in this case), it should return –1. If the two
    objects are equal, it should return 0. And if
    self is greater than the argument object, the method
    should return 1. (The method should return
    nil if the argument object and
    self are of incomparable types.) The following code
    is our implementation of <=>. There are two things to note about it. First, it doesn't
    bother with the Math.sqrt method and instead simply
    compares the sum of the squares of the coordinates. Second, after
    computing the sums of the squares, it simply delegates to the
    <=> operator of the Float
    class:


    include Comparable   # Mix in methods from the Comparable module.

    # Define an ordering for points based on their distance from the origin.
    # This method is required by the Comparable module.
    def <=>(other)
    return nil unless other.instance_of? Point
    @x**2 + @y**2 <=> other.x**2 + other.y**2
    end



    Note that the Comparable module defines an
    == method that uses our definition of
    <=>. Our distance-based comparison operator
    results in an == method that considers the points
    (1,0) and (0,1) to be equal.
    Because our Point class explicitly defines its own
    == method, however, the == method
    of Comparable is never invoked. Ideally, the
    == and <=> operators should
    have consistent definitions of equality. This was not possible in our
    Point class, and we end up with operators that allow
    the following:


    p,q = Point.new(1,0), Point.new(0,1)
    p == q # => false: p is not equal to q
    p < q # => false: p is not less than q
    p > q # => false: p is not greater than q



    Finally, It is worth noting here that the
    Enumerable module defines several methods, such as
    sort, min, and
    max, that only work if the objects being enumerated
    define the <=> operator.




    7.1.11. A Mutable Point


    The Point class we've been developing
    is immutable: once a point object has
    been created, there is no public API to change the X and Y coordinates
    of that point. This is probably as it should be. But let's detour and
    investigate some methods we might add if we wanted points to be mutable.


    First of all, we'd need x= and
    y= setter methods to allow the X and Y coordinates to
    be set directly. We could define these methods explicitly, or simply
    change our attr_reader line to
    attr_accessor:


    attr_accessor :x, :y



    Next, we'd like an alternative to the +
    operator for when we want to add the coordinates of point
    q to the coordinates of point p,
    and modify point p rather than creating and returning
    a new Point object. We'll call this method
    add!, with the exclamation mark indicating that it
    alters the internal state of the object on which it is invoked:


    def add!(p)          # Add p to self, return modified self
    @x += p.x
    @y += p.y
    self
    end



    When defining a mutator method, we normally only add an
    exclamation mark to the name if there is a nonmutating version of the
    same method. In this case, the name add! makes sense
    if we also define an add method that returns a new
    object, rather than altering its receiver. A nonmutating version of a
    mutator method is often written simply by creating a copy of
    self and invoking the mutator on the copied
    object:


    def add(p)           # A nonmutating version of add!
    q = self.dup # Make a copy of self
    q.add!(p) # Invoke the mutating method on the copy
    end



    In this trivial example, our add method works
    just like the + operator we've already defined, and
    it's not really necessary. So if we don't define a nonmutating
    add, we should consider dropping the exclamation mark
    from add! and allowing the name of the method itself
    ("add" instead of "plus") to indicate that it is a mutator.




    7.1.12. Quick and Easy Mutable Classes


    If you want a mutable Point class, one way to
    create it is with Struct. Struct
    is a core Ruby class that generates other classes. These generated
    classes have accessor methods for the named fields you specify. There
    are two ways to create a new class with
    Struct.new:


    Struct.new("Point", :x, :y)  # Creates new class Struct::Point
    Point = Struct.new(:x, :y) # Creates new class, assigns to Point




    Naming Anonymous Classes


    The second line in the code relies on a curious fact about Ruby
    classes: if you assign an unnamed class object to a constant, the name
    of that constant becomes the name of a class. You can observe this
    same behavior if you use the Class.new
    constructor:


    C = Class.new   # A new class with no body, assigned to a constant
    c = C.new # Create an instance of the class
    c.class.to_s # => "C": constant name becomes class name





    Once a class has been created with Struct.new,
    you can use it like any other class. Its new method
    will expect values for each of the named fields you specify, and its
    instance methods provide read and write accessors for those
    fields:


    p = Point.new(1,2)   # => #<struct Point x=1, y=2>
    p.x # => 1
    p.y # => 2
    p.x = 3 # => 3
    p.x # => 3



    Structs also define the [] and
    []= operators for array and hash-style indexing, and
    even provide each and each_pair
    iterators for looping through the values held in an instance of the
    struct:


    p[:x] = 4             # => 4: same as p.x =
    p[:x] # => 4: same as p.x
    p[1] # => 2: same as p.y
    p.each {|c| print c} # prints "42"
    p.each_pair {|n,c| print n,c } # prints "x4y2"



    Struct-based classes have a working ==
    operator, can be used as hash keys (though caution is necessary because
    they are mutable), and even define a helpful to_s
    method:


    q = Point.new(4,2)
    q == p # => true
    h = {q => 1} # Create a hash using q as a key
    h[p] # => 1: extract value using p as key
    q.to_s # => "#<struct Point x=4, y=2>"



    A Point class defined as a struct does not have
    point-specific methods like add! or the
    <=> operator defined earlier in this chapter.
    There is no reason we can't add them, though. Ruby class definitions are
    not static. Any class (including classes defined with
    Struct.new) can be "opened" and have methods added to
    it. Here's a Point class initially defined as a
    Struct, with point-specific methods added:


    Point = Struct.new(:x, :y)   # Create new class, assign to Point
    class Point # Open Point class for new methods
    def add!(other) # Define an add! method
    self.x += other.x
    self.y += other.y
    self
    end

    include Comparable # Include a module for the class
    def <=>(other) # Define the <=> operator
    return nil unless other.instance_of? Point
    self.x**2 + self.y**2 <=> other.x**2 + other.y**2
    end
    end



    As noted at the beginning of this section, the
    Struct class is designed to create mutable classes.
    With just a bit of work, however, we can make a
    Struct-based class immutable:


    Point = Struct.new(:x, :y)  # Define mutable class
    class Point # Open the class
    undef x=,y=,[]= # Undefine mutator methods
    end





    7.1.13. A Class Method


    Let's take another approach to adding Point
    objects together. Instead of invoking an instance method of one point
    and passing another point to that method, let's write a method named
    sum that takes any number of Point
    objects, adds them together, and returns a new Point.
    This method is not an instance method invoked on a
    Point object. Rather, it is a class method, invoked through
    the Point class itself. We might invoke the
    sum method like this:


    total = Point.sum(p1, p2, p3)  # p1, p2 and p3 are Point objects



    Keep in mind that the expression Point refers
    to a Class object that represents our point class. To
    define a class method for the Point class, what we
    are really doing is defining a singleton method of the
    Point object. (We covered singleton methods in Section 6.1.4.) To define a singleton method, use the
    def statement as usual, but specify the object on
    which the method is to be defined as well as the name of the method. Our
    class method sum is defined like this:


    class Point
    attr_reader :x, :y # Define accessor methods for our instance variables

    def Point.sum(*points) # Return the sum of an arbitrary number of points
    x = y = 0
    points.each {|p| x += p.x; y += p.y }
    Point.new(x,y)
    end

    # ...the rest of class omitted here...
    end





    This definition of the class method names the class explicitly,
    and mirrors the syntax used to invoke the method. Class methods can also
    be defined using self instead of the class name.
    Thus, this method could also be written like this:


    def self.sum(*points)  # Return the sum of an arbitrary number of points
    x = y = 0
    points.each {|p| x += p.x; y += p.y }
    Point.new(x,y)
    end



    Using self instead of Point makes the code slightly less
    clear, but it's an application of the DRY (Don't Repeat Yourself)
    principle. If you use self instead of the class name,
    you can change the name of a class without having to edit the definition
    of its class methods.


    There is yet another technique for defining class methods. Though
    it is less clear than the previously shown technique, it can be handy
    when defining multiple class methods, and you are likely to see it used
    in existing code:


    # Open up the Point object so we can add methods to it
    class << Point # Syntax for adding methods to a single object
    def sum(*points) # This is the class method Point.sum
    x = y = 0
    points.each {|p| x += p.x; y += p.y }
    Point.new(x,y)
    end

    # Other class methods can be defined here
    end



    This technique can also be used inside the class definition, where
    we can use self instead of repeating the class
    name:


    class Point
    # Instance methods go here

    class << self
    # Class methods go here
    end
    end



    We'll learn more about this syntax in Section 7.7.




    7.1.14. Constants


    Many classes can benefit from the definition of some associated
    constants. Here are some constants that might be useful for our
    Point class:


    class Point
    def initialize(x,y) # Initialize method
    @x,@y = x, y
    end

    ORIGIN = Point.new(0,0)
    UNIT_X = Point.new(1,0)
    UNIT_Y = Point.new(0,1)

    # Rest of class definition goes here
    end



    Inside the class definition, these constants can be referred to by
    their unqualified names. Outside the definition, they must be prefixed
    by the name of the class, of course:


    Point::UNIT_X + Point::UNIT_Y   # => (1,1)



    Note that because our constants in this example refer to instances
    of the class, we cannot define the constants until after we've defined
    the initialize method of the class. Also, keep in mind that it is perfectly
    legal to define constants in the Point class from
    outside the class:


    Point::NEGATIVE_UNIT_X = Point.new(-1,0)





    7.1.15. Class Variables


    Class variables are visible to, and shared by, the class
    methods and the instance methods of a class, and also by the class
    definition itself. Like instance variables, class variables are
    encapsulated; they can be used by the implementation of a class, but
    they are not visible to the users of a class. Class variables have names
    that begin with @@.


    There is no real need to use class variables in our
    Point class, but for the purposes of this tutorial,
    let's suppose that we want to collect data about the number of
    Point objects that are created and their average
    coordinates. Here's how we might write the code:


    class Point
    # Initialize our class variables in the class definition itself
    @@n = 0 # How many points have been created
    @@totalX = 0 # The sum of all X coordinates
    @@totalY = 0 # The sum of all Y coordinates

    def initialize(x,y) # Initialize method
    @x,@y = x, y # Sets initial values for instance variables

    # Use the class variables in this instance method to collect data
    @@n += 1 # Keep track of how many Points have been created
    @@totalX += x # Add these coordinates to the totals
    @@totalY += y
    end

    # A class method to report the data we collected
    def self.report
    # Here we use the class variables in a class method
    puts "Number of points created: #@@n"
    puts "Average X coordinate: #{@@totalX.to_f/@@n}"
    puts "Average Y coordinate: #{@@totalY.to_f/@@n}"
    end
    end



    The thing to notice about this code is that class variables are
    used in instance methods, class methods, and in the class definition
    itself, outside of any method. Class variables are fundamentally
    different than instance variables. We've seen that instance variables
    are always evaluated in reference to self. That is
    why an instance variable reference in a class definition or class method
    is completely different from an instance variable reference in an
    instance method. Class variables, on the other hand, are always
    evaluated in reference to the class object created by the enclosing
    class definition statement.




    7.1.16. Class Instance Variables


    Classes are objects and can have instance variables just as other
    objects can. The instance
    variables of a class—often called class instance variables—are not the
    same as class variables. But they are similar enough that they can often
    be used instead of class variables.


    An instance variable used inside a class
    definition but outside an instance method definition is a class instance
    variable. Like class variables, class instance variables are associated
    with the class rather than with any particular instance of the class. A
    disadvantage of class instance
    variables is that they cannot be used within instance methods as class
    variables can. Another disadvantage is the potential for confusing them
    with ordinary instance variables. Without the distinctive punctuation
    prefixes, it may be more difficult to remember whether a variable is
    associated with instances or with the class object.


    One of the most important advantages of class instance variables
    over class variables has to do with the confusing behavior of class
    variables when subclassing an existing class. We'll return to this point
    later in the chapter.


    Let's port our statistics-gathering version of the
    Point class to use class instance variables instead
    of class variables. The only difficulty is that because class instance
    variables cannot be used from instance methods, we must move the
    statistics gathering code out of the initialize method (which is an
    instance method) and into the new class method used
    to create points:


    class Point
    # Initialize our class instance variables in the class definition itself
    @n = 0 # How many points have been created
    @totalX = 0 # The sum of all X coordinates
    @totalY = 0 # The sum of all Y coordinates

    def initialize(x,y) # Initialize method
    @x,@y = x, y # Sets initial values for instance variables
    end

    def self.new(x,y) # Class method to create new Point objects
    # Use the class instance variables in this class method to collect data
    @n += 1 # Keep track of how many Points have been created
    @totalX += x # Add these coordinates to the totals
    @totalY += y

    super # Invoke the real definition of new to create a Point
    # More about super later in the chapter
    end

    # A class method to report the data we collected
    def self.report
    # Here we use the class instance variables in a class method
    puts "Number of points created: #@n"
    puts "Average X coordinate: #{@totalX.to_f/@n}"
    puts "Average Y coordinate: #{@totalY.to_f/@n}"
    end
    end





    Because class instance variables are just instance variables of
    class objects, we can use attr,
    attr_reader, and attr_accessor to
    create accessor methods for them. The trick, however, is to invoke these
    metaprogramming methods in the right context. Recall that one way to
    define class methods uses the syntax class <<
    self
    . This same syntax allows us to define attribute accessor
    methods for class instance variables:


    class << self
    attr_accessor :n, :totalX, :totalY
    end



    With these accessors defined, we can refer to our raw data as
    Point.n, Point.totalX, and
    Point.totalY.