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 355 356 357 358 359 360 361 362 363 364
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_c_nilclass.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_object.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
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/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Numeric</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#_pl_at"><i>+@</i></a> <a href="#_mi_mi_at"><i>--@</i></a> <a href="#abs"><i>abs</i></a> <a href="#coerce"><i>coerce</i></a> <a href="#divmod"><i>divmod</i></a> <a href="#eql_qm"><i>eql?</i></a> <a href="#integer_qm"><i>integer?</i></a> <a href="#modulo"><i>modulo</i></a> <a href="#nonzero_qm"><i>nonzero?</i></a> <a href="#remainder"><i>remainder</i></a> <a href="#zero_qm"><i>zero?</i></a> <p></p><hr>
<P></P>
<br><font size="-1">Subclasses: Float, Integer</font><p></p>
<P></P>
Numeric is the fundamental base type for the concrete number classes
<code>Float</code>, <code>Fixnum</code>, and <code>Bignum</code>.
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">
mixins
</font></td></tr>
<tr><td>Comparable:
</td><td>
<, <=, ==, >=, >, between?</td></tr>
</table>
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>Difference between modulo and
remainder. The modulo operator
(``%'') always has the sign of the divisor, whereas
<code>remainder</code> has the sign of the dividend.</b>
<center>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>a</b></td>
<td valign="top"><b>b</b></td>
<td colspan="2" valign="top"><b>a.divmod(b)</b></td>
<td colspan="1" valign="top"><b>a / b</b></td>
<td colspan="1" valign="top"><b>a.modulo(b)</b></td>
<td colspan="1" valign="top"><b>a.remainder(b)</b></td>
</tr>
<tr>
<td valign="top">13</td>
<td valign="top">4</td>
<td valign="top">3,</td>
<td valign="top">1</td>
<td valign="top">3</td>
<td valign="top">1</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top">13</td>
<td valign="top">-4</td>
<td valign="top">-4,</td>
<td valign="top">-3</td>
<td valign="top">-4</td>
<td valign="top">-3</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top">-13</td>
<td valign="top">4</td>
<td valign="top">-4,</td>
<td valign="top">3</td>
<td valign="top">-4</td>
<td valign="top">3</td>
<td valign="top">-1</td>
</tr>
<tr>
<td valign="top">-13</td>
<td valign="top">-4</td>
<td valign="top">3,</td>
<td valign="top">-1</td>
<td valign="top">3</td>
<td valign="top">-1</td>
<td valign="top">-1</td>
</tr>
<tr>
<td valign="top">11.5</td>
<td valign="top">4</td>
<td valign="top">2.0,</td>
<td valign="top">3.5</td>
<td valign="top">2.875</td>
<td valign="top">3.5</td>
<td valign="top">3.5</td>
</tr>
<tr>
<td valign="top">11.5</td>
<td valign="top">-4</td>
<td valign="top">-3.0,</td>
<td valign="top">-0.5</td>
<td valign="top">-2.875</td>
<td valign="top">-0.5</td>
<td valign="top">3.5</td>
</tr>
<tr>
<td valign="top">-11.5</td>
<td valign="top">4</td>
<td valign="top">-3.0,</td>
<td valign="top">0.5</td>
<td valign="top">-2.875</td>
<td valign="top">0.5</td>
<td valign="top">-3.5</td>
</tr>
<tr>
<td valign="top">-11.5</td>
<td valign="top">-4</td>
<td valign="top">2.0,</td>
<td valign="top">-3.5</td>
<td valign="top">2.875</td>
<td valign="top">-3.5</td>
<td valign="top">-3.5</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</center>
</td></tr></table><table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_pl_at"><b>+@</b></a></font></td><td bgcolor="#ffaaaa">
+<i>num</i> -> <i>num</i>
</td></tr><td></td><td>
<P></P>
Unary Plus---Returns the receiver's value.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_mi_mi_at"><b>--@</b></a></font></td><td bgcolor="#ffaaaa">
--<i>num</i> -> <i>aNumeric</i>
</td></tr><td></td><td>
<P></P>
Unary Minus---Returns the receiver's value, negated.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abs"><b>abs</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.abs -> <i>aNumeric</i>
</td></tr><td></td><td>
<P></P>
Returns the absolute value of <i>num</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>12.abs</code></td>
<td valign="top"></td>
<td valign="top"><code>12</code></td>
</tr>
<tr>
<td valign="top"><code>(-34.56).abs</code></td>
<td valign="top"></td>
<td valign="top"><code>34.56</code></td>
</tr>
<tr>
<td valign="top"><code>-34.56.abs</code></td>
<td valign="top"></td>
<td valign="top"><code>34.56</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="coerce"><b>coerce</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.coerce( <i>aNumeric</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
If <i>aNumeric</i> is the same type as <i>num</i>, returns an array
containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an
array with both <i>aNumeric</i> and <i>num</i> represented as
<code>Float</code> objects.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>1.coerce(2.5)</code></td>
<td valign="top"></td>
<td valign="top"><code>[2.5, 1.0]</code></td>
</tr>
<tr>
<td valign="top"><code>1.2.coerce(3)</code></td>
<td valign="top"></td>
<td valign="top"><code>[3.0, 1.2]</code></td>
</tr>
<tr>
<td valign="top"><code>1.coerce(2)</code></td>
<td valign="top"></td>
<td valign="top"><code>[2, 1]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="divmod"><b>divmod</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.divmod( <i>aNumeric</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array containing the quotient and modulus obtained by
dividing <i>num</i> by <i>aNumeric</i>.
If <code>q, r = x.divmod(y)</code>,
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr>
<td valign="top">q</td>
<td valign="top">=</td>
<td valign="top">floor(float(x) / float(y))</td>
</tr>
<tr>
<td valign="top">x</td>
<td valign="top">=</td>
<td valign="top">q * y + r</td>
</tr>
</table>
<P></P>
The quotient is rounded toward -infinity. See Table
22.6 on page 355.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>11.divmod(3)</code></td>
<td valign="top"></td>
<td valign="top"><code>[3, 2]</code></td>
</tr>
<tr>
<td valign="top"><code>11.divmod(-3)</code></td>
<td valign="top"></td>
<td valign="top"><code>[-4, -1]</code></td>
</tr>
<tr>
<td valign="top"><code>11.divmod(3.5)</code></td>
<td valign="top"></td>
<td valign="top"><code>[3.0, 0.5]</code></td>
</tr>
<tr>
<td valign="top"><code>(-11).divmod(3.5)</code></td>
<td valign="top"></td>
<td valign="top"><code>[-4.0, 3.0]</code></td>
</tr>
<tr>
<td valign="top"><code>(11.5).divmod(3.5)</code></td>
<td valign="top"></td>
<td valign="top"><code>[3.0, 1.0]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="eql_qm"><b>eql?</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.eql?( <i>aNumeric</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>num</i> and <i>aNumeric</i> are the same
type and have equal values.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>1 == 1.0</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>1.eql?(1.0)</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>(1.0).eql?(1.0)</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="integer_qm"><b>integer?</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.integer? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>num</i> is an <code>Integer</code> (including
<code>Fixnum</code> and <code>Bignum</code>).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="modulo"><b>modulo</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.modulo( <i>aNumeric</i> )
-> <i>aNumeric</i>
</td></tr><td></td><td>
<P></P>
Equivalent to <i>num</i>.<code>divmod(</code><i>aNumeric</i><code>)[1]</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="nonzero_qm"><b>nonzero?</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.nonzero?
-> <i>num</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Returns <i>num</i> if <i>num</i> is not zero, <code>nil</code> otherwise. This
behavior is useful when chaining comparisons:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = %w( z Bb bB bb BB a aA Aa AA A )</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }</code></td>
</tr>
<tr>
<td valign="top"><code>b</code></td>
<td valign="top"></td>
<td valign="top"><code>["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="remainder"><b>remainder</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.remainder( <i>aNumeric</i> )
-> <i>aNumeric</i>
</td></tr><td></td><td>
<P></P>
If <i>num</i> and <i>aNumeric</i> have different signs, returns
<em>mod</em>-<i>aNumeric</i>; otherwise, returns
<em>mod</em>. In both cases
<em>mod</em> is the value <i>num</i>.<code>modulo(</code><i>aNumeric</i><code>)</code>. The
differences between <code>remainder</code> and modulo (<code>%</code>) are
shown in Table 22.6 on page 355.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="zero_qm"><b>zero?</b></a></font></td><td bgcolor="#ffaaaa">
<i>num</i>.zero? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>num</i> has a zero value.
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_c_nilclass.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_object.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|