1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
<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>
<class name="Comparable" type="module">
<p/>
<relieson><tt><=></tt></relieson> The <modulename>Comparable</modulename> mixin is used by classes whose objects may be
ordered. The class must define the <tt><=></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><=></tt> to implement the
conventional comparison operators (<tt><</tt>, <tt><=</tt>, <tt>==</tt>,
<tt>>=</tt>, and <tt>></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/><=>(anOther)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>str.size<nbsp/><=><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/><<nbsp/>s2</tt></td>
<td>»</td>
<td><tt>true</tt></td>
</tr>
<tr>
<td><tt>s4.between?(s1,<nbsp/>s3)</tt></td>
<td>»</td>
<td><tt>false</tt></td>
</tr>
<tr>
<td><tt>s4.between?(s3,<nbsp/>s5)</tt></td>
<td>»</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>»</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> < <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> == <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> > <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><=></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><=></tt><nbsp/><obj>min</obj> is less than
zero or if <obj>anObject</obj><nbsp/><tt><=></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>»</td>
<td><tt>true</tt></td>
</tr>
<tr>
<td><tt>6.between?(1,<nbsp/>5)</tt></td>
<td>»</td>
<td><tt>false</tt></td>
</tr>
<tr>
<td><tt>'cat'.between?('ant',<nbsp/>'dog')</tt></td>
<td>»</td>
<td><tt>true</tt></td>
</tr>
<tr>
<td><tt>'gnu'.between?('ant',<nbsp/>'dog')</tt></td>
<td>»</td>
<td><tt>false</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|