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 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
|
<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_m_gc.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_m_marshal.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">module Kernel</font></td></tr></table><p></p><H3>Index:</H3><a href="#Array">Array</a> <a href="#Float">Float</a> <a href="#Integer">Integer</a> <a href="#String">String</a> <a href="#_bq">` (backquote)</a> <a href="#abort">abort</a> <a href="#at_exit">at_exit</a> <a href="#autoload">autoload</a> <a href="#binding">binding</a> <a href="#block_given_qm">block_given?</a> <a href="#callcc">callcc</a> <a href="#caller">caller</a> <a href="#catch">catch</a> <a href="#chomp">chomp</a> <a href="#chomp_oh">chomp!</a> <a href="#chop">chop</a> <a href="#chop_oh">chop!</a> <a href="#eval">eval</a> <a href="#exec">exec</a> <a href="#exit">exit</a> <a href="#exit_oh">exit!</a> <a href="#fail">fail</a> <a href="#fork">fork</a> <a href="#format">format</a> <a href="#gets">gets</a> <a href="#global_variables">global_variables</a> <a href="#gsub">gsub</a> <a href="#gsub_oh">gsub!</a> <a href="#iterator_qm">iterator?</a> <a href="#lambda">lambda</a> <a href="#load">load</a> <a href="#local_variables">local_variables</a> <a href="#loop">loop</a> <a href="#open">open</a> <a href="#p">p</a> <a href="#print">print</a> <a href="#printf">printf</a> <a href="#proc">proc</a> <a href="#putc">putc</a> <a href="#puts">puts</a> <a href="#raise">raise</a> <a href="#rand">rand</a> <a href="#readline">readline</a> <a href="#readlines">readlines</a> <a href="#require">require</a> <a href="#scan">scan</a> <a href="#select">select</a> <a href="#set_trace_func">set_trace_func</a> <a href="#singleton_method_added">singleton_method_added</a> <a href="#sleep">sleep</a> <a href="#split">split</a> <a href="#sprintf">sprintf</a> <a href="#srand">srand</a> <a href="#sub">sub</a> <a href="#sub_oh">sub!</a> <a href="#syscall">syscall</a> <a href="#system">system</a> <a href="#test">test</a> <a href="#throw">throw</a> <a href="#trace_var">trace_var</a> <a href="#trap">trap</a> <a href="#untrace_var">untrace_var</a> <p></p><hr>
<P></P>
The <code>Kernel</code> module is included by class <code>Object</code>, so its
methods are available in every Ruby object. The <code>Kernel</code>
instance methods are documented in class <code>Object</code>
beginning on page 356. This section documents the module methods.
These methods are called without a receiver and thus can be called in
functional form.
<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="Array"><b>Array</b></a></font></td><td bgcolor="#ffaaaa">
Array( <em>arg</em> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns <em>arg</em><code>.to_a</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Array(1..5)</code></td>
<td valign="top"></td>
<td valign="top"><code>[1, 2, 3, 4, 5]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="Float"><b>Float</b></a></font></td><td bgcolor="#ffaaaa">
Float( <em>arg</em> ) -> <i>aFloat</i>
</td></tr><td></td><td>
<P></P>
Returns <em>arg</em> converted to a float. Numeric types are
converted directly, <code>nil</code> is converted to <code>0.0</code>, and
the rest are converted using <em>arg</em>.to_f.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Float(1)</code></td>
<td valign="top"></td>
<td valign="top"><code>1.0</code></td>
</tr>
<tr>
<td valign="top"><code>Float(nil)</code></td>
<td valign="top"></td>
<td valign="top"><code>0.0</code></td>
</tr>
<tr>
<td valign="top"><code>Float("123.456")</code></td>
<td valign="top"></td>
<td valign="top"><code>123.456</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="Integer"><b>Integer</b></a></font></td><td bgcolor="#ffaaaa">
Integer( <em>arg</em> ) -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Converts <em>arg</em> to a <code>Fixnum</code> or <code>Bignum</code>. Numeric types
are converted directly (with floating point numbers being
truncated). If <em>arg</em> is a <code>String</code>, leading radix
indicators (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored. This
behavior is different from that of <a href="ref_c_string.html#to_i"><code>String#to_i</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Integer(123.999)</code></td>
<td valign="top"></td>
<td valign="top"><code>123</code></td>
</tr>
<tr>
<td valign="top"><code>Integer("0x1a")</code></td>
<td valign="top"></td>
<td valign="top"><code>26</code></td>
</tr>
<tr>
<td valign="top"><code>Integer(Time.new)</code></td>
<td valign="top"></td>
<td valign="top"><code>983770240</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="String"><b>String</b></a></font></td><td bgcolor="#ffaaaa">
String( <em>arg</em> ) -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Converts <em>arg</em> to a <code>String</code> by calling its
<code>to_s</code> method.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>String(self)</code></td>
<td valign="top"></td>
<td valign="top"><code>"main"</code></td>
</tr>
<tr>
<td valign="top"><code>String(self.type)</code></td>
<td valign="top"></td>
<td valign="top"><code>"Object"</code></td>
</tr>
<tr>
<td valign="top"><code>String(123456)</code></td>
<td valign="top"></td>
<td valign="top"><code>"123456"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_bq"><b>` (backquote)</b></a></font></td><td bgcolor="#ffaaaa">
`<em>cmd</em>` -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the standard output of running <em>cmd</em> in a
subshell. The built-in syntax <code>%x{...}</code>
described
on page 75 uses this method.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>`date`</code></td>
<td valign="top"></td>
<td valign="top"><code>"Sun Mar 4 23:30:40 CST 2001\n"</code></td>
</tr>
<tr>
<td valign="top"><code>`ls testdir`.split[1]</code></td>
<td valign="top"></td>
<td valign="top"><code>"main.rb"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="abort"><b>abort</b></a></font></td><td bgcolor="#ffaaaa">
abort
</td></tr><td></td><td>
<P></P>
Terminate execution immediately, effectively by calling
<code>Kernel.exit(1)</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="at_exit"><b>at_exit</b></a></font></td><td bgcolor="#ffaaaa">
at_exit { block }
-> <i>aProc</i>
</td></tr><td></td><td>
<P></P>
Converts <em>block</em> to a <code>Proc</code> object (and therefore binds
it at the point of call) and registers it for
execution when the program exits. If multiple handlers are
registered, they are executed in reverse order of registration.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
goodbye cruel world
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="autoload"><b>autoload</b></a></font></td><td bgcolor="#ffaaaa">
autoload( <i>aModule</i>, <i>aFile</i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Registers <i>aFile</i> to be loaded (using
<a href="ref_m_kernel.html#require"><code>Kernel::require</code></a>) the first time that <i>aModule</i> (which
may be a <code>String</code> or a symbol) is accessed.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
autoload :MyModule, "/usr/local/lib/modules/my_module.rb"
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="binding"><b>binding</b></a></font></td><td bgcolor="#ffaaaa">
binding -> <i>aBinding</i>
</td></tr><td></td><td>
<P></P>
Returns a <code>Binding</code> object, describing the variable and method
bindings at the point of call. This object can be used when calling
<code>eval</code> to execute the evaluated command in this
environment. Also see the description of <code>Binding</code>
beginning on page 295.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>def getBinding(param)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> return binding</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>b = getBinding("hello")</code></td>
</tr>
<tr>
<td valign="top"><code>eval "param", b</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="block_given_qm"><b>block_given?</b></a></font></td><td bgcolor="#ffaaaa">
block_given? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <code>yield</code> would execute a block
in the current context.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>def try</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> if block_given?</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> yield</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> else</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "no block"</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>try</code></td>
<td valign="top"></td>
<td valign="top"><code>"no block"</code></td>
</tr>
<tr>
<td valign="top"><code>try { "hello" }</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>try do</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> "hello"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="callcc"><b>callcc</b></a></font></td><td bgcolor="#ffaaaa">
callcc {| cont | block }
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Generates a <code>Continuation</code> object, which it passes to the associated
block. Performing a <em>cont</em><code>.call</code> will cause the
<code>callcc</code> to return (as will falling through the end of
the block). The value returned by the <code>callcc</code> is the
value of the block, or the value passed to
<em>cont</em><code>.call</code>. See <code>Continuation</code>
on page 298 for more details. Also see
<a href="ref_m_kernel.html#throw"><code>Kernel::throw</code></a> for an alternative mechanism for unwinding a
call stack.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="caller"><b>caller</b></a></font></td><td bgcolor="#ffaaaa">
caller( <i>[</i><i>anInteger</i><i>]</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the current execution
stack---an array containing strings in the form
``<em>file:line</em>'' or ``<em>file:line: in `method'</em>''. The
optional <em>anInteger</em> parameter determines the number of
initial stack entries to omit from the result.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>def a(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> caller(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>def b(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> a(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>def c(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> b(skip)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td valign="top"><code>c(0)</code></td>
<td valign="top"></td>
<td valign="top"><code>["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]</code></td>
</tr>
<tr>
<td valign="top"><code>c(1)</code></td>
<td valign="top"></td>
<td valign="top"><code>["prog:5:in `b'", "prog:8:in `c'", "prog:11"]</code></td>
</tr>
<tr>
<td valign="top"><code>c(2)</code></td>
<td valign="top"></td>
<td valign="top"><code>["prog:8:in `c'", "prog:12"]</code></td>
</tr>
<tr>
<td valign="top"><code>c(3)</code></td>
<td valign="top"></td>
<td valign="top"><code>["prog:13"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="catch"><b>catch</b></a></font></td><td bgcolor="#ffaaaa">
catch( <em>symbol</em> ) {| | block }
<P></P>
-> anObject
</td></tr><td></td><td>
<P></P>
<code>catch</code> executes its block. If a <code>throw</code> is
executed, Ruby searches up its stack for a <code>catch</code> block with a tag
corresponding to the <code>throw</code>'s <em>symbol</em>. If found, that block is
terminated, and <code>catch</code> returns the value given to
<code>throw</code>. If <code>throw</code> is not called,
the block terminates normally, and
the value of <code>catch</code> is the value of the last expression
evaluated. <code>catch</code> expressions may be nested, and the
<code>throw</code> call need not be in lexical scope.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def routine(n)
puts n
throw :done if n <= 0
routine(n-1)
end
<P></P>
catch(:done) { routine(3) }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
3
2
1
0
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="chomp"><b>chomp</b></a></font></td><td bgcolor="#ffaaaa">
chomp( <i>[</i><i>aString</i><i>]</i> )
-> $_ or <em>aString</em>
</td></tr><td></td><td>
Equivalent to
<code>$_ = $_.chomp(<em>aString</em>)</code>.
See <a href="ref_c_string.html#chomp"><code>String#chomp</code></a> on page 372.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>$_ = "now\n"</code></td>
</tr>
<tr>
<td valign="top"><code>chomp</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>chomp "ow"</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
<tr>
<td valign="top"><code>chomp "xxx"</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="chomp_oh"><b>chomp!</b></a></font></td><td bgcolor="#ffaaaa">
chomp!( <i>[</i><i>aString</i><i>]</i> )
-> $_ or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$_.chomp!(<em>aString</em>)</code>.
See <a href="ref_c_string.html#chomp_oh"><code>String#chomp!</code></a>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>$_ = "now\n"</code></td>
</tr>
<tr>
<td valign="top"><code>chomp!</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>chomp! "x"</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="chop"><b>chop</b></a></font></td><td bgcolor="#ffaaaa">
chop -> <em>aString</em>
</td></tr><td></td><td>
<P></P>
Equivalent to
<code>($_.dup).chop!</code>, except <code>nil</code> is never
returned.
See <a href="ref_c_string.html#chop_oh"><code>String#chop!</code></a> on page 372.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = "now\r\n"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>$_ = a</code></td>
</tr>
<tr>
<td valign="top"><code>chop</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>chop</code></td>
<td valign="top"></td>
<td valign="top"><code>"no"</code></td>
</tr>
<tr>
<td valign="top"><code>chop</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
<tr>
<td valign="top"><code>chop</code></td>
<td valign="top"></td>
<td valign="top"><code>""</code></td>
</tr>
<tr>
<td valign="top"><code>chop</code></td>
<td valign="top"></td>
<td valign="top"><code>""</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>"now\r\n"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="chop_oh"><b>chop!</b></a></font></td><td bgcolor="#ffaaaa">
chop! -> $_ or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$_.chop!</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = "now\r\n"</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>$_ = a</code></td>
</tr>
<tr>
<td valign="top"><code>chop!</code></td>
<td valign="top"></td>
<td valign="top"><code>"now"</code></td>
</tr>
<tr>
<td valign="top"><code>chop!</code></td>
<td valign="top"></td>
<td valign="top"><code>"no"</code></td>
</tr>
<tr>
<td valign="top"><code>chop!</code></td>
<td valign="top"></td>
<td valign="top"><code>"n"</code></td>
</tr>
<tr>
<td valign="top"><code>chop!</code></td>
<td valign="top"></td>
<td valign="top"><code>""</code></td>
</tr>
<tr>
<td valign="top"><code>chop!</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>""</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>""</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="eval"><b>eval</b></a></font></td><td bgcolor="#ffaaaa">
eval( <em>aString</em>
<i>[</i>, <em>aBinding</em> <i>[</i><em>file</em> <i>[</i><em>line</em><i>]</i><i>]</i><i>]</i>)
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Evaluates the Ruby expression(s) in <em>aString</em>. If
<em>aBinding</em> is given, the evaluation is performed in its
context.
The binding may be a <code>Binding</code> object or a <code>Proc</code>
object. If the optional <em>file</em> and <em>line</em> parameters
are present, they will be used when reporting syntax errors.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>def getBinding(str)</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code> return binding</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>end</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>str = "hello"</code></td>
</tr>
<tr>
<td valign="top"><code>eval "str + ' Fred'"</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello Fred"</code></td>
</tr>
<tr>
<td valign="top"><code>eval "str + ' Fred'", getBinding("bye")</code></td>
<td valign="top"></td>
<td valign="top"><code>"bye Fred"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="exec"><b>exec</b></a></font></td><td bgcolor="#ffaaaa">
exec( <em>command</em> <i>[</i>, <em>args</em><i>]</i>)
</td></tr><td></td><td>
<P></P>
Replaces the current process by running the given external
command.
If <code>exec</code> is given a single
argument, that argument is taken as a line that is subject to
shell expansion before being executed. If multiple arguments are
given, the second and subsequent arguments are passed as
parameters to <em>command</em> with no shell expansion. If the
first argument is a two-element array, the first element is the
command to be executed, and the second argument is used as the
<code>argv[0]</code> value, which may show up in process listings. In MSDOS
environments, the command is executed in a subshell; otherwise,
one of the <code>exec(2)</code> system calls is used, so the running
command may inherit some of the environment of the original
program (including open file descriptors).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
exec "echo *" # echoes list of files in current directory
# never get here
<P></P>
exec "echo", "*" # echoes an asterisk
# never get here
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="exit"><b>exit</b></a></font></td><td bgcolor="#ffaaaa">
exit( <i>anInteger</i>=0 )
</td></tr><td></td><td>
<P></P>
Initiates the termination of the Ruby script by raising the
<code>SystemExit</code> exception. This exception may be caught. The
optional parameter is used to return a status code to the
invoking environment.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
begin
exit
puts "never get here"
rescue SystemExit
puts "rescued a SystemExit exception"
end
puts "after begin block"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
rescued a SystemExit exception
after begin block
</pre></td></tr></table>
<P></P>
Just prior to termination, Ruby executes
any <code>at_exit</code> functions and runs any object finalizers (see
<code>ObjectSpace</code> beginning on page 434).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
at_exit { puts "at_exit function" }
ObjectSpace.define_finalizer(self, proc { puts "in finalizer" })
exit
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
at_exit function
0x4019ac90
n finals=>0
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="exit_oh"><b>exit!</b></a></font></td><td bgcolor="#ffaaaa">
exit!( <i>anInteger</i>=-1 )
</td></tr><td></td><td>
<P></P>
Similar to <a href="ref_m_kernel.html#exit"><code>Kernel::exit</code></a>, but exception handling,
<code>at_exit</code> functions, and finalizers are bypassed.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="fail"><b>fail</b></a></font></td><td bgcolor="#ffaaaa">
fail<br>fail( <i>aString</i> )<br>fail( <i>anException</i> <i>[</i>, <i>aString</i>
<i>[</i><i>anArray</i><i>]</i><i>]</i> )
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_m_kernel.html#raise"><code>Kernel::raise</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="fork"><b>fork</b></a></font></td><td bgcolor="#ffaaaa">
fork <i>[</i>{ block }
<i>]</i> -> <i>aFixnum</i>
or <code>nil</code>
</td></tr><td></td><td>
Creates a subshell. If a block is specified, that block is run
in the subshell, and the subshell terminates with a status of
zero. Otherwise, the <code>fork</code> call returns twice, once in
the parent, returning the process id of the child, and once in
the child, returning <code>nil</code>. The child process can
exit using <a href="ref_m_kernel.html#exit_oh"><code>Kernel::exit!</code></a> to avoid running any
<code>at_exit</code> functions. The parent process should use
<a href="ref_m_process.html#wait"><code>Process::wait</code></a> to collect the termination statuses of its
children; otherwise, the operating system may accumulate zombie
processes.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
fork do
3.times {|i| puts "Child: #{i}" }
end
3.times {|i| puts "Parent: #{i}" }
Process.wait
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Parent: 0
Child: 0
Child: 1
Parent: 1
Parent: 2
Child: 2
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="format"><b>format</b></a></font></td><td bgcolor="#ffaaaa">
format( <i>aString</i> <i>[</i>,
<i>anObject</i><i>]<sup>*</sup></i> ) -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_m_kernel.html#sprintf"><code>Kernel::sprintf</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gets"><b>gets</b></a></font></td><td bgcolor="#ffaaaa">
gets( <i>aString</i>=<code>$/</code> )
-> <i>aString</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Returns (and assigns to <code>$_</code>) the next line from
the list of files in <code>ARGV</code> (or <code>$*</code>), or
from standard input if no files are present on the command line.
Returns <code>nil</code> at end of file.
The optional argument specifies the
record separator. The separator is
included with the contents of each record. A separator of <code>nil</code>
reads the entire contents, and a zero-length separator reads the
input one paragraph at a time, where paragraphs are divided
by two consecutive newlines. If multiple filenames are
present in <code>ARGV</code>, <code>gets(nil)</code> will read the
contents one file at a time.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
ARGV << "testfile"
print while gets
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
This is line one
This is line two
This is line three
And so on...
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="global_variables"><b>global_variables</b></a></font></td><td bgcolor="#ffaaaa">
global_variables
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of the names of global variables.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>global_variables.grep /std/</code></td>
<td valign="top"></td>
<td valign="top"><code>["$stderr", "$stdout", "$stdin"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gsub"><b>gsub</b></a></font></td><td bgcolor="#ffaaaa">
gsub( <i>pattern</i>, <i>replacement</i> )
-> <i>aString</i><br>gsub( <i>pattern</i> ) {| | block }
<P></P>
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$_.gsub...</code>, except that <code>$_</code>
receives the modified result.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>$_ = "quick brown fox"</code></td>
</tr>
<tr>
<td valign="top"><code>gsub /[aeiou]/, '*'</code></td>
<td valign="top"></td>
<td valign="top"><code>"q**ck br*wn f*x"</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"q**ck br*wn f*x"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gsub_oh"><b>gsub!</b></a></font></td><td bgcolor="#ffaaaa">
gsub!( <i>pattern</i>, <i>replacement</i> )
-> <i>aString</i> or <code>nil</code><br>gsub!( <i>pattern</i> ) {| | block }
<P></P>
-> <i>aString</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <a href="ref_m_kernel.html#gsub"><code>Kernel::gsub</code></a>, except <code>nil</code> is returned if
<code>$_</code> is not modified.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>$_ = "quick brown fox"</code></td>
</tr>
<tr>
<td valign="top"><code>gsub! /cat/, '*'</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>$_</code></td>
<td valign="top"></td>
<td valign="top"><code>"quick brown fox"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="iterator_qm"><b>iterator?</b></a></font></td><td bgcolor="#ffaaaa">
iterator? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_m_kernel.html#block_given_qm"><code>Kernel::block_given?</code></a>. The <code>iterator?</code>
method will be removed in Ruby 1.8.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="lambda"><b>lambda</b></a></font></td><td bgcolor="#ffaaaa">
lambda {| | block }
-> <i>aProc</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_m_kernel.html#proc"><code>Kernel::proc</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="load"><b>load</b></a></font></td><td bgcolor="#ffaaaa">
load( <i>aFileName</i>,
<i>wrap</i>=<code>false</code> ) -> <code>true</code>
</td></tr><td></td><td>
<P></P>
Loads and executes the Ruby program in the file
<i>aFileName</i>. If the filename does not resolve to an absolute path, the
file is searched for in the library directories listed in
<code>$:</code>. If the optional <i>wrap</i> parameter is
<code>true</code>, the loaded script will be executed under an
anonymous module, protecting the calling program's global
namespace. Any local variables in the loaded file will not be
propagated to the loading environment.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="local_variables"><b>local_variables</b></a></font></td><td bgcolor="#ffaaaa">
local_variables
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the names of the current local variables.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>fred = 1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>for i in 1..10</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 valign="top"><code>local_variables</code></td>
<td valign="top"></td>
<td valign="top"><code>["fred", "i"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="loop"><b>loop</b></a></font></td><td bgcolor="#ffaaaa">
loop {| | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Repeatedly executes the block.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
loop {
print "Input: "
break if !gets or $_ =~ /^qQ/
# ...
}
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
open( <i>aString</i>
<i>[</i>, <i>aMode</i> <i>[</i><i>perm</i><i>]</i><i>]</i> )
-> <i>anIO</i> or <code>nil</code><br>open( <i>aString</i>
<i>[</i>, <i>aMode</i> <i>[</i><i>perm</i><i>]</i><i>]</i> ) {| anIO | block }
<P></P>
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Creates an <code>IO</code> object connected to the given stream, file,
or subprocess.
<P></P>
If <i>aString</i> does not start with a pipe character
(``<code>|</code>''), treat it as the name of a file to open using the
specified mode defaulting to ``<code>r</code>'' (see the table of
valid modes on page 331). If a file is being
created, its initial permissions may be set using the integer
third parameter.
<P></P>
If a block is specified, it will be invoked with the <code>File</code>
object as a parameter, and the file will be automatically
closed when the block terminates. The call always returns
<code>nil</code> in this case.
<P></P>
If <i>aString</i> starts with a pipe character, a subprocess
is created, connected to the caller by a pair of pipes. The
returned <code>IO</code> object may be used to write to the standard
input and read from the standard output of this subprocess.
If the command following the ``<code>|</code>'' is a single minus sign,
Ruby forks, and this subprocess is connected to the parent.
In the subprocess, the <code>open</code> call returns <code>nil</code>. If
the command is not ``<code>-</code>'', the subprocess runs the command. If
a block is associated with an <code>open("|-")</code> call, that
block will be run twice---once in the parent and once in the
child. The block parameter will be an <code>IO</code> object in the
parent and <code>nil</code> in the child. The parent's <code>IO</code> object will
be connected to the child's <code>$stdin</code> and <code>$stdout</code>.
The subprocess will be terminated at the end of the
block.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
open("testfile") do |f|
print f.gets
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
This is line one
</pre></td></tr></table>
<P></P>
Open a subprocess and read its output:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
cmd = open("|date")
print cmd.gets
cmd.close
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Sun Mar 4 23:30:41 CST 2001
</pre></td></tr></table>
<P></P>
Open a subprocess running the same Ruby program:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
f = open("|-", "w+")
if f == nil
puts "in Child"
exit
else
puts "Got: #{f.gets}"
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Got: in Child
</pre></td></tr></table>
<P></P>
Open a subprocess using a block to receive the I/O object:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
open("|-") do |f|
if f == nil
puts "in Child"
else
puts "Got: #{f.gets}"
end
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Got: in Child
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="p"><b>p</b></a></font></td><td bgcolor="#ffaaaa">
p( <i>[</i><i>anObject</i><i>]<sup>+</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
For each object, directly writes <code><i>anObject</i>.inspect</code> followed by
the current output record separator to the program's standard
output. <code>p</code> bypasses the Ruby I/O libraries.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
p self
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
main
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="print"><b>print</b></a></font></td><td bgcolor="#ffaaaa">
print( <i>[</i><i>anObject</i><i>]<sup>*</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Prints each object in turn to <code>$defout</code>. If the
output field separator (<code>$,</code>)
is not <code>nil</code>, its contents will appear
between each field.
If the output record separator
(<code>$\</code>)
is not <code>nil</code>, it will be appended to the output. If
no arguments are given, prints <code>$_</code>. Objects that aren't
strings will be converted by calling their <code>to_s</code>
method.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
cat12399
cat, 1, 2, 3, 99
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="printf"><b>printf</b></a></font></td><td bgcolor="#ffaaaa">
printf( <i>anIO</i>, <i>aString</i> <i>[</i>, <i>anObject</i><i>]<sup>*</sup></i> )
-> <code>nil</code><br>printf( <i>aString</i> <i>[</i>, <i>anObject</i><i>]<sup>*</sup></i> )
-> <code>nil</code>
</td></tr><td></td><td>
Equivalent to:
<P></P>
<i>anIO.</i><code>write sprintf(</code><i>aString, anObject</i><code> ...)</code><br><em>or</em><br><code>$defout.write sprintf(</code><i>aString, anObject</i><code> ...)</code>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="proc"><b>proc</b></a></font></td><td bgcolor="#ffaaaa">
proc { block }
-> <i>aProc</i>
</td></tr><td></td><td>
<P></P>
Creates a new procedure object from the given block. Equivalent
to <a href="ref_c_proc.html#new"><code>Proc::new</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>aProc = proc { "hello" }</code></td>
</tr>
<tr>
<td valign="top"><code>aProc.call</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="putc"><b>putc</b></a></font></td><td bgcolor="#ffaaaa">
putc( <i>anInteger</i> ) -> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$defout.putc(</code><i>anInteger</i><code>)</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="puts"><b>puts</b></a></font></td><td bgcolor="#ffaaaa">
puts( <i>[</i><i>args</i><i>]<sup>*</sup></i> ) -> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$defout.puts(</code><i>args</i><code>)</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="raise"><b>raise</b></a></font></td><td bgcolor="#ffaaaa">
raise<br>raise( <i>aString</i> )<br>raise( <i>anException</i> <i>[</i>, <i>aString</i>
<i>[</i><i>anArray</i><i>]</i><i>]</i> )
</td></tr><td></td><td>
<P></P>
With no arguments, raises the
exception in <code>$!</code> or raises a
<code>RuntimeError</code> if <code>$!</code> is <code>nil</code>.
With a single <code>String</code> argument, raises a
<code>RuntimeError</code> with the string as a message. Otherwise, the
first parameter should be the name of an <code>Exception</code> class
(or an object that returns an <code>Exception</code> when sent
<code>exception</code>). The
optional second parameter sets the message associated with the
exception, and the third parameter is an array of callback
information. Exceptions are caught by the <code>rescue</code> clause of
<code>begin...end</code> blocks.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
raise "Failed to create socket"
raise ArgumentError, "No parameters", caller
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="rand"><b>rand</b></a></font></td><td bgcolor="#ffaaaa">
rand( <i>max</i>=0 )
-> <i>aNumber</i>
</td></tr><td></td><td>
Converts <i>max</i> to an integer using max<sub>1</sub> =
max<code>.to_i.abs</code>.
If the result is zero, returns a pseudorandom floating point
number greater than or equal to 0.0 and less than
1.0. Otherwise, returns a pseudorandom integer greater than or
equal to zero and less than max<sub>1</sub>. <a href="ref_m_kernel.html#srand"><code>Kernel::srand</code></a> may be
used to ensure repeatable sequences of random numbers between
different runs of the program.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>srand 1234</code></td>
<td valign="top"></td>
<td valign="top"><code>0</code></td>
</tr>
<tr>
<td valign="top"><code>[ rand, rand ]</code></td>
<td valign="top"></td>
<td valign="top"><code>[0.7408769294, 0.2145348572]</code></td>
</tr>
<tr>
<td valign="top"><code>[ rand(10), rand(1000) ]</code></td>
<td valign="top"></td>
<td valign="top"><code>[3, 323]</code></td>
</tr>
<tr>
<td valign="top"><code>srand 1234</code></td>
<td valign="top"></td>
<td valign="top"><code>1234</code></td>
</tr>
<tr>
<td valign="top"><code>[ rand, rand ]</code></td>
<td valign="top"></td>
<td valign="top"><code>[0.7408769294, 0.2145348572]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="readline"><b>readline</b></a></font></td><td bgcolor="#ffaaaa">
readline( <i>[</i><i>aString</i>=<code>$/</code><i>]</i> )
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Equivalent to <a href="ref_m_kernel.html#gets"><code>Kernel::gets</code></a>, except <code>readline</code> raises
<code>EOFError</code> at end of file.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="readlines"><b>readlines</b></a></font></td><td bgcolor="#ffaaaa">
readlines( <i>[</i><i>aString</i>=<code>$/</code><i>]</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array containing the lines returned by
calling <code>Kernel.gets(<i>aString</i>)</code> until the end of file.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="require"><b>require</b></a></font></td><td bgcolor="#ffaaaa">
require( <i>aString</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
Ruby tries to load the library named <i>aString</i>, returning
<code>true</code> if successful. If the filename does not resolve to an
absolute path, it will be searched for in the directories listed
in <code>$:</code>. If the file has the extension
``.rb'', it is loaded as a source file; if the extension is
``.so'', ``.o'', or ``.dll'',<em>[Or whatever the default
shared library extension is on the current platform.]</em> Ruby
loads the shared library as a Ruby extension. Otherwise, Ruby
tries adding ``.rb'', ``.so'', and so on to the name. The name
of the loaded feature is added to the array in <code>$"</code>. A
feature will not be loaded if it already appears in <code>$"</code>.
<code>require</code> returns <code>true</code> if the feature was
successfully loaded.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require "my-library.rb"
require "db-driver"
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="scan"><b>scan</b></a></font></td><td bgcolor="#ffaaaa">
scan( <i>pattern</i> ) -> <i>anArray</i><br>scan( <i>pattern</i> ) {| | block }
-> $_
</td></tr><td></td><td>
<P></P>
Equivalent to calling <code>$_.scan</code>. See <a href="ref_c_string.html#scan"><code>String#scan</code></a>
on page 378.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="select"><b>select</b></a></font></td><td bgcolor="#ffaaaa">
select( <i>readArray</i> <i>[</i>,
<i>writeArray</i>
<i>[</i><i>errorArray</i> <i>[</i><i>timeout</i><i>]</i><i>]</i><i>]</i> )
-> <i>anArray</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Performs
a low-level <code>select</code> call, which waits for data to
become available from input/output devices. The first three
parameters are arrays of <code>IO</code> objects or <code>nil</code>. The last is a
timeout in seconds, which should be an <code>Integer</code> or a
<code>Float</code>. The call waits for data to become available for any
of the <code>IO</code> objects in <i>readArray</i>, for buffers to have
cleared sufficiently to enable writing to any of the devices in
<i>writeArray</i>, or for an error to occur on the devices in
<i>errorArray</i>. If one or more of these conditions are met, the
call returns a three-element array containing arrays of the
<code>IO</code> objects that were ready. Otherwise, if there is no
change in status for <i>timeout</i> seconds, the call returns
<code>nil</code>. If all parameters are <code>nil</code>, the current thread sleeps forever.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>select( [$stdin], nil, nil, 1.5 )</code></td>
<td valign="top"></td>
<td valign="top"><code>[[#<IO:0x4019202c>], [], []]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="set_trace_func"><b>set_trace_func</b></a></font></td><td bgcolor="#ffaaaa">
set_trace_func( <i>aProc</i> ) -> <i>aProc</i><br>set_trace_func( <code>nil</code> ) -> <code>nil</code>
</td></tr><td></td><td>
Establishes <i>aProc</i> as the handler for tracing, or disables
tracing if the parameter is <code>nil</code>. <i>aProc</i>
takes up to six parameters: an event name, a filename, a line
number, an object id, a binding, and the name of a
class. <i>aProc</i> is invoked whenever an event
occurs. Events are:
<code>c-call</code> (call a C-language routine),
<code>c-return</code> (return from a C-language routine),
<code>call</code> (call a Ruby method),
<code>class</code> (start a class or module definition),
<code>end</code> (finish a class or module definition),
<code>line</code> (execute code on a new line),
<code>raise</code> (raise an exception), and
<code>return</code> (return from a Ruby method).
Tracing is disabled within the context of <i>aProc</i>.
<P></P>
See the example starting on page 271 for more
information.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="singleton_method_added"><b>singleton_method_added</b></a></font></td><td bgcolor="#ffaaaa">
singleton_method_added( <i>aFixnum</i> ) -> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Invoked with a symbol id whenever a singleton method is added to
a module or a class. The default implementation in <code>Kernel</code>
ignores this, but subclasses may override the method to provide
specialized functionality.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
class Test
def Test.singleton_method_added(id)
puts "Added #{id.id2name} to Test"
end
def a() end
def Test.b() end
end
def Test.c() end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Added singleton_method_added to Test
Added b to Test
Added c to Test
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sleep"><b>sleep</b></a></font></td><td bgcolor="#ffaaaa">
sleep( <i>[</i><i>aNumeric</i><i>]</i> )
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Suspends the current thread for <i>aNumber</i> seconds (which may be a <code>Float</code>
with fractional seconds). Returns the actual number of seconds
slept (rounded), which may be less than that asked for if the
thread was interrupted by a
<code>SIGALRM</code>, or if another thread
calls <a href="ref_c_thread.html#run"><code>Thread#run</code></a>. An argument of zero causes <code>sleep</code>
to sleep forever.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Time.new</code></td>
<td valign="top"></td>
<td valign="top"><code>Sun Mar 04 23:30:41 CST 2001</code></td>
</tr>
<tr>
<td valign="top"><code>sleep 1.2</code></td>
<td valign="top"></td>
<td valign="top"><code>2</code></td>
</tr>
<tr>
<td valign="top"><code>Time.new</code></td>
<td valign="top"></td>
<td valign="top"><code>Sun Mar 04 23:30:43 CST 2001</code></td>
</tr>
<tr>
<td valign="top"><code>sleep 1.9</code></td>
<td valign="top"></td>
<td valign="top"><code>2</code></td>
</tr>
<tr>
<td valign="top"><code>Time.new</code></td>
<td valign="top"></td>
<td valign="top"><code>Sun Mar 04 23:30:45 CST 2001</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="split"><b>split</b></a></font></td><td bgcolor="#ffaaaa">
split( <i>[</i><i>pattern</i>
<i>[</i><i>limit</i><i>]</i><i>]</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Equivalent to
<code>$_.split(<i>pattern</i>, <i>limit</i>)</code>. See <a href="ref_c_string.html#split"><code>String#split</code></a>
on page 379.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sprintf"><b>sprintf</b></a></font></td><td bgcolor="#ffaaaa">
sprintf( <i>aFormatString</i>
<i>[</i>, <i>arguments</i><i>]<sup>*</sup></i> ) -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the string resulting
from applying <i>aFormatString</i> to any additional arguments.
Within the format string, any characters other than format
sequences are copied to the result.
A format sequence consists of a percent sign, followed by
optional flags, width, and precision indicators, then terminated
with a field type character. The field type controls how the
corresponding <code>sprintf</code> argument is to be interpreted,
while the flags modify that interpretation.
The flag characters are shown in Table
23.1 on page 428, and the field type characters are listed
in Table 23.2.
<P></P>
The field width is an optional integer, followed optionally by a
period and a precision. The width specifies the minimum number
of characters that will be written to the result for this
field. For numeric fields, the precision controls the number of
decimal places displayed. For string fields, the precision
determines the maximum number of characters to be copied from
the string. (Thus, the format sequence <code>%10.10s</code> will always
contribute exactly ten characters to the result.)
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b><code>sprintf</code> flag characters</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Flag</b></td>
<td valign="top"><b>Applies to</b></td>
<td valign="top"><b>Meaning</b></td>
</tr>
<tr>
<td valign="top"><code><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"></code> (space)</td>
<td valign="top">bdeEfgGioxXu</td>
<td valign="top">Leave a
space at the start of positive numbers.</td>
</tr>
<tr>
<td valign="top"><code>#</code></td>
<td valign="top">beEfgGoxX</td>
<td valign="top">Use an alternative format. For the
conversions `o', `x', `X', and `b', prefix the result with
``0'', ``0x'', ``0X'', and ``0b'', respectively. For `e',
`E', `f', `g', and 'G', force a decimal point to be added,
even if no digits follow. For `g' and 'G', do not remove
trailing zeros.</td>
</tr>
<tr>
<td valign="top"><code>+</code></td>
<td valign="top">bdeEfgGioxXu</td>
<td valign="top">Add a leading plus sign to
positive numbers.</td>
</tr>
<tr>
<td valign="top"><code>-</code></td>
<td valign="top">all</td>
<td valign="top">Left-justify the result of this conversion.</td>
</tr>
<tr>
<td valign="top"><code>0</code> (zero)</td>
<td valign="top">all</td>
<td valign="top">Pad with zeros, not spaces.</td>
</tr>
<tr>
<td valign="top"><code>*</code></td>
<td valign="top">all</td>
<td valign="top">Use the next argument as the field width. If
negative, left-justify the result. If the asterisk is
followed by a number and a dollar sign, use
the indicated argument as the width.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b><code>sprintf</code> field types</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Field</b></td>
<td valign="top"><b>Conversion</b></td>
</tr>
<tr>
<td valign="top">b</td>
<td valign="top">Convert argument as a binary number.</td>
</tr>
<tr>
<td valign="top">c</td>
<td valign="top">Argument is the numeric code for a single character.</td>
</tr>
<tr>
<td valign="top">d</td>
<td valign="top">Convert argument as a decimal number.</td>
</tr>
<tr>
<td valign="top">E</td>
<td valign="top">Equivalent to `e', but uses an uppercase E to indicate
the exponent.</td>
</tr>
<tr>
<td valign="top">e</td>
<td valign="top">Convert floating point argument into exponential notation
with one digit before the decimal point. The precision
determines the number of fractional digits (defaulting to six).</td>
</tr>
<tr>
<td valign="top">f</td>
<td valign="top">Convert floating point argument as <code>[<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">-]ddd.ddd</code>,
where the precision determines the number of digits after
the decimal point.</td>
</tr>
<tr>
<td valign="top">G</td>
<td valign="top">Equivalent to `g', but use an uppercase `E' in exponent
form.</td>
</tr>
<tr>
<td valign="top">g</td>
<td valign="top">Convert a floating point number using exponential form
if the exponent is less than -4 or greater than or
equal to the precision, or in <code>d.dddd</code> form otherwise.</td>
</tr>
<tr>
<td valign="top">i</td>
<td valign="top">Identical to `d'.</td>
</tr>
<tr>
<td valign="top">o</td>
<td valign="top">Convert argument as an octal number.</td>
</tr>
<tr>
<td valign="top">s</td>
<td valign="top">Argument is a string to be substituted. If the format
sequence contains a precision, at most that many characters
will be copied.</td>
</tr>
<tr>
<td valign="top">u</td>
<td valign="top">Treat argument as an unsigned decimal number.</td>
</tr>
<tr>
<td valign="top">X</td>
<td valign="top">Convert argument as a hexadecimal number using uppercase letters.</td>
</tr>
<tr>
<td valign="top">x</td>
<td valign="top">Convert argument as a hexadecimal number.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>sprintf("%d %04x", 123, 123)</code></td>
<td valign="top"></td>
<td valign="top"><code>"123<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">007b"</code></td>
</tr>
<tr>
<td valign="top"><code>sprintf("%08b '%4s'", 123, 123)</code></td>
<td valign="top"></td>
<td valign="top"><code>"01111011<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">'<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">123'"</code></td>
</tr>
<tr>
<td valign="top"><code>sprintf("%*2$s %d", "hello", 10)</code></td>
<td valign="top"></td>
<td valign="top"><code>"<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">hello<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">10"</code></td>
</tr>
<tr>
<td valign="top"><code>sprintf("%*2$s %d", "hello", -10)</code></td>
<td valign="top"></td>
<td valign="top"><code>"hello<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]"><img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">-10"</code></td>
</tr>
<tr>
<td valign="top"><code>sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23)</code></td>
<td valign="top"></td>
<td valign="top"><code>"+1.23:<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">1.23:1.23"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="srand"><b>srand</b></a></font></td><td bgcolor="#ffaaaa">
srand( <i>[</i><i>aNumber</i><i>]</i> )
-> <i>oldSeed</i>
</td></tr><td></td><td>
<P></P>
Seeds the pseudorandom number generator to the value of
<i>aNumber</i>.<code>to_i.abs</code>. If <i>aNumber</i> is omitted or zero,
seeds the generator using a combination of the time, the process
id, and a sequence number. (This is also the behavior if
<a href="ref_m_kernel.html#rand"><code>Kernel::rand</code></a> is called without previously calling
<code>srand</code>, but without the sequence.)
By setting the seed to a known value, scripts can be made
deterministic during testing. The previous seed value is
returned. Also see <a href="ref_m_kernel.html#rand"><code>Kernel::rand</code></a> on page 425.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sub"><b>sub</b></a></font></td><td bgcolor="#ffaaaa">
sub( <i>pattern</i>, <i>replacement</i> )
-> $_<br>sub( <i>pattern</i> ) { <i>block</i> }
-> $_
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$_.sub(<i>args</i>)</code>, except that <code>$_</code>
will be updated if substitution occurs.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sub_oh"><b>sub!</b></a></font></td><td bgcolor="#ffaaaa">
sub!( <i>pattern</i>, <i>replacement</i> )
-> $_ or <code>nil</code><br>sub!( <i>pattern</i> ) { <i>block</i> }
-> $_ or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>$_.sub!(<i>args</i>)</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="syscall"><b>syscall</b></a></font></td><td bgcolor="#ffaaaa">
syscall( <i>aFixnum</i>
<i>[</i>, <i>args</i><i>]<sup>*</sup></i> )
-> <i>anInteger</i>
</td></tr><td></td><td>
<P></P>
Calls the operating system function identified by <i>aFixnum</i>,
passing in the arguments, which must be either <code>String</code> objects, or
<code>Integer</code> objects that ultimately fit within a native <code>long</code>.
Up to nine parameters may be passed (14 on the
Atari-ST). The function identified
by <i>Fixnum</i> is system dependent. On some Unix systems, the
numbers may be obtained from a header file called
<code>syscall.h</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
hello
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="system"><b>system</b></a></font></td><td bgcolor="#ffaaaa">
system( <i>aCmd</i> <i>[</i>, <i>args</i><i>]<sup>*</sup></i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Executes <i>aCmd</i> in a subshell, returning <code>true</code> if the
command was found and ran successfully, <code>false</code>
otherwise. A detailed error code is available in <code>$?</code>. The
arguments are processed in the same way as for <a href="ref_m_kernel.html#exec"><code>Kernel::exec</code></a>
on page 419.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
system("echo *")
system("echo", "*")
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
config.h main.rb
*
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="test"><b>test</b></a></font></td><td bgcolor="#ffaaaa">
test(<i>aCmd</i>, <i>file1</i> <i>[</i>, <i>file2</i><i>]</i> )
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Uses the integer <i>aCmd</i> to perform various tests on
<i>file1</i> (Table 23.3 on page 430) or on <i>file1</i> and
<i>file2</i> (Table 23.4).
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>File tests with a single argument</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Integer</b></td>
<td valign="top"><b>Description</b></td>
<td valign="top"><b>Returns</b></td>
</tr>
<tr>
<td valign="top">?A</td>
<td valign="top">Last access time for <i>file1</i></td>
<td valign="top">Time</td>
</tr>
<tr>
<td valign="top">?b</td>
<td valign="top">True if <i>file1</i> is a block device</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?c</td>
<td valign="top">True if <i>file1</i> is a character device</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?C</td>
<td valign="top">Last change time for <i>file1</i></td>
<td valign="top">Time</td>
</tr>
<tr>
<td valign="top">?d</td>
<td valign="top">True if <i>file1</i> exists and is a directory</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?e</td>
<td valign="top">True if <i>file1</i> exists</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?f</td>
<td valign="top">True if <i>file1</i> exists and is a regular file</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?g</td>
<td valign="top">True if <i>file1</i> has the <code>setgid</code> bit set (false under
NT)</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?G</td>
<td valign="top">True if <i>file1</i> exists and has a group ownership equal to
the caller's group</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?k</td>
<td valign="top">True if <i>file1</i> exists and has the sticky bit set</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?l</td>
<td valign="top">True if <i>file1</i> exists and is a symbolic link</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?M</td>
<td valign="top">Last modification time for <i>file1</i></td>
<td valign="top">Time</td>
</tr>
<tr>
<td valign="top">?o</td>
<td valign="top">True if <i>file1</i> exists and is owned by the caller's
effective uid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?O</td>
<td valign="top">True if <i>file1</i> exists and is owned by the caller's
real uid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?p</td>
<td valign="top">True if <i>file1</i> exists and is a fifo</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?r</td>
<td valign="top">True if file is readable by the effective uid/gid of the
caller</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?R</td>
<td valign="top">True if file is readable by the real uid/gid of the
caller</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?s</td>
<td valign="top">If <i>file1</i> has nonzero size, return the size, otherwise
return <code>nil</code></td>
<td valign="top">Integer or <code>nil</code></td>
</tr>
<tr>
<td valign="top">?S</td>
<td valign="top">True if <i>file1</i> exists and is a socket</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?u</td>
<td valign="top">True if <i>file1</i> has the setuid bit set</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?w</td>
<td valign="top">True if <i>file1</i> exists and is writable by the effective
uid/gid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?W</td>
<td valign="top">True if <i>file1</i> exists and is writable by the real
uid/gid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?x</td>
<td valign="top">True if <i>file1</i> exists and is executable by the effective
uid/gid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?X</td>
<td valign="top">True if <i>file1</i> exists and is executable by the real
uid/gid</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr>
<td valign="top">?z</td>
<td valign="top">True if <i>file1</i> exists and has a zero length</td>
<td valign="top"><code>true</code> or <code>false</code></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>File tests with two arguments</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Integer</b></td>
<td valign="top"><b>Description</b></td>
</tr>
<tr>
<td valign="top">?-</td>
<td valign="top">True if <i>file1</i> is a hard link to <i>file2</i></td>
</tr>
<tr>
<td valign="top">?=</td>
<td valign="top">True if the modification times of <i>file1</i> and <i>file2</i> are
equal</td>
</tr>
<tr>
<td valign="top">?<</td>
<td valign="top">True if the modification time of <i>file1</i> is prior to that
of <i>file2</i></td>
</tr>
<tr>
<td valign="top">?></td>
<td valign="top">True if the modification time of <i>file1</i> is after that
of <i>file2</i></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="throw"><b>throw</b></a></font></td><td bgcolor="#ffaaaa">
throw( <i>aSymbol</i>
<i>[</i>, <i>anObject</i><i>]</i> )
</td></tr><td></td><td>
<P></P>
Transfers control to the end of the active <code>catch</code> block
waiting for <i>aSymbol</i>. Raises <code>NameError</code> if
there is no <code>catch</code> block
for the symbol. The optional second
parameter supplies a return value for the <code>catch</code> block,
which otherwise defaults to <code>nil</code>. For examples, see
<a href="ref_m_kernel.html#catch"><code>Kernel::catch</code></a> on page 417.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="trace_var"><b>trace_var</b></a></font></td><td bgcolor="#ffaaaa">
trace_var( <i>aSymbol</i>, <i>aCmd</i> ) -> <code>nil</code><br>trace_var( <i>aSymbol</i> ) {| val | block }
<P></P>
-> <code>nil</code>
</td></tr><td></td><td>
Controls tracing of assignments to global variables. The
parameter <i>aSymbol</i> identifies the variable (as either a
string name or a symbol identifier).
<i>cmd</i> (which may be a string or a <code>Proc</code> object) or block
is executed whenever the variable is assigned. The block or
<code>Proc</code> object receives the variable's new value as a
parameter. Also see <a href="ref_m_kernel.html#untrace_var"><code>Kernel::untrace_var</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
$_ = "hello"
$_ = ' there'
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
$_ is now 'hello'
$_ is now ' there'
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="trap"><b>trap</b></a></font></td><td bgcolor="#ffaaaa">
trap( <i>signal</i>, <i>cmd</i> ) -> <i>anObject</i><br>trap( <i>signal</i> ) {| | block }
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Specifies the handling of signals. The first parameter is a
signal name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on)
or a signal number. The characters ``SIG'' may be omitted from
the signal name. The command or block specifies code to be run
when the signal is raised. If the command is the string
``IGNORE'' or ``SIG_IGN'', the signal will be ignored. If the
command is ``DEFAULT'' or ``SIG_DFL'', the operating system's
default handler will be invoked. If the command is ``EXIT'', the
script will be terminated by the signal. Otherwise, the given
command or block will be run.
<P></P>
The special signal name ``EXIT'' or signal
number zero will be invoked just prior to program termination.
<P></P>
<code>trap</code> returns the previous handler for the given signal.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
trap 0, proc { puts "Terminating: #{$$}" }
trap("CLD") { puts "Child died" }
fork && Process.wait
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Terminating: 16422
Child died
Terminating: 16421
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="untrace_var"><b>untrace_var</b></a></font></td><td bgcolor="#ffaaaa">
untrace_var( <i>aSymbol</i> <i>[</i>,
<i>aCmd</i><i>]</i> ) -> <i>anArray</i> or <code>nil</code>
</td></tr><td></td><td>
Removes tracing
for the specified command on the given global variable and
returns <code>nil</code>. If no command is specified, removes all tracing for
that variable and returns an array containing the commands
actually removed.
<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_m_gc.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_m_marshal.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>
|