<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman, Inc. 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/).
<p/>
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
<p/>
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.
</copyright>
<class name="Comparable" type="module" ref="xref-000001">
<p/>
  <relieson><tt>&lt;=&gt;</tt></relieson>  The <modulename>Comparable</modulename> mixin is used by classes whose objects may be
  ordered.  The class must define the <tt>&lt;=&gt;</tt>  
  operator, which
  compares the receiver against another object, returning -1, 0,
  or +1 depending on whether the receiver is less than, equal to, or
  greater than the other object.  <modulename>Comparable</modulename> uses <tt>&lt;=&gt;</tt> to implement the
  conventional comparison operators (<tt>&lt;</tt>, <tt>&lt;=</tt>, <tt>==</tt>,
  <tt>&gt;=</tt>, and <tt>&gt;</tt>) and the method <meth>between?</meth>.          
<p/>
<codefragment>
<fullcode><![CDATA[     class SizeMatters
       include Comparable
       attr :str
       def <=>(anOther)
         str.size <=> anOther.str.size
       end
       def initialize(str)
         @str = str
       end
       def inspect
         @str
       end
     end

     s1 = SizeMatters.new("Z")
     s2 = SizeMatters.new("YY")
     s3 = SizeMatters.new("XXX")
     s4 = SizeMatters.new("WWWW")
     s5 = SizeMatters.new("VVVVV")

     s1 < s2
     s4.between?(s1, s3)
     s4.between?(s3, s5)
     [ s3, s2, s5, s4, s1 ].sort
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>class<nbsp/>SizeMatters</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>include<nbsp/>Comparable</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>attr<nbsp/>:str</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>&lt;=&gt;(anOther)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>str.size<nbsp/>&lt;=&gt;<nbsp/>anOther.str.size</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>initialize(str)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>@str<nbsp/>=<nbsp/>str</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>inspect</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>@str</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
<td colspan="3"><tt>s1<nbsp/>=<nbsp/>SizeMatters.new("Z")</tt></td>
</tr>
<tr>
<td colspan="3"><tt>s2<nbsp/>=<nbsp/>SizeMatters.new("YY")</tt></td>
</tr>
<tr>
<td colspan="3"><tt>s3<nbsp/>=<nbsp/>SizeMatters.new("XXX")</tt></td>
</tr>
<tr>
<td colspan="3"><tt>s4<nbsp/>=<nbsp/>SizeMatters.new("WWWW")</tt></td>
</tr>
<tr>
<td colspan="3"><tt>s5<nbsp/>=<nbsp/>SizeMatters.new("VVVVV")</tt></td>
</tr>
<tr>
<td colspan="3"><tt></tt></td>
</tr>
<tr>
  <td><tt>s1<nbsp/>&lt;<nbsp/>s2</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>s4.between?(s1,<nbsp/>s3)</tt></td>
  <td>&#187;</td>
  <td><tt>false</tt></td>
</tr>
<tr>
  <td><tt>s4.between?(s3,<nbsp/>s5)</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>[<nbsp/>s3,<nbsp/>s2,<nbsp/>s5,<nbsp/>s4,<nbsp/>s1<nbsp/>].sort</tt></td>
  <td>&#187;</td>
  <td><tt>[Z,<nbsp/>YY,<nbsp/>XXX,<nbsp/>WWWW,<nbsp/>VVVVV]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
  <methods type="instance">
<p/>
        <method name="Comparisons" ref="Comparisons">
      <callseq>
                  <obj>anObject</obj> &lt;  <obj>otherObject</obj> <returns><const>true</const> or <const>false</const></returns><br/><obj>anObject</obj> &lt;= <obj>otherObject</obj> <returns><const>true</const> or <const>false</const></returns><br/><obj>anObject</obj> == <obj>otherObject</obj> <returns><const>true</const> or <const>false</const></returns><br/><obj>anObject</obj> &gt;= <obj>otherObject</obj> <returns><const>true</const> or <const>false</const></returns><br/><obj>anObject</obj> &gt;  <obj>otherObject</obj> <returns><const>true</const> or <const>false</const></returns><br/>
      </callseq>
      <desc>
<p/>
      Compares two objects based on the receiver's <meth>&lt;=&gt;</meth> method.
<p/>
      </desc>
    </method>
<p/>
        <method name="between?" ref="between_qm">
      <callseq>
        <obj>anObject</obj>.between?( <obj>min</obj>, <obj>max</obj> )
        <returns><const>true</const> or <const>false</const></returns>
      </callseq>
      <desc>
<p/>
      Returns <const>false</const> if <obj>anObject</obj><nbsp/><tt>&lt;=&gt;</tt><nbsp/><obj>min</obj> is less than
      zero or if <obj>anObject</obj><nbsp/><tt>&lt;=&gt;</tt><nbsp/><obj>max</obj> is greater than zero,
      <const>true</const> otherwise.
<p/>
<codefragment>
<fullcode><![CDATA[        3.between?(1, 5)
        6.between?(1, 5)
        'cat'.between?('ant', 'dog')
        'gnu'.between?('ant', 'dog')
]]></fullcode><rubycode>
<tr>
  <td><tt>3.between?(1,<nbsp/>5)</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>6.between?(1,<nbsp/>5)</tt></td>
  <td>&#187;</td>
  <td><tt>false</tt></td>
</tr>
<tr>
  <td><tt>'cat'.between?('ant',<nbsp/>'dog')</tt></td>
  <td>&#187;</td>
  <td><tt>true</tt></td>
</tr>
<tr>
  <td><tt>'gnu'.between?('ant',<nbsp/>'dog')</tt></td>
  <td>&#187;</td>
  <td><tt>false</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
      </desc>
    </method>
<p/>
  </methods>
<p/>
</class>
</ppdoc>
