<ppdoc>
<copyright>
    Copyright (c) 2001 by Addison Wesley Longman.  This
    material may be distributed only subject to the terms and
    conditions set forth in the Open Publication License, v1.0 or
    later (the latest version is presently available at
    http://www.opencontent.org/openpub/).
</copyright>
<chapter  ref="xref-000001" name="Object-Oriented Design Libraries">
<p/>
One of the interesting things about Ruby is the way it blurs the
distinction between design and implementation. Ideas that have to be
expressed at the design level in other languages can be implemented
directly in Ruby.
<p/>
To help in this process, Ruby has support for some design-level strategies.
<p/>
<ul>
<li> <b>The Visitor pattern</b> (Design Patterns,
  ) is a way of traversing a collection 
  without having to know the internal organization of that collection.
</li><li> <b>Delegation</b> is a way of composing classes more flexibly and
  dynamically than can be done using standard inheritance.
</li><li> <b>The Singleton pattern</b> is a way of ensuring that only
  one instantiation of a particular class exists at a time.
</li><li> <b>The Observer pattern</b> implements a protocol allowing one 
  object to notify a set of interested objects when certain changes
  have occurred.
</li></ul>
<p/>
Normally, all four of these strategies require explicit code each time 
they're implemented. With Ruby, they can be abstracted into a library
and reused freely and transparently.
<p/>
Before we get into the proper library descriptions, let's get the
simplest strategy out of the way.
<p/>
<section>The Visitor Pattern</section>
<p/>
It's the method <meth>each</meth>.
<p/>
<library name="delegate">
<p/>
Object delegation is a way of <em>composing</em> objects---extending an
object with the capabilities of another---at runtime. This promotes
writing flexible, decoupled code, as there are no compile-time
dependencies between the users of the overall class and the delegates.
<p/>
The Ruby <classname>Delegator</classname> class implements a simple but powerful
delegation scheme, where requests are automatically forwarded from a
master class to delegates or their ancestors, and where the delegate
can be changed at runtime with a single method call.
<p/>
The <tt>delegate.rb</tt> library provides two mechanisms for allowing an
object to forward messages to a delegate.
<p/>
<ol>
<p/>
<li> For simple cases where the class of the delegate is fixed,
  arrange for the master class to be a subclass of
  <classname>DelegateClass</classname>, passing the name of the class to be
  delegated as a parameter (Example 1). Then, in your class's
  <meth>initialize</meth> method, you'd call the superclass, passing in
  the object to be delegated.  For example, to declare a class
  <classname>Fred</classname> that also supports all the methods in <classname>Flintstone</classname>, you'd
  write
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    class Fred < DelegateClass(Flintstone)
      def initialize
        # ...
        super(Flintstone.new(...))
      end
      # ...
     end
]]></fullcode>
class<nbsp/>Fred<nbsp/>&lt;<nbsp/>DelegateClass(Flintstone)
<nbsp/><nbsp/>def<nbsp/>initialize
<nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>...
<nbsp/><nbsp/><nbsp/><nbsp/>super(Flintstone.new(...))
<nbsp/><nbsp/>end
<nbsp/><nbsp/>#<nbsp/>...
<nbsp/>end
</alltt>
</codefragment>
   This is subtly different from using subclassing. With subclassing,
   there is only one object, which has the methods and the defined
   class, its parent, and their ancestors. With delegation there are
   two objects, linked so that calls to one may be delegated to the
   other.
<p/>
 </li><li> For cases where the delegate needs to be dynamic, make the
   master class a subclass of <classname>SimpleDelegator</classname> (Example 2). You
   can also add delegation capabilities to an existing object using
   <classname>SimpleDelegator</classname> (Example 3). In these cases, you can call the
   <meth>__setobj__</meth> method in <classname>SimpleDelegator</classname> to
   change the object being delegated at runtime.
<p/>
</li></ol>
<p/>
  <b>Example 1.</b> Use the <classname>DelegateClass</classname> method and subclass
  the result when you need a class with its own behavior that also
  delegates to an object of another class. In this example, we assume
  that the <tt>@sizeInInches</tt> array is large, so we want only one copy
  of it. We then define a class that accesses it, converting the
  values to feet.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[    require 'delegate'
    
    sizeInInches = [ 10, 15, 22, 120 ]

    class Feet < DelegateClass(Array)
      def initialize(arr)
        super(arr)
      end
      def [](*n)
        val = super(*n)
        case val.type
        when Numeric; val/12.0
        else;         val.collect {|i| i/12.0}
        end
      end
    end
]]></fullcode>
require<nbsp/>'delegate'
<p/>
sizeInInches<nbsp/>=<nbsp/>[<nbsp/>10,<nbsp/>15,<nbsp/>22,<nbsp/>120<nbsp/>]
<p/>
class<nbsp/>Feet<nbsp/>&lt;<nbsp/>DelegateClass(Array)
<nbsp/><nbsp/>def<nbsp/>initialize(arr)
<nbsp/><nbsp/><nbsp/><nbsp/>super(arr)
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>[](*n)
<nbsp/><nbsp/><nbsp/><nbsp/>val<nbsp/>=<nbsp/>super(*n)
<nbsp/><nbsp/><nbsp/><nbsp/>case<nbsp/>val.type
<nbsp/><nbsp/><nbsp/><nbsp/>when<nbsp/>Numeric;<nbsp/>val/12.0
<nbsp/><nbsp/><nbsp/><nbsp/>else;<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>val.collect<nbsp/>{|i|<nbsp/>i/12.0}
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<p/>
<codefragment>
<fullcode><![CDATA[!-    require 'delegate'
!-    sizeInInches = [ 10, 15, 22, 120 ]
!-
!-    class Feet < DelegateClass(Array)
!-      def initialize(arr)
!-        super(arr)
!-      end
!-      def [](*n)
!-        val = super(*n)
!-        case val.type
!-        when Numeric; val/12.0
!-        else;         val.collect {|i| i/12.0}
!-        end
!-      end
!-    end
    sizeInFeet = Feet.new(sizeInInches)
    sizeInInches[0..3]
    sizeInFeet[0..3]
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>sizeInFeet<nbsp/>=<nbsp/>Feet.new(sizeInInches)</tt></td>
</tr>
<tr>
  <td><tt>sizeInInches[0..3]</tt></td>
  <td>&#187;</td>
  <td><tt>[10,<nbsp/>15,<nbsp/>22,<nbsp/>120]</tt></td>
</tr>
<tr>
  <td><tt>sizeInFeet[0..3]</tt></td>
  <td>&#187;</td>
  <td><tt>[0.8333333333,<nbsp/>1.25,<nbsp/>1.833333333,<nbsp/>10.0]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
  <b>Example 2.</b> Use subclass <classname>SimpleDelegator</classname> when you want an
  object that both has its own behavior <em>and</em> delegates to
  different objects during its lifetime.  This is an example of the
  State
  pattern.  
  Objects of class <classname>TicketOffice</classname> sell tickets if a
  seller is available, or tell you to come back tomorrow if there is
  no seller.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[     require 'delegate'

     class TicketSeller
       def sellTicket()
         return 'Here is a ticket'
       end
     end

     class NoTicketSeller
       def sellTicket()
         "Sorry-come back tomorrow"
        end
     end

     class TicketOffice < SimpleDelegator
       def initialize
         @seller = TicketSeller.new
         @noseller = NoTicketSeller.new
         super(@seller)
       end
       def allowSales(allow = true)
         __setobj__(allow ? @seller : @noseller)
         allow
       end
     end
]]></fullcode>
require<nbsp/>'delegate'
<p/>
class<nbsp/>TicketSeller
<nbsp/><nbsp/>def<nbsp/>sellTicket()
<nbsp/><nbsp/><nbsp/><nbsp/>return<nbsp/>'Here<nbsp/>is<nbsp/>a<nbsp/>ticket'
<nbsp/><nbsp/>end
end
<p/>
class<nbsp/>NoTicketSeller
<nbsp/><nbsp/>def<nbsp/>sellTicket()
<nbsp/><nbsp/><nbsp/><nbsp/>"Sorry-come<nbsp/>back<nbsp/>tomorrow"
<nbsp/><nbsp/><nbsp/>end
end
<p/>
class<nbsp/>TicketOffice<nbsp/>&lt;<nbsp/>SimpleDelegator
<nbsp/><nbsp/>def<nbsp/>initialize
<nbsp/><nbsp/><nbsp/><nbsp/>@seller<nbsp/>=<nbsp/>TicketSeller.new
<nbsp/><nbsp/><nbsp/><nbsp/>@noseller<nbsp/>=<nbsp/>NoTicketSeller.new
<nbsp/><nbsp/><nbsp/><nbsp/>super(@seller)
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>allowSales(allow<nbsp/>=<nbsp/>true)
<nbsp/><nbsp/><nbsp/><nbsp/>__setobj__(allow<nbsp/>?<nbsp/>@seller<nbsp/>:<nbsp/>@noseller)
<nbsp/><nbsp/><nbsp/><nbsp/>allow
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<p/>
<codefragment>
<fullcode><![CDATA[!-     require 'delegate'
!-
!-     class TicketSeller
!-       def sellTicket()
!-         return 'Here is a ticket'
!-       end
!-     end
!-
!-     class NoTicketSeller
!-       def sellTicket()
!-         "Sorry-come back tomorrow"
!-        end
!-     end
!-
!-     class TicketOffice < SimpleDelegator
!-       def initialize
!-         @seller = TicketSeller.new
!-         @noseller = NoTicketSeller.new
!-         super(@seller)
!-       end
!-       def allowSales(allow = true)
!-         __setobj__(allow ? @seller : @noseller)
!-         allow
!-       end
!-     end
     to = TicketOffice.new
     to.sellTicket
     to.allowSales(false)
     to.sellTicket
     to.allowSales(true)
     to.sellTicket
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>to<nbsp/>=<nbsp/>TicketOffice.new</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Here<nbsp/>is<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
  <td><tt>to.allowSales(false)</tt></td>
  <td>&#187;</td>
  <td><tt>false</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Sorry-come<nbsp/>back<nbsp/>tomorrow"</tt></td>
</tr>
<tr>
  <td><tt>to.allowSales(true)</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Here<nbsp/>is<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
  <b>Example 3.</b> Create <classname>SimpleDelegator</classname> objects when you want
  a single object to delegate all its methods to two or more other
  objects.
<p/>
<codefragment>
<fullcode><![CDATA[     # Example 3 - delegate from existing object
!-     require 'delegate'
!-     class TicketSeller
!-       def sellTicket()
!-         "Here's a ticket"
!-       end
!-     end
!-
!-     class NoTicketSeller
!-       def sellTicket()
!-         "Sorry-come back tomorrow"
!-       end
!-     end
!-
     seller   = TicketSeller.new
     noseller = NoTicketSeller.new
     to = SimpleDelegator.new(seller)
     to.sellTicket
     to.sellTicket
     to.__setobj__(noseller) #!sh!
     to.sellTicket
     to.__setobj__(seller)   #!sh!
     to.sellTicket
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>#<nbsp/>Example<nbsp/>3<nbsp/>-<nbsp/>delegate<nbsp/>from<nbsp/>existing<nbsp/>object</tt></td>
</tr>
<tr>
<td colspan="3"><tt>seller<nbsp/><nbsp/><nbsp/>=<nbsp/>TicketSeller.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>noseller<nbsp/>=<nbsp/>NoTicketSeller.new</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to<nbsp/>=<nbsp/>SimpleDelegator.new(seller)</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to.__setobj__(noseller)</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Sorry-come<nbsp/>back<nbsp/>tomorrow"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>to.__setobj__(seller)</tt></td>
</tr>
<tr>
  <td><tt>to.sellTicket</tt></td>
  <td>&#187;</td>
  <td><tt>"Here's<nbsp/>a<nbsp/>ticket"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</library>
<p/>
<library name="observer">
<p/>
  The Observer pattern, also known as
  Publish/Subscribe,  
  provides a simple mechanism for one object to
  inform a set of interested third-party objects when its state
  changes.  
<p/>
  In the Ruby implementation, the notifying class mixes in the
  <modulename>Observable</modulename> module, which provides the methods for managing the
associated observer objects.
<table>
<p/>
  <toprule/><tr>
  <td>add_observer(<obj>obj</obj>)</td>
  <td>Add <obj>obj</obj> as an observer on this
   object. <obj>obj</obj> will now receive notifications.</td>
</tr>
<tr>
  <td>delete_observer(<obj>obj</obj>)</td>
  <td>Delete <obj>obj</obj> as an observer on this
   object. It will no longer receive notifications.</td>
</tr>
<tr>
  <td>delete_observers</td>
  <td>Delete all observers associated with this
   object.</td>
</tr>
<tr>
  <td>count_observers</td>
  <td>Return the count of observers associated with this 
  object.</td>
</tr>
<tr>
  <td>changed(<obj>newState</obj>=<const>true</const>)</td>
  <td>Set the changed state of this
  object. Notifications will be sent only if the changed state is
  <const>true</const>.</td>
</tr>
<tr>
  <td>changed?</td>
  <td>Query the changed state of this object.</td>
</tr>
<tr>
  <td>notify_observers(<obj>*args</obj>)</td>
  <td>If this object's changed state is
  true, invoke the <meth>update</meth> method in each currently associated 
  observer in turn, passing it the given arguments. The changed state is then
  set to <const>false</const>.</td>
</tr>
<bottomrule/></table>
<p/>
The observers must implement the <meth>update</meth> method to receive
notifications. 
<p/>
<codefragment>
<alltt><fullcode><![CDATA[!-class Price
!-  @p = [ 83, 75, 90, 134, 134, 112, 79 ]
!-  @i = 0
!-  def Price.fetch(symbol)
!-    exit if @i >= @p.length
!-    p = @p[@i]
!-    @i += 1
!-    p
!-  end
!-end
require "observer"

  class Ticker # Periodically fetch a stock price
    include Observable

    def initialize(symbol)
      @symbol = symbol
    end

    def run
      lastPrice = nil
      loop do
        price = Price.fetch(@symbol)
        print "Current price: #{price}\n"
        if price != lastPrice
          changed                 # notify observers
          lastPrice = price
          notify_observers(Time.now, price)
        end
      end    
    end
  end

  class Warner
    def initialize(ticker, limit)
      @limit = limit
      ticker.add_observer(self)   # all warners are observers
    end
  end
    
  class WarnLow < Warner
    def update(time, price)       # callback for observer
      if price < @limit
        print "--- #{time.to_s}: Price below #@limit: #{price}\n"
      end
    end
  end

  class WarnHigh < Warner
    def update(time, price)       # callback for observer
      if price > @limit
        print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
      end
    end
  end

ticker = Ticker.new("MSFT")
WarnLow.new(ticker, 80)
WarnHigh.new(ticker, 120)
ticker.run
]]></fullcode>
require<nbsp/>"observer"
<p/>
<nbsp/><nbsp/>class<nbsp/>Ticker<nbsp/>#<nbsp/>Periodically<nbsp/>fetch<nbsp/>a<nbsp/>stock<nbsp/>price
<nbsp/><nbsp/><nbsp/><nbsp/>include<nbsp/>Observable
<p/>
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>initialize(symbol)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>@symbol<nbsp/>=<nbsp/>symbol
<nbsp/><nbsp/><nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>run
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>lastPrice<nbsp/>=<nbsp/>nil
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>loop<nbsp/>do
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>price<nbsp/>=<nbsp/>Price.fetch(@symbol)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"Current<nbsp/>price:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/>!=<nbsp/>lastPrice
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>changed<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>notify<nbsp/>observers
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>lastPrice<nbsp/>=<nbsp/>price
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>notify_observers(Time.now,<nbsp/>price)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>initialize(ticker,<nbsp/>limit)
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>@limit<nbsp/>=<nbsp/>limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>ticker.add_observer(self)<nbsp/><nbsp/><nbsp/>#<nbsp/>all<nbsp/>warners<nbsp/>are<nbsp/>observers
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>WarnLow<nbsp/>&lt;<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>update(time,<nbsp/>price)<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>callback<nbsp/>for<nbsp/>observer
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/>&lt;<nbsp/>@limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"---<nbsp/>#{time.to_s}:<nbsp/>Price<nbsp/>below<nbsp/>#@limit:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
<nbsp/><nbsp/>class<nbsp/>WarnHigh<nbsp/>&lt;<nbsp/>Warner
<nbsp/><nbsp/><nbsp/><nbsp/>def<nbsp/>update(time,<nbsp/>price)<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>callback<nbsp/>for<nbsp/>observer
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>if<nbsp/>price<nbsp/>&gt;<nbsp/>@limit
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>print<nbsp/>"+++<nbsp/>#{time.to_s}:<nbsp/>Price<nbsp/>above<nbsp/>#@limit:<nbsp/>#{price}\n"
<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/><nbsp/><nbsp/>end
<nbsp/><nbsp/>end
<p/>
ticker<nbsp/>=<nbsp/>Ticker.new("MSFT")
WarnLow.new(ticker,<nbsp/>80)
WarnHigh.new(ticker,<nbsp/>120)
ticker.run
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Current<nbsp/>price:<nbsp/>83
Current<nbsp/>price:<nbsp/>75
---<nbsp/>Sun<nbsp/>Nov<nbsp/>25<nbsp/>23:46:29<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>below<nbsp/>80:<nbsp/>75
Current<nbsp/>price:<nbsp/>90
Current<nbsp/>price:<nbsp/>134
+++<nbsp/>Sun<nbsp/>Nov<nbsp/>25<nbsp/>23:46:29<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>above<nbsp/>120:<nbsp/>134
Current<nbsp/>price:<nbsp/>134
Current<nbsp/>price:<nbsp/>112
Current<nbsp/>price:<nbsp/>79
---<nbsp/>Sun<nbsp/>Nov<nbsp/>25<nbsp/>23:46:29<nbsp/>CST<nbsp/>2001:<nbsp/>Price<nbsp/>below<nbsp/>80:<nbsp/>79
</alltt>
</codefragment>
<p/>
</library>
<p/>
<library name="singleton">
<p/>
The Singleton design pattern ensures that only
one instance of a particular class may be
created.
<p/>
The <tt>singleton</tt> library makes this simple to implement. Mix
the <modulename>Singleton</modulename> module into each class that is to be a singleton,
and that class's <meth>new</meth> method will be made private. In its
place, users of the class call the method <meth>instance</meth>, which
returns a singleton instance of that class.
<p/>
In this example, the two instances of <tt>MyClass</tt> are the same object.
<p/>
<codefragment>
<fullcode><![CDATA[     require 'singleton'

     class MyClass
       include Singleton
     end

     a = MyClass.instance
     b = MyClass.instance
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>require<nbsp/>'singleton'</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td colspan="3"><tt>class<nbsp/>MyClass</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>include<nbsp/>Singleton</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
  <td><tt>a<nbsp/>=<nbsp/>MyClass.instance</tt></td>
  <td>&#187;</td>
  <td><tt>#&lt;MyClass:0x401b2d20&gt;</tt></td>
</tr>
<tr>
  <td><tt>b<nbsp/>=<nbsp/>MyClass.instance</tt></td>
  <td>&#187;</td>
  <td><tt>#&lt;MyClass:0x401b2d20&gt;</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</library>
<p/>
</chapter>
</ppdoc>
