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 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
|
<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_method.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_nilclass.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 Module</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="#constants">constants</a> <a href="#nesting">nesting</a> <a href="#new">new</a> <a href="#_lt_cm_lt_eq_cm_lt_cm_lt_eq"><i><, <=, >, >=</i></a> <a href="#_lt_eq_lt"><i><=></i></a> <a href="#_eq_eq_eq"><i>===</i></a> <a href="#ancestors"><i>ancestors</i></a> <a href="#class_eval"><i>class_eval</i></a> <a href="#class_variables"><i>class_variables</i></a> <a href="#clone"><i>clone</i></a> <a href="#const_defined_qm"><i>const_defined?</i></a> <a href="#const_get"><i>const_get</i></a> <a href="#const_set"><i>const_set</i></a> <a href="#constants"><i>constants</i></a> <a href="#included_modules"><i>included_modules</i></a> <a href="#instance_methods"><i>instance_methods</i></a> <a href="#method_defined_qm"><i>method_defined?</i></a> <a href="#module_eval"><i>module_eval</i></a> <a href="#name"><i>name</i></a> <a href="#private_class_method"><i>private_class_method</i></a> <a href="#private_instance_methods"><i>private_instance_methods</i></a> <a href="#protected_instance_methods"><i>protected_instance_methods</i></a> <a href="#public_class_method"><i>public_class_method</i></a> <a href="#public_instance_methods"><i>public_instance_methods</i></a> <a href="#alias_method"><i>alias_method</i></a> <a href="#append_features"><i>append_features</i></a> <a href="#attr"><i>attr</i></a> <a href="#attr_accessor"><i>attr_accessor</i></a> <a href="#attr_reader"><i>attr_reader</i></a> <a href="#attr_writer"><i>attr_writer</i></a> <a href="#extend_object"><i>extend_object</i></a> <a href="#include"><i>include</i></a> <a href="#method_added"><i>method_added</i></a> <a href="#module_function"><i>module_function</i></a> <a href="#private"><i>private</i></a> <a href="#protected"><i>protected</i></a> <a href="#public"><i>public</i></a> <a href="#remove_const"><i>remove_const</i></a> <a href="#remove_method"><i>remove_method</i></a> <a href="#undef_method"><i>undef_method</i></a> <p></p><hr> <br><font size="-1">Subclasses: Class</font><p></p>
<P></P>
A <code>Module</code> is a collection of methods and constants. The methods
in a module may be instance methods or module methods. Instance
methods appear as methods in a class when the module is included,
module methods do not. Conversely, module methods may be called
without creating an encapsulating object, while instance methods may
not. See <a href="ref_c_module.html#module_function"><code>Module#module_function</code></a>
on page 350.
<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 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> include Math</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> CONST = 1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def meth</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> # ...</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 valign="top"><code>Mod.type</code></td>
<td valign="top"></td>
<td valign="top"><code>Module</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.constants</code></td>
<td valign="top"></td>
<td valign="top"><code>["CONST", "E", "PI"]</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["meth"]</code></td>
</tr>
</table>
<P></P>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="constants"><b>constants</b></a></font></td><td bgcolor="#ffaaaa">
Module.constants -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of the names of all constants defined in the
system. This list includes the names of all modules and classes.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
p Module.constants.sort[1..5]
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="nesting"><b>nesting</b></a></font></td><td bgcolor="#ffaaaa">
Module.nesting -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the list of <code>Modules</code> nested at the point of call.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>module M1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> module M2</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> $a = Module.nesting</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 valign="top"><code>$a</code></td>
<td valign="top"></td>
<td valign="top"><code>[M1::M2, M1]</code></td>
</tr>
<tr>
<td valign="top"><code>$a[0].name</code></td>
<td valign="top"></td>
<td valign="top"><code>"M1::M2"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Module.new -> <i>aModule</i>
</td></tr><td></td><td>
<P></P>
Creates a new anonymous module.
<P></P>
</td></table>
<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="_lt_cm_lt_eq_cm_lt_cm_lt_eq"><b><, <=, >, >=</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i> <code><em>relop</em></code>
<i>aModule</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Hierarchy Query---One module is considered <em>greater than</em>
another if it is included in (or is a
parent class of) the other module.
The other operators are
defined accordingly. If there is no relationship
between the modules, returns false for all operators.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>module Mixin</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>module Parent</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> include Mixin</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>module Unrelated</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 valign="top"><code>Parent > Mixin</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>Parent < Mixin</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>Parent <= Parent</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>Parent < Unrelated</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>Parent > Unrelated</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="_lt_eq_lt"><b><=></b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i> <=> <i>aModule</i>
-> -1, 0, +1
</td></tr><td></td><td>
<P></P>
Comparison---Returns -1 if <i>mod</i> includes <i>aModule</i>, 0 if <i>mod</i> is
the same as <i>aModule</i>, and +1 if <i>mod</i> is included by
<i>aModule</i> or if <i>mod</i> has no relationship with
<i>aModule</i>.
<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>mod</i> === <i>anObject</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Case Equality---Returns <code>true</code> if <i>anObject</i> is an instance of <i>mod</i>
or one of <i>mod</i>'s descendents. Of limited use for modules, but
can be used in <code>case</code> statements to classify objects by class.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="ancestors"><b>ancestors</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.ancestors -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of modules included in <i>mod</i> (including <i>mod</i>
itself).
<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> include Math</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> include Comparable</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 valign="top"><code>Mod.ancestors</code></td>
<td valign="top"></td>
<td valign="top"><code>[Mod, Comparable, Math]</code></td>
</tr>
<tr>
<td valign="top"><code>Math.ancestors</code></td>
<td valign="top"></td>
<td valign="top"><code>[Math]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="class_eval"><b>class_eval</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.class_eval( <i>aString</i> ) -> <i>anObject</i><br><i>mod</i>.class_eval {| | block }
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>Module.module_eval</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="class_variables"><b>class_variables</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.class_variables
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of the names of class variables in
<i>mod</i> and the ancestors of <i>mod</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class One</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> @@var1 = 1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>class Two < One</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> @@var2 = 2</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>One.class_variables</code></td>
<td valign="top"></td>
<td valign="top"><code>["@@var1"]</code></td>
</tr>
<tr>
<td valign="top"><code>Two.class_variables</code></td>
<td valign="top"></td>
<td valign="top"><code>["@@var2", "@@var1"]</code></td>
</tr>
</table>
<P></P>
<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>mod</i>.clone -> <i>aModule</i>
</td></tr><td></td><td>
<P></P>
Creates a new copy of a module.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>m = Math.clone</code></td>
<td valign="top"></td>
<td valign="top"><code>Math</code></td>
</tr>
<tr>
<td valign="top"><code>m.constants</code></td>
<td valign="top"></td>
<td valign="top"><code>["PI", "E"]</code></td>
</tr>
<tr>
<td valign="top"><code>m == Math</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="const_defined_qm"><b>const_defined?</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.const_defined?( <i>aSymbol</i> ) -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if a constant with
the given name is defined by <i>mod</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Math.const_defined? "PI"</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="const_get"><b>const_get</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.const_get( <i>aSymbol</i> ) -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Returns the value of the named constant in <i>mod</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Math.const_get :PI</code></td>
<td valign="top"></td>
<td valign="top"><code>3.141592654</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="const_set"><b>const_set</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.const_set( <i>aSymbol</i>, <i>anObject</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Sets the named constant to the given object, returning that
object. Creates a new constant if no constant with the given
name previously existed.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)</code></td>
<td valign="top"></td>
<td valign="top"><code>3.142857143</code></td>
</tr>
<tr>
<td valign="top"><code>Math::HIGH_SCHOOL_PI - Math::PI</code></td>
<td valign="top"></td>
<td valign="top"><code>0.001264489267</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="constants"><b>constants</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.constants -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of the names of the constants accessible
in <i>mod</i>. This includes the names of constants in any
included modules (example at start of section).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="included_modules"><b>included_modules</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.included_modules
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the list of modules included in <i>mod</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>module Mixin</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>module Outer</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> include Mixin</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 valign="top"><code>Mixin.included_modules</code></td>
<td valign="top"></td>
<td valign="top"><code>[]</code></td>
</tr>
<tr>
<td valign="top"><code>Outer.included_modules</code></td>
<td valign="top"></td>
<td valign="top"><code>[Mixin]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="instance_methods"><b>instance_methods</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.instance_methods(
<i>includeSuper</i>=<code>false</code> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array containing the names of public instance methods
in the receiver. For a module, these are the public methods; for
a class, they are the instance (not singleton) methods. With no
argument, or with an argument that is
<code>false</code>, the instance methods in <i>mod</i> are returned,
otherwise the methods in <i>mod</i> and <i>mod</i>'s superclasses
are returned.
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module A
def method1() end
end
class B
def method2() end
end
class C < B
def method3() end
end
</pre></td></tr></table>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>A.instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["method1"]</code></td>
</tr>
<tr>
<td valign="top"><code>B.instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["method2"]</code></td>
</tr>
<tr>
<td valign="top"><code>C.instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["method3"]</code></td>
</tr>
<tr>
<td valign="top"><code>C.instance_methods(true).length</code></td>
<td valign="top"></td>
<td valign="top"><code>39</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="method_defined_qm"><b>method_defined?</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.method_defined?( <i>aSymbol</i> ) -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the named method is defined by <i>mod</i>
(or its included modules and, if <i>mod</i> is a class, its
ancestors).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>module A</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def method1() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>class B</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def method2() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>class C < B</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> include A</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def method3() 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 valign="top"><code>A.method_defined? :method1</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>C.method_defined? "method1"</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>C.method_defined? "method2"</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>C.method_defined? "method3"</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>C.method_defined? "method4"</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="module_eval"><b>module_eval</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.module_eval( <i>aString</i> ) -> <i>anObject</i><br><i>mod</i>.module_eval {| | block }
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Evaluates the string or block in the context of <i>mod</i>. This can
be used to add methods to a class. <code>module_eval</code> returns
the result of evaluating its argument.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>class Thing</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>a = %q{def hello() "Hello there!" end}</code></td>
</tr>
<tr>
<td valign="top"><code>Thing.module_eval(a)</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>Thing.new.hello()</code></td>
<td valign="top"></td>
<td valign="top"><code>"Hello there!"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="name"><b>name</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.name -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the name of the module <i>mod</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="private_class_method"><b>private_class_method</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.private_class_method( <i>[</i><i>aSymbol</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Makes existing class methods private.
Often used to hide the
default constructor <code>new</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class SimpleSingleton # Not thread safe
private_class_method :new
def SimpleSingleton.create(*args, &block)
@me = new(*args, &block) if ! @me
@me
end
end
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="private_instance_methods"><b>private_instance_methods</b></a></font></td><td bgcolor="#ffaaaa">
<br><i>mod</i>.private_instance_methods(
<i>includeSuper</i>=<code>false</code> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of the private instance methods defined in <i>mod</i>.
If the optional parameter is not <code>false</code>, the methods
of any ancestors are included.
<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 method1() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> private :method1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def method2() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["method2"]</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.private_instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["method1"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="protected_instance_methods"><b>protected_instance_methods</b></a></font></td><td bgcolor="#ffaaaa">
<br><i>mod</i>.protected_instance_methods(
<i>includeSuper</i>=<code>false</code> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of the protected instance methods defined in <i>mod</i>.
If the optional parameter is not <code>false</code>, the methods
of any ancestors are included.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="public_class_method"><b>public_class_method</b></a></font></td><td bgcolor="#ffaaaa">
<i>mod</i>.public_class_method( <i>[</i><i>aSymbol</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Makes a list of existing class methods public.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="public_instance_methods"><b>public_instance_methods</b></a></font></td><td bgcolor="#ffaaaa">
<br><i>mod</i>.public_instance_methods(
<i>includeSuper</i>=<code>false</code> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a list of the public instance methods defined in <i>mod</i>.
If the optional parameter is not <code>false</code>, the methods
of any ancestors are included.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">private methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="alias_method"><b>alias_method</b></a></font></td><td bgcolor="#ffaaaa">
alias_method( <i>newID, oldID</i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
Makes <i>newID</i> a new copy of the method
<i>oldID</i>. This can be used to retain access to methods that
are overridden.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module Mod
alias_method :origExit, :exit
def exit(code=0)
print "Exiting with code #{code}\n"
origExit(code)
end
end
include Mod
exit(99)
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Exiting with code 99
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="append_features"><b>append_features</b></a></font></td><td bgcolor="#ffaaaa">
append_features( <i>aModule</i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
When this module is included in
another, Ruby calls <code>append_features</code> in this module,
passing it the receiving module in <i>aModule</i>. Ruby's default
implementation is to add the constants, methods, and module
variables of this module to <i>aModule</i> if this
module has not already been added to <i>aModule</i> or one of its
ancestors. See also
<a href="ref_c_module.html#include"><code>Module#include</code></a> on page 350.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="attr"><b>attr</b></a></font></td><td bgcolor="#ffaaaa">
attr( <i>aSymbol</i>, <i>writable</i>=<code>false</code> )
-> <code>nil</code>
</td></tr><td></td><td>
Defines a named attribute for this module, where the name is
<i>aSymbol.</i><code>id2name</code>, creating an instance variable
(<code>@name</code>) and a corresponding access method to read it.
If the optional <i>writable</i>
argument is <code>true</code>, also creates a method called <code>name=</code> to
set the attribute.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module Mod
attr :size, true
end
</pre></td></tr></table>
<em>is equivalent to:</em>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module Mod
def size
@size
end
def size=(val)
@size = val
end
end
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="attr_accessor"><b>attr_accessor</b></a></font></td><td bgcolor="#ffaaaa">
attr_accessor( <i>[</i><i>aSymbol</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to calling ``<code>attr </code><i>aSymbol</i><code>, true</code>'' on each
<i>aSymbol</i> in turn.
<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> attr_accessor(:one, :two)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.instance_methods.sort</code></td>
<td valign="top"></td>
<td valign="top"><code>["one", "one=", "two", "two="]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="attr_reader"><b>attr_reader</b></a></font></td><td bgcolor="#ffaaaa">
attr_reader( <i>[</i><i>aSymbol</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Creates instance variables and corresponding methods that
return the value of each instance variable.
Equivalent to calling ``<code>attr </code><i>:name</i>'' on each name
in turn.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="attr_writer"><b>attr_writer</b></a></font></td><td bgcolor="#ffaaaa">
<P></P>
attr_writer( <i>[</i><i>aSymbol</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Creates an accessor method to allow assignment to the attribute
<i>aSymbol</i><code>.id2name</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="extend_object"><b>extend_object</b></a></font></td><td bgcolor="#ffaaaa">
extend_object( <i>anObject</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Extends the specified object by adding this module's constants
and methods (which are added as singleton methods). This is the
callback method used by <a href="ref_c_object.html#extend"><code>Object#extend</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module Picky
def Picky.extend_object(o)
if String === o
print "Can't add Picky to a String\n"
else
print "Picky added to ", o.type, "\n"
super
end
end
end
(s = Array.new).extend Picky # Call Object.extend
(s = "quick brown fox").extend Picky
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Picky added to Array
Can't add Picky to a String
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="include"><b>include</b></a></font></td><td bgcolor="#ffaaaa">
include( <i>[</i><i>aModule</i><i>]<sup>+</sup></i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
Invokes <code>Module.append_features</code>
(documented on page 349)
on each parameter in turn.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="method_added"><b>method_added</b></a></font></td><td bgcolor="#ffaaaa">
method_added( <i>aSymbol</i> )
</td></tr><td></td><td>
<P></P>
Invoked as a callback whenever a method is added to the receiver.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
module Chatty
def Chatty.method_added(id)
print "Adding ", id.id2name, "\n"
end
def one() end
end
module Chatty
def two() end
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Adding one
Adding two
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="module_function"><b>module_function</b></a></font></td><td bgcolor="#ffaaaa">
module_function( <i>[</i><i>aSymbol</i><i>]<sup>*</sup></i> ) -> <i>mod</i>
</td></tr><td></td><td>
<P></P>
Creates module functions for the named methods. These
functions may be called with the module as a receiver, and also
become available as instance methods to classes that mix in the
module. Module functions are copies of the original, and so may
be changed independently. The instance-method versions are made
private. If used with no arguments, subsequently defined methods
become module functions.
<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 one</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "This is one"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> module_function :one</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>class Cls</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> include Mod</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def callOne</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> one</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 valign="top"><code>Mod.one</code></td>
<td valign="top"></td>
<td valign="top"><code>"This is one"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>c = Cls.new</code></td>
</tr>
<tr>
<td valign="top"><code>c.callOne</code></td>
<td valign="top"></td>
<td valign="top"><code>"This is one"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>module Mod</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def one</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "This is the new one"</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 valign="top"><code>Mod.one</code></td>
<td valign="top"></td>
<td valign="top"><code>"This is one"</code></td>
</tr>
<tr>
<td valign="top"><code>c.callOne</code></td>
<td valign="top"></td>
<td valign="top"><code>"This is the new one"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="private"><b>private</b></a></font></td><td bgcolor="#ffaaaa">
private( <i>[</i><i>aSymbol</i><i>]<sup>*</sup></i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
With no arguments, sets the default visibility for subsequently
defined methods to private.
With arguments, sets the named
methods to have private visibility. See <em>Access Control</em>
starting on page 235.
<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 a() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def b() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> private</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> def c() end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> private :a</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>Mod.private_instance_methods</code></td>
<td valign="top"></td>
<td valign="top"><code>["c", "a"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="protected"><b>protected</b></a></font></td><td bgcolor="#ffaaaa">
protected( <i>[</i><i>aSymbol</i><i>]<sup>*</sup></i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
With no arguments, sets the default visibility for subsequently
defined methods to protected.
With arguments, sets the named
methods to have protected visibility. See <em>Access Control</em> starting
on page 235.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="public"><b>public</b></a></font></td><td bgcolor="#ffaaaa">
public( <i>[</i><i>aSymbol</i><i>]<sup>*</sup></i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
With no arguments, sets the default visibility for subsequently
defined methods to public.
With arguments, sets the named
methods to have public visibility. See <em>Access Control</em> starting
on page 235.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="remove_const"><b>remove_const</b></a></font></td><td bgcolor="#ffaaaa">
remove_const( <i>aSymbol</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Removes the definition of the given constant, returning that
constant's value. Predefined classes and singleton objects (such
as <i>true</i>) cannot be removed.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="remove_method"><b>remove_method</b></a></font></td><td bgcolor="#ffaaaa">
remove_method( <i>aSymbol</i> )
-> <i>mod</i>
</td></tr><td></td><td>
Removes the method identified by
<i>aSymbol</i>
from the current class.
For an example, see <code>Module.undef_method</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="undef_method"><b>undef_method</b></a></font></td><td bgcolor="#ffaaaa">
undef_method( <i>aSymbol</i> )
-> <i>mod</i>
</td></tr><td></td><td>
<P></P>
Prevents the current class from responding to calls to the named
method. Contrast this with
<code>remove_method</code>, which
deletes the method from the particular class; Ruby will still
search superclasses and mixed-in modules for a possible
receiver.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class Parent
def hello
print "In parent\n"
end
end
class Child < Parent
def hello
print "In child\n"
end
end
<P></P>
c = Child.new
c.hello
<P></P>
class Child
remove_method :hello # remove from child, still in parent
end
c.hello
<P></P>
class Child
undef_method :hello # prevent any calls to 'hello'
end
c.hello
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
In child
In parent
prog.rb:23: undefined method `hello' for #<Child:0x4018d428> (NameError)
</pre></td></tr></table>
<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_method.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_nilclass.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>
|