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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
<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="Range" super="Object" type="class"> A <classname>Range</classname> represents an interval---a set of values with a start
and an end. Ranges may be constructed using the
<em>s</em><tt>..</tt><em>e</em> and <em>s</em><tt>...</tt><em>e</em> literals, or
with <ccm><file>range</file><front>Range</front><back>new</back><mref>new</mref></ccm>. Ranges constructed using <tt>..</tt> run from the
start to the end inclusively. Those created using <tt>...</tt> exclude the
end value. When used as an iterator, ranges return each value in
the sequence.
<p/>
<codefragment>
<fullcode><![CDATA[ (-1..-5).to_a
(-5..-1).to_a
('a'..'e').to_a
('a'...'e').to_a
]]></fullcode><rubycode>
<tr>
<td><tt>(-1..-5).to_a</tt></td>
<td>»</td>
<td><tt>[]</tt></td>
</tr>
<tr>
<td><tt>(-5..-1).to_a</tt></td>
<td>»</td>
<td><tt>[-5,<nbsp/>-4,<nbsp/>-3,<nbsp/>-2,<nbsp/>-1]</tt></td>
</tr>
<tr>
<td><tt>('a'..'e').to_a</tt></td>
<td>»</td>
<td><tt>["a",<nbsp/>"b",<nbsp/>"c",<nbsp/>"d",<nbsp/>"e"]</tt></td>
</tr>
<tr>
<td><tt>('a'...'e').to_a</tt></td>
<td>»</td>
<td><tt>["a",<nbsp/>"b",<nbsp/>"c",<nbsp/>"d"]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
Ranges can be constructed using objects of any type, as long as the
objects can be compared using their <tt><=></tt> operator and they
support the <meth>succ</meth> method to return the next object in
sequence.
<p/>
<codefragment>
<fullcode><![CDATA[ class Xs # represent a string of 'x's
include Comparable
attr :length
def initialize(n)
@length = n
end
def succ
Xs.new(@length + 1)
end
def <=>(other)
raise TypeError unless other.kind_of? Xs
@length <=> other.length
end
def inspect
'x' * @length
end
end
r = Xs.new(3)..Xs.new(6)
r.to_a
r.member?(Xs.new(5))
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>class<nbsp/>Xs<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>represent<nbsp/>a<nbsp/>string<nbsp/>of<nbsp/>'x's</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/>:length</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>initialize(n)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>@length<nbsp/>=<nbsp/>n</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/>succ</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>Xs.new(@length<nbsp/>+<nbsp/>1)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>def<nbsp/><=>(other)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>raise<nbsp/>TypeError<nbsp/>unless<nbsp/>other.kind_of?<nbsp/>Xs</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>@length<nbsp/><=><nbsp/>other.length</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/>'x'<nbsp/>*<nbsp/>@length</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><tt>r<nbsp/>=<nbsp/>Xs.new(3)..Xs.new(6)</tt></td>
<td>»</td>
<td><tt>xxx..xxxxxx</tt></td>
</tr>
<tr>
<td><tt>r.to_a</tt></td>
<td>»</td>
<td><tt>[xxx,<nbsp/>xxxx,<nbsp/>xxxxx,<nbsp/>xxxxxx]</tt></td>
</tr>
<tr>
<td><tt>r.member?(Xs.new(5))</tt></td>
<td>»</td>
<td><tt>true</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<mixins>
<mixin name="Enumerable">
collect, detect, each_with_index, entries, find, find_all, grep,
include?, map, max, member?, min, reject, select, sort, to_a</mixin>
</mixins>
<p/>
<methods type="class">
<method name="new" ref="new">
<callseq>
Range.new( <obj>start</obj>,
<obj>end</obj>, <obj>exclusive</obj><tt>=false</tt> )
<returns><obj>aRange</obj></returns>
</callseq>
<desc>
<p/>
Constructs a range using the given <obj>start</obj> and <obj>end</obj>. If the
third parameter is omitted or is <const>false</const>, the range will
include the end object; otherwise, it will be excluded.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
<methods type="instance">
<p/>
<method name="===" ref="_eq_eq_eq">
<callseq>
<obj>rng</obj> === <obj>anObject</obj> <returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
<p/>
Returns <const>true</const> if <obj>anObject</obj> is an element of <obj>rng</obj>,
<const>false</const> otherwise. Conveniently, <tt>===</tt> is the comparison
operator used by <kw>case</kw> statements.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ case 79
when 1..50 then print "low\n"
when 51..75 then print "medium\n"
when 76..100 then print "high\n"
end
]]></fullcode>
case<nbsp/>79
when<nbsp/>1..50<nbsp/><nbsp/><nbsp/>then<nbsp/><nbsp/><nbsp/>print<nbsp/>"low\n"
when<nbsp/>51..75<nbsp/><nbsp/>then<nbsp/><nbsp/><nbsp/>print<nbsp/>"medium\n"
when<nbsp/>76..100<nbsp/>then<nbsp/><nbsp/><nbsp/>print<nbsp/>"high\n"
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
high
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="begin" ref="begin">
<callseq>
<obj>rng</obj>.begin <returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Returns the first object of <obj>rng</obj>.
<p/>
</desc>
</method>
<p/>
<method name="each" ref="each">
<callseq>
<obj>rng</obj>.each <block>{| i | <blockbody>block</blockbody> }</block>
<returns><obj>rng</obj></returns>
</callseq>
<desc>
<p/>
Iterates over the elements <obj>rng</obj>, passing each in turn to the
block.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ (10..15).each do |n|
print n, ' '
end
!- print "\n"
]]></fullcode>
(10..15).each<nbsp/>do<nbsp/>|n|
<nbsp/><nbsp/><nbsp/>print<nbsp/>n,<nbsp/>'<nbsp/>'
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
10<nbsp/>11<nbsp/>12<nbsp/>13<nbsp/>14<nbsp/>15
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="end" ref="end">
<callseq>
<obj>rng</obj>.end <returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Returns the object that defines the end of <obj>rng</obj>. See also
<cim><file>range</file><front>Range</front><back>length</back><mref>length</mref></cim>.
<p/>
<codefragment>
<fullcode><![CDATA[ (1..10).end
(1...10).end
]]></fullcode><rubycode>
<tr>
<td><tt>(1..10).end</tt></td>
<td>»</td>
<td><tt>10</tt></td>
</tr>
<tr>
<td><tt>(1...10).end</tt></td>
<td>»</td>
<td><tt>10</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="exclude_end?" ref="exclude_end_qm">
<callseq>
<obj>rng</obj>.exclude_end?
<returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
<p/>
Returns <const>true</const> if <obj>rng</obj> excludes its end value.
<p/>
</desc>
</method>
<p/>
<method name="first" ref="first">
<callseq>
<obj>rng</obj>.first <returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Returns the first object in <obj>rng</obj>.
<p/>
</desc>
</method>
<p/>
<method name="last" ref="last">
<callseq>
<obj>rng</obj>.last <returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <cim><file>range</file><front>Range</front><back>end</back><mref>end</mref></cim>.
<p/>
</desc>
</method>
<p/>
<method name="length" ref="length">
<callseq>
<obj>rng</obj>.length <returns><obj>anInteger</obj></returns>
</callseq>
<desc>
<p/>
Returns the number of objects in <obj>rng</obj>.
<p/>
<codefragment>
<fullcode><![CDATA[ (1..10).length
(1...10).length
]]></fullcode><rubycode>
<tr>
<td><tt>(1..10).length</tt></td>
<td>»</td>
<td><tt>10</tt></td>
</tr>
<tr>
<td><tt>(1...10).length</tt></td>
<td>»</td>
<td><tt>9</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="size" ref="size">
<callseq>
<obj>rng</obj>.size <returns><obj>anInteger</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <cim><file>range</file><front>Range</front><back>length</back><mref>length</mref></cim>.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|