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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
|
<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_numeric.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_proc.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 Object</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white"></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="#_eq_eq"><i>==</i></a> <a href="#_eq_eq_eq"><i>===</i></a> <a href="#_eq_sd"><i>=~</i></a> <a href="#__id__"><i>__id__</i></a> <a href="#__send__"><i>__send__</i></a> <a href="#class"><i>class</i></a> <a href="#clone"><i>clone</i></a> <a href="#display"><i>display</i></a> <a href="#dup"><i>dup</i></a> <a href="#eql_qm"><i>eql?</i></a> <a href="#equal_qm"><i>equal?</i></a> <a href="#extend"><i>extend</i></a> <a href="#freeze"><i>freeze</i></a> <a href="#frozen_qm"><i>frozen?</i></a> <a href="#hash"><i>hash</i></a> <a href="#id"><i>id</i></a> <a href="#inspect"><i>inspect</i></a> <a href="#instance_eval"><i>instance_eval</i></a> <a href="#instance_of_qm"><i>instance_of?</i></a> <a href="#instance_variables"><i>instance_variables</i></a> <a href="#is_a_qm"><i>is_a?</i></a> <a href="#kind_of_qm"><i>kind_of?</i></a> <a href="#method"><i>method</i></a> <a href="#method_missing"><i>method_missing</i></a> <a href="#methods"><i>methods</i></a> <a href="#nil_qm"><i>nil?</i></a> <a href="#private_methods"><i>private_methods</i></a> <a href="#protected_methods"><i>protected_methods</i></a> <a href="#public_methods"><i>public_methods</i></a> <a href="#respond_to_qm"><i>respond_to?</i></a> <a href="#send"><i>send</i></a> <a href="#singleton_methods"><i>singleton_methods</i></a> <a href="#taint"><i>taint</i></a> <a href="#tainted_qm"><i>tainted?</i></a> <a href="#to_a"><i>to_a</i></a> <a href="#to_s"><i>to_s</i></a> <a href="#type"><i>type</i></a> <a href="#untaint"><i>untaint</i></a> <p></p><hr>
<br><font size="-1">Subclasses: Array, Binding, Continuation,
Data, Dir, Exception, FalseClass, File::Stat, Hash, IO,
MatchingData, Method, Module, NilClass, Numeric, Proc, Range,
Regexp, String, Struct, Symbol, Thread, Time, TrueClass</font><p></p>
<P></P>
<code>Object</code> is the parent class of all classes in Ruby. Its methods
are therefore available to all objects unless explicitly
overridden.
<P></P>
<code>Object</code> mixes in the <code>Kernel</code> module, making the built-in
kernel functions globally accessible. Although the instance methods
of <code>Object</code> are defined by the <code>Kernel</code> module, we have chosen
to document them here for clarity.
<P></P>
In the descriptions that follow, the
parameter <i>aSymbol</i> refers to a symbol, which is either a
quoted string or a <code>Symbol</code> (such as <code>:name</code>).
<P></P>
<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="_eq_eq"><b>==</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i> == <i>anObject</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Equality---At the <code>Object</code> level, <code>==</code> returns <code>true</code> only if
<i>obj</i> and <i>anObject</i> are the same object. Typically, this
method is overridden in descendent classes to provide
class-specific meaning.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_eq_eq_eq"><b>===</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i> === <i>anObject</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Case Equality---A synonym for <a href="ref_c_object.html#_eq_eq"><code>Object#==</code></a>, but typically overridden by
descendents to provide meaningful semantics in <code>case</code>
statements.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_eq_sd"><b>=~</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i> =~ <i>anObject</i>
-> <code>false</code>
</td></tr><td></td><td>
<P></P>
Pattern Match---Overridden by descendents (notably <code>Regexp</code>
and <code>String</code>) to provide meaningful
pattern-match semantics.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="__id__"><b>__id__</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.__id__
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_object.html#id"><code>Object#id</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="__send__"><b>__send__</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.__send__(
<i>aSymbol</i> <i>[</i>, <i>args</i><i>]<sup>+</sup></i> ) -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_object.html#send"><code>Object#send</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="class"><b>class</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.class -> <i>aClass</i>
</td></tr><td></td><td>
<P></P>
Returns the class of <i>obj</i> (synonym for <a href="ref_c_object.html#type"><code>Object#type</code></a>).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="clone"><b>clone</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.clone -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Produces a shallow copy of <i>obj</i>---the instance variables of <i>obj</i>
are copied, but not the objects they reference.
Copies the frozen and tainted state of <i>obj</i>. See also the
discussion under <a href="ref_c_object.html#dup"><code>Object#dup</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> attr_accessor :str</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>s1 = Klass.new</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Klass:0x4018d234></code></td>
</tr>
<tr>
<td valign="top"><code>s1.str = "Hello"</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello"</code></td>
</tr>
<tr>
<td valign="top"><code>s2 = s1.clone</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Klass:0x4018d194 @str="Hello"></code></td>
</tr>
<tr>
<td valign="top"><code>s2.str[1,4] = "i"</code></td>
<td valign="top"></td>
<td valign="top"><code>"i"</code></td>
</tr>
<tr>
<td valign="top"><code>s1.inspect</code></td>
<td valign="top"></td>
<td valign="top"><code>"#<Klass:0x4018d234 @str=\"Hi\">"</code></td>
</tr>
<tr>
<td valign="top"><code>s2.inspect</code></td>
<td valign="top"></td>
<td valign="top"><code>"#<Klass:0x4018d194 @str=\"Hi\">"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="display"><b>display</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.display( <i>port</i>=<code>$></code> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Prints <i>obj</i> on the given port (default <code>$></code>). Equivalent
to:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def display(port=$>)
port.write self
end
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="dup"><b>dup</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.dup -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Produces a shallow copy
of <i>obj</i>---the instance variables of <i>obj</i>
are copied, but not the objects they reference. <code>dup</code>
copies the tainted state of <i>obj</i>. See also the
discussion under <a href="ref_c_object.html#clone"><code>Object#clone</code></a>.
In general,
<code>clone</code> and <code>dup</code> may have different semantics in
descendent classes. While <code>clone</code> is used to duplicate an
object, including its internal state, <code>dup</code> typically
uses the class of the descendent object to create the new instance.
<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>obj</i>.eql?( <i>anObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>obj</i> and <i>anObject</i> have the
same value. Used by <code>Hash</code> to test
members for equality. For objects of class <code>Object</code>,
<code>eql?</code> is synonymous with <code>==</code>. Subclasses
normally continue this tradition, but there are
exceptions. <code>Numeric</code> types, for example, perform type
conversion across <code>==</code>, but not across <code>eql?</code>, so:
<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>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="equal_qm"><b>equal?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.equal?( <i>anObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>obj</i> and <i>anObject</i> have the
same object ID. This method should
not be overridden by subclasses.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = [ 'cat', 'dog' ]</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b = [ 'cat', 'dog' ]</code></td>
</tr>
<tr>
<td valign="top"><code>a == b</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>a.id == b.id</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>a.eql?(b)</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>a.equal?(b)</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="extend"><b>extend</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.extend( <i>[</i><i>aModule</i><i>]<sup>+</sup></i> )
-> <i>obj</i>
</td></tr><td></td><td>
<P></P>
Adds to <i>obj</i> the instance methods from each module given as a parameter.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>module Mod</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def hello</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "Hello from Mod.\n"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code></code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def hello</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "Hello from Klass.\n"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code></code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Klass.new</code></td>
</tr>
<tr>
<td valign="top"><code>k.hello</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello from Klass.\n"</code></td>
</tr>
<tr>
<td valign="top"><code>k.extend(Mod)</code></td>
<td valign="top"></td>
<td valign="top"><code>#<Klass:0x4018d414></code></td>
</tr>
<tr>
<td valign="top"><code>k.hello</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello from Mod.\n"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="freeze"><b>freeze</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.freeze -> <i>obj</i>
</td></tr><td></td><td>
<P></P>
Prevents further modifications to <i>obj</i>. A <code>TypeError</code>
will be raised if modification is attempted. There is no way to
unfreeze a frozen object.
See also <a href="ref_c_object.html#frozen_qm"><code>Object#frozen?</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = [ "a", "b", "c" ]
a.freeze
a << "z"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
prog.rb:3:in `<<': can't modify frozen array (TypeError)
from prog.rb:3
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="frozen_qm"><b>frozen?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.frozen?
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the freeze status of <i>obj</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = [ "a", "b", "c" ]</code></td>
</tr>
<tr>
<td valign="top"><code>a.freeze</code></td>
<td valign="top"></td>
<td valign="top"><code>["a", "b", "c"]</code></td>
</tr>
<tr>
<td valign="top"><code>a.frozen?</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="hash"><b>hash</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.hash -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Generates a <code>Fixnum</code> hash value for this object. This function
must have the property that <code>a.eql?(b)</code> implies <code>a.hash ==
b.hash</code>. The hash value is used by class <code>Hash</code>.
Any hash value that exceeds the
capacity of a <code>Fixnum</code> will be truncated before being used.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="id"><b>id</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.id -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns an integer identifier for <i>obj</i>. The same number will be
returned on all calls to <code>id</code> for a given object, and no
two active objects will share an id. <a href="ref_c_object.html#id"><code>Object#id</code></a> is a
different concept from the <code>:name</code> notation, which returns the
symbol id of <code>name</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="inspect"><b>inspect</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.inspect -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns a string containing a human-readable representation of
<i>obj</i>. If not overridden, uses the <code>to_s</code> method to generate
the string.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>[ 1, 2, 3..4, 'five' ].inspect</code></td>
<td valign="top"></td>
<td valign="top"><code>"[1, 2, 3..4, \"five\"]"</code></td>
</tr>
<tr>
<td valign="top"><code>Time.new.inspect</code></td>
<td valign="top"></td>
<td valign="top"><code>"Sun Mar 04 23:29:19 CST 2001"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="instance_eval"><b>instance_eval</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.instance_eval(<i>aString</i>
<i>[</i>, <i>file</i>
<i>[</i><i>line</i><i>]</i><i>]</i> )
-> <i>anObject</i><br><i>obj</i>.instance_eval {| | block }
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Evaluates a string containing Ruby source code, or the given
block, within the
context of the receiver (<i>obj</i>). In order to set the context, the
variable <code>self</code> is set to <i>obj</i> while the code is
executing, giving the code access to <i>obj</i>'s instance variables.
In the version of <code>instance_eval</code> that takes a
<code>String</code>, the optional second and third parameters supply a
filename and starting line number that are used when reporting
compilation errors.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def initialize</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> @secret = 99</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Klass.new</code></td>
</tr>
<tr>
<td valign="top"><code>k.instance_eval { @secret }</code></td>
<td valign="top"></td>
<td valign="top"><code>99</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="instance_of_qm"><b>instance_of?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.instance_of?( <i>aClass</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>obj</i> is an instance of the
given class. See also <a href="ref_c_object.html#kind_of_qm"><code>Object#kind_of?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="instance_variables"><b>instance_variables</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.instance_variables
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of instance variable names for the
receiver.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="is_a_qm"><b>is_a?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.is_a?( <i>aClass</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_object.html#kind_of_qm"><code>Object#kind_of?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="kind_of_qm"><b>kind_of?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.kind_of?( <i>aClass</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
Returns <code>true</code> if <i>aClass</i> is the class of <i>obj</i>, or
if <i>aClass</i> is one of the superclasses of <i>obj</i> or modules included
in <i>obj</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Integer.new</code></td>
</tr>
<tr>
<td valign="top"><code>a.instance_of? Numeric</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>a.instance_of? Integer</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>a.instance_of? Fixnum</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>a.instance_of? Comparable</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>a.kind_of? Numeric</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>a.kind_of? Integer</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>a.kind_of? Fixnum</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>a.kind_of? Comparable</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="method"><b>method</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.method( <i>aSymbol</i> )
-> <i>aMethod</i>
</td></tr><td></td><td>
<P></P>
Looks up the named method as a receiver in <i>obj</i>, returning a
<code>Method</code> object (or raising <code>NameError</code>). The <code>Method</code> object acts
as a closure in <i>obj</i>'s object instance, so instance variables
and the value of <code>self</code> remain available.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Demo</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def initialize(n)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> @iv = n</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def hello()</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "Hello, @iv = #{@iv}"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code></code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Demo.new(99)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>m = k.method(:hello)</code></td>
</tr>
<tr>
<td valign="top"><code>m.call</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello, @iv = 99"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code></code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>l = Demo.new('Fred')</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>m = l.method("hello")</code></td>
</tr>
<tr>
<td valign="top"><code>m.call</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello, @iv = Fred"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="method_missing"><b>method_missing</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.method_missing( <i>aSymbol</i> <i>[</i>, <i>*args</i><i>]</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
Invoked by Ruby when <i>obj</i> is sent a message it cannot
handle. <i>aSymbol</i> is the symbol for the method called,
and <i>args</i> are any arguments that were passed to it. The
example below creates a class <code>Roman</code>, which responds to methods
with names consisting of roman numerals, returning the corresponding
integer values.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class Roman
def romanToInt(str)
# ...
end
def method_missing(methId)
str = methId.id2name
romanToInt(str)
end
end
</pre></td></tr></table>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>r = Roman.new</code></td>
</tr>
<tr>
<td valign="top"><code>r.iv</code></td>
<td valign="top"></td>
<td valign="top"><code>4</code></td>
</tr>
<tr>
<td valign="top"><code>r.xxiii</code></td>
<td valign="top"></td>
<td valign="top"><code>23</code></td>
</tr>
<tr>
<td valign="top"><code>r.mm</code></td>
<td valign="top"></td>
<td valign="top"><code>2000</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="methods"><b>methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.methods -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of the names of methods publicly accessible in
<i>obj</i>. This will include all the methods accessible in <i>obj</i>'s
ancestors.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def kMethod()</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Klass.new</code></td>
</tr>
<tr>
<td valign="top"><code>k.methods[0..9]</code></td>
<td valign="top"></td>
<td valign="top"><code>["kMethod", "instance_of?", "protected_methods", "inspect", "freeze", "dup", "__id__", "equal?", "send", "=="]</code></td>
</tr>
<tr>
<td valign="top"><code>k.methods.length</code></td>
<td valign="top"></td>
<td valign="top"><code>38</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="nil_qm"><b>nil?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.nil? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
All objects except <code>nil</code> return <code>false</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="private_methods"><b>private_methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.private_methods
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of private methods accessible within <i>obj</i>. This
will include the private methods in <i>obj</i>'s ancestors, along
with any mixed-in module functions.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="protected_methods"><b>protected_methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.protected_methods
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the list of protected methods accessible to <i>obj</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="public_methods"><b>public_methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.public_methods -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_object.html#methods"><code>Object#methods</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="respond_to_qm"><b>respond_to?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.respond_to?( <i>aSymbol</i>,
<i>includePriv</i>=<code>false</code> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>obj</i> responds to the given
method. Private methods are included in the search only if the
optional second parameter evaluates to <code>true</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="send"><b>send</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.send( <i>aSymbol</i>
<i>[</i>, <i>args</i><i>]<sup>*</sup></i> ) -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Invokes the method identified by <i>aSymbol</i>, passing it any
arguments specified. You can use <code>__send__</code> if the
name <code>send</code> clashes with an existing method in <i>obj</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def hello(*args)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "Hello " + args.join(' ')</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Klass.new</code></td>
</tr>
<tr>
<td valign="top"><code>k.send :hello, "gentle", "readers"</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello gentle readers"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="singleton_methods"><b>singleton_methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.singleton_methods
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of the names of singleton methods for <i>obj</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Klass</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def Klass.classMethod</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>k = Klass.new</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>def k.sm()</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>Klass.singleton_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["classMethod"]</code></td>
</tr>
<tr>
<td valign="top"><code>k.singleton_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["sm"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="taint"><b>taint</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.taint -> <i>obj</i>
</td></tr><td></td><td>
<P></P>
Marks <i>obj</i> as tainted
(see Chapter 20, which begins on page 257).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="tainted_qm"><b>tainted?</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.tainted? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the object is tainted.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_a"><b>to_a</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.to_a -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array representation of <i>obj</i>. For
objects of class <code>Object</code> and others that don't explicitly
override the method, the return value is an array containing <code>self</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>self.to_a</code></td>
<td valign="top"></td>
<td valign="top"><code>[main]</code></td>
</tr>
<tr>
<td valign="top"><code>"hello".to_a</code></td>
<td valign="top"></td>
<td valign="top"><code>["hello"]</code></td>
</tr>
<tr>
<td valign="top"><code>Time.new.to_a</code></td>
<td valign="top"></td>
<td valign="top"><code>[19, 29, 23, 4, 3, 2001, 0, 63, false, "CST"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_s"><b>to_s</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.to_s -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns a string representing <i>obj</i>. The default <code>to_s</code>
prints the object's class and an encoding of the object id. As a
special case, the top-level object that is the initial execution
context of Ruby programs returns ``main.''
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="type"><b>type</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.type -> <i>aClass</i>
</td></tr><td></td><td>
<P></P>
Returns the class of <i>obj</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="untaint"><b>untaint</b></a></font></td><td bgcolor="#ffaaaa">
<i>obj</i>.untaint -> <i>obj</i>
</td></tr><td></td><td>
<P></P>
Removes the taint from <i>obj</i>.
<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_numeric.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_proc.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>
|