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 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
|
<ppdoc>
<copyright>
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
</copyright>
<class name="Kernel" type="module">
<p/>
The <modulename>Kernel</modulename> module is included by class <classname>Object</classname>, so its
methods are available in every Ruby object. The <modulename>Kernel</modulename>
instance methods are documented in class <classname>Object</classname>
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/>
<methods type="class">
<method name="Array" ref="Array">
<callseq>
Array( <em>arg</em> ) <returns><obj>anArray</obj></returns>
</callseq>
<desc>
<p/>
Returns <em>arg</em><tt>.to_a</tt>.
<p/>
<codefragment>
<fullcode><![CDATA[ Array(1..5)
]]></fullcode><rubycode>
<tr>
<td><tt>Array(1..5)</tt></td>
<td>»</td>
<td><tt>[1,<nbsp/>2,<nbsp/>3,<nbsp/>4,<nbsp/>5]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="Float" ref="Float">
<callseq>
Float( <em>arg</em> ) <returns><obj>aFloat</obj></returns>
</callseq>
<desc>
<p/>
Returns <em>arg</em> converted to a float. Numeric types are
converted directly, <tt>nil</tt> is converted to <tt>0.0</tt>, and
the rest are converted using <em>arg</em>.to_f.
<p/>
<codefragment>
<fullcode><![CDATA[ Float(1)
Float(nil)
Float("123.456")
]]></fullcode><rubycode>
<tr>
<td><tt>Float(1)</tt></td>
<td>»</td>
<td><tt>1.0</tt></td>
</tr>
<tr>
<td><tt>Float(nil)</tt></td>
<td>»</td>
<td><tt>0.0</tt></td>
</tr>
<tr>
<td><tt>Float("123.456")</tt></td>
<td>»</td>
<td><tt>123.456</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="Integer" ref="Integer">
<callseq>
Integer( <em>arg</em> ) <returns><obj>anInteger</obj></returns>
</callseq>
<desc>
<p/>
Converts <em>arg</em> to a <classname>Fixnum</classname> or <classname>Bignum</classname>. Numeric types
are converted directly (with floating point numbers being
truncated). If <em>arg</em> is a <classname>String</classname>, leading radix
indicators (<tt>0</tt>, <tt>0b</tt>, and <tt>0x</tt>) are honored. This
behavior is different from that of <cim><file>string</file><front>String</front><back>to_i</back><mref>to_i</mref></cim>.
<p/>
<codefragment>
<fullcode><![CDATA[ Integer(123.999)
Integer("0x1a")
Integer(Time.new)
]]></fullcode><rubycode>
<tr>
<td><tt>Integer(123.999)</tt></td>
<td>»</td>
<td><tt>123</tt></td>
</tr>
<tr>
<td><tt>Integer("0x1a")</tt></td>
<td>»</td>
<td><tt>26</tt></td>
</tr>
<tr>
<td><tt>Integer(Time.new)</tt></td>
<td>»</td>
<td><tt>983770240</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="String" ref="String">
<callseq>
String( <em>arg</em> ) <returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Converts <em>arg</em> to a <classname>String</classname> by calling its
<meth>to_s</meth> method.
<p/>
<codefragment>
<fullcode><![CDATA[ String(self)
String(self.type)
String(123456)
]]></fullcode><rubycode>
<tr>
<td><tt>String(self)</tt></td>
<td>»</td>
<td><tt>"main"</tt></td>
</tr>
<tr>
<td><tt>String(self.type)</tt></td>
<td>»</td>
<td><tt>"Object"</tt></td>
</tr>
<tr>
<td><tt>String(123456)</tt></td>
<td>»</td>
<td><tt>"123456"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="` (backquote)" ref="_bq">
<callseq>
`<em>cmd</em>` <returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Returns the standard output of running <em>cmd</em> in a
subshell. The built-in syntax <tt>%x{...}</tt>
described
on page 75 uses this method.
<p/>
<codefragment>
<fullcode><![CDATA[ `date`
`ls testdir`.split[1]
]]></fullcode><rubycode>
<tr>
<td><tt>`date`</tt></td>
<td>»</td>
<td><tt>"Sun<nbsp/>Mar<nbsp/><nbsp/>4<nbsp/>23:30:40<nbsp/>CST<nbsp/>2001\n"</tt></td>
</tr>
<tr>
<td><tt>`ls<nbsp/>testdir`.split[1]</tt></td>
<td>»</td>
<td><tt>"main.rb"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="abort" ref="abort">
<callseq>
abort
</callseq>
<desc>
<p/>
Terminate execution immediately, effectively by calling
<tt>Kernel.exit(1)</tt>.
<p/>
</desc>
</method>
<p/>
<method name="at_exit" ref="at_exit">
<callseq>
at_exit <block>{ <blockbody>block</blockbody> }</block>
<returns><obj>aProc</obj></returns>
</callseq>
<desc>
<p/>
Converts <em>block</em> to a <classname>Proc</classname> 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/>
<codefragment>
<alltt><fullcode><![CDATA[ def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
]]></fullcode>
def<nbsp/>do_at_exit(str1)
<nbsp/><nbsp/>at_exit<nbsp/>{<nbsp/>print<nbsp/>str1<nbsp/>}
end
at_exit<nbsp/>{<nbsp/>puts<nbsp/>"cruel<nbsp/>world"<nbsp/>}
do_at_exit("goodbye<nbsp/>")
exit
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
goodbye<nbsp/>cruel<nbsp/>world
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="autoload" ref="autoload">
<callseq>
autoload( <obj>aModule</obj>, <obj>aFile</obj> )
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Registers <obj>aFile</obj> to be loaded (using
<mmm><file>kernel</file><front>Kernel</front><back>require</back><mref>require</mref></mmm>) the first time that <obj>aModule</obj> (which
may be a <classname>String</classname> or a symbol) is accessed.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ autoload :MyModule, "/usr/local/lib/modules/my_module.rb"
]]></fullcode>
autoload<nbsp/>:MyModule,<nbsp/>"/usr/local/lib/modules/my_module.rb"
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="binding" ref="binding">
<callseq>
binding <returns><obj>aBinding</obj></returns>
</callseq>
<desc>
<p/>
Returns a <classname>Binding</classname> object, describing the variable and method
bindings at the point of call. This object can be used when calling
<meth>eval</meth> to execute the evaluated command in this
environment. Also see the description of <classname>Binding</classname>
beginning on page 295.
<p/>
<codefragment>
<fullcode><![CDATA[ def getBinding(param)
return binding
end
b = getBinding("hello")
eval "param", b
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>getBinding(param)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>return<nbsp/>binding</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>b<nbsp/>=<nbsp/>getBinding("hello")</tt></td>
</tr>
<tr>
<td><tt>eval<nbsp/>"param",<nbsp/>b</tt></td>
<td>»</td>
<td><tt>"hello"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="block_given?" ref="block_given_qm">
<callseq>
block_given? <returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
<p/>
Returns <const>true</const> if <kw>yield</kw> would execute a block
in the current context.
<p/>
<codefragment>
<fullcode><![CDATA[ def try
if block_given?
yield
else
"no block"
end
end
try
try { "hello" }
try do
"hello"
end
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>try</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>if<nbsp/>block_given?</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>yield</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>else</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/><nbsp/>"no<nbsp/>block"</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td><tt>try</tt></td>
<td>»</td>
<td><tt>"no<nbsp/>block"</tt></td>
</tr>
<tr>
<td><tt>try<nbsp/>{<nbsp/>"hello"<nbsp/>}</tt></td>
<td>»</td>
<td><tt>"hello"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>try<nbsp/>do</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>"hello"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="callcc" ref="callcc">
<callseq>
callcc <block>{| cont | <blockbody>block</blockbody> }</block>
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Generates a <classname>Continuation</classname> object, which it passes to the associated
block. Performing a <em>cont</em><tt>.call</tt> will cause the
<meth>callcc</meth> to return (as will falling through the end of
the block). The value returned by the <meth>callcc</meth> is the
value of the block, or the value passed to
<em>cont</em><tt>.call</tt>. See <classname>Continuation</classname>
on page 298 for more details. Also see
<mmm><file>kernel</file><front>Kernel</front><back>throw</back><mref>throw</mref></mmm> for an alternative mechanism for unwinding a
call stack.
<p/>
</desc>
</method>
<p/>
<method name="caller" ref="caller">
<callseq>
caller( <opt><obj>anInteger</obj></opt> )
<returns><obj>anArray</obj></returns>
</callseq>
<desc>
<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/>
<codefragment>
<fullcode><![CDATA[!- module Kernel
!- alias oldc caller
!- def caller(lev)
!- res = oldc(lev+1)
!- res.each {|l| l[0] = 'prog'; l.sub!(/\d+/) { ($&.to_i-7).to_s }}
!- end
!- end
def a(skip)
caller(skip)
end
def b(skip)
a(skip)
end
def c(skip)
b(skip)
end
c(0)
c(1)
c(2)
c(3)
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>a(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>caller(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>def<nbsp/>b(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>a(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>def<nbsp/>c(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>b(skip)</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td><tt>c(0)</tt></td>
<td>»</td>
<td><tt>["prog:2:in<nbsp/>`a'",<nbsp/>"prog:5:in<nbsp/>`b'",<nbsp/>"prog:8:in<nbsp/>`c'",<nbsp/>"prog:10"]</tt></td>
</tr>
<tr>
<td><tt>c(1)</tt></td>
<td>»</td>
<td><tt>["prog:5:in<nbsp/>`b'",<nbsp/>"prog:8:in<nbsp/>`c'",<nbsp/>"prog:11"]</tt></td>
</tr>
<tr>
<td><tt>c(2)</tt></td>
<td>»</td>
<td><tt>["prog:8:in<nbsp/>`c'",<nbsp/>"prog:12"]</tt></td>
</tr>
<tr>
<td><tt>c(3)</tt></td>
<td>»</td>
<td><tt>["prog:13"]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="catch" ref="catch">
<callseq>
catch( <em>symbol</em> ) <block>{| | <blockbody>block</blockbody> }</block>
<p/>
<returns>anObject</returns>
</callseq>
<desc>
<p/>
<meth>catch</meth> executes its block. If a <meth>throw</meth> is
executed, Ruby searches up its stack for a <meth>catch</meth> block with a tag
corresponding to the <meth>throw</meth>'s <em>symbol</em>. If found, that block is
terminated, and <meth>catch</meth> returns the value given to
<meth>throw</meth>. If <meth>throw</meth> is not called,
the block terminates normally, and
the value of <tt>catch</tt> is the value of the last expression
evaluated. <meth>catch</meth> expressions may be nested, and the
<meth>throw</meth> call need not be in lexical scope.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ def routine(n)
puts n
throw :done if n <= 0
routine(n-1)
end
catch(:done) { routine(3) }
]]></fullcode>
def<nbsp/>routine(n)
<nbsp/><nbsp/>puts<nbsp/>n
<nbsp/><nbsp/>throw<nbsp/>:done<nbsp/>if<nbsp/>n<nbsp/><=<nbsp/>0
<nbsp/><nbsp/>routine(n-1)
end
<p/>
catch(:done)<nbsp/>{<nbsp/>routine(3)<nbsp/>}
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
3
2
1
0
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="chomp" ref="chomp">
<callseq>
chomp( <opt><obj>aString</obj></opt> )
<returns>$_ or <em>aString</em></returns>
</callseq>
<desc>
Equivalent to
<tt>$_ = $_.chomp(<em>aString</em>)</tt>.
See <cim><file>string</file><front>String</front><back>chomp</back><mref>chomp</mref></cim> on page 372.
<p/>
<codefragment>
<fullcode><![CDATA[ $_ = "now\n"
chomp
$_
chomp "ow"
$_
chomp "xxx"
$_
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>"now\n"</tt></td>
</tr>
<tr>
<td><tt>chomp</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>chomp<nbsp/>"ow"</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
<tr>
<td><tt>chomp<nbsp/>"xxx"</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="chomp!" ref="chomp_oh">
<callseq>
chomp!( <opt><obj>aString</obj></opt> )
<returns>$_ or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$_.chomp!(<em>aString</em>)</tt>.
See <cim><file>string</file><front>String</front><back>chomp!</back><mref>chomp_oh</mref></cim>
<p/>
<codefragment>
<fullcode><![CDATA[ $_ = "now\n"
chomp!
$_
chomp! "x"
$_
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>"now\n"</tt></td>
</tr>
<tr>
<td><tt>chomp!</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>chomp!<nbsp/>"x"</tt></td>
<td>»</td>
<td><tt>nil</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="chop" ref="chop">
<callseq>
chop <returns><em>aString</em></returns>
</callseq>
<desc>
<p/>
Equivalent to
<tt>($_.dup).chop!</tt>, except <tt>nil</tt> is never
returned.
See <cim><file>string</file><front>String</front><back>chop!</back><mref>chop_oh</mref></cim> on page 372.
<p/>
<codefragment>
<fullcode><![CDATA[ a = "now\r\n"
$_ = a
chop
$_
chop
chop
chop
chop
a
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>a<nbsp/><nbsp/>=<nbsp/><nbsp/>"now\r\n"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>a</tt></td>
</tr>
<tr>
<td><tt>chop</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>chop</tt></td>
<td>»</td>
<td><tt>"no"</tt></td>
</tr>
<tr>
<td><tt>chop</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
<tr>
<td><tt>chop</tt></td>
<td>»</td>
<td><tt>""</tt></td>
</tr>
<tr>
<td><tt>chop</tt></td>
<td>»</td>
<td><tt>""</tt></td>
</tr>
<tr>
<td><tt>a</tt></td>
<td>»</td>
<td><tt>"now\r\n"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="chop!" ref="chop_oh">
<callseq>
chop! <returns>$_ or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$_.chop!</tt>.
<p/>
<codefragment>
<fullcode><![CDATA[ a = "now\r\n"
$_ = a
chop!
chop!
chop!
chop!
chop!
$_
a
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>a<nbsp/><nbsp/>=<nbsp/>"now\r\n"</tt></td>
</tr>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>a</tt></td>
</tr>
<tr>
<td><tt>chop!</tt></td>
<td>»</td>
<td><tt>"now"</tt></td>
</tr>
<tr>
<td><tt>chop!</tt></td>
<td>»</td>
<td><tt>"no"</tt></td>
</tr>
<tr>
<td><tt>chop!</tt></td>
<td>»</td>
<td><tt>"n"</tt></td>
</tr>
<tr>
<td><tt>chop!</tt></td>
<td>»</td>
<td><tt>""</tt></td>
</tr>
<tr>
<td><tt>chop!</tt></td>
<td>»</td>
<td><tt>nil</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>""</tt></td>
</tr>
<tr>
<td><tt>a</tt></td>
<td>»</td>
<td><tt>""</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="eval" ref="eval">
<callseq>
eval( <em>aString</em>
<opt>, <em>aBinding</em> <opt><em>file</em> <opt><em>line</em></opt></opt></opt>)
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<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 <classname>Binding</classname> object or a <classname>Proc</classname>
object. If the optional <em>file</em> and <em>line</em> parameters
are present, they will be used when reporting syntax errors.
<p/>
<codefragment>
<fullcode><![CDATA[ def getBinding(str)
return binding
end
str = "hello"
eval "str + ' Fred'"
eval "str + ' Fred'", getBinding("bye")
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>def<nbsp/>getBinding(str)</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/>return<nbsp/>binding</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td colspan="3"><tt>str<nbsp/>=<nbsp/>"hello"</tt></td>
</tr>
<tr>
<td><tt>eval<nbsp/>"str<nbsp/>+<nbsp/>'<nbsp/>Fred'"</tt></td>
<td>»</td>
<td><tt>"hello<nbsp/>Fred"</tt></td>
</tr>
<tr>
<td><tt>eval<nbsp/>"str<nbsp/>+<nbsp/>'<nbsp/>Fred'",<nbsp/>getBinding("bye")</tt></td>
<td>»</td>
<td><tt>"bye<nbsp/>Fred"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="exec" ref="exec">
<callseq>
exec( <em>command</em> <opt>, <em>args</em></opt>)
</callseq>
<desc>
<p/>
Replaces the current process by running the given external
command.
If <meth>exec</meth> 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
<tt>argv[0]</tt> value, which may show up in process listings. In MSDOS
environments, the command is executed in a subshell; otherwise,
one of the <tt>exec(2)</tt> system calls is used, so the running
command may inherit some of the environment of the original
program (including open file descriptors).
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ exec "echo *" # echoes list of files in current directory
# never get here
exec "echo", "*" # echoes an asterisk
# never get here
]]></fullcode>
exec<nbsp/>"echo<nbsp/>*"<nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>echoes<nbsp/>list<nbsp/>of<nbsp/>files<nbsp/>in<nbsp/>current<nbsp/>directory
#<nbsp/>never<nbsp/>get<nbsp/>here
<p/>
exec<nbsp/>"echo",<nbsp/>"*"<nbsp/><nbsp/><nbsp/><nbsp/>#<nbsp/>echoes<nbsp/>an<nbsp/>asterisk
#<nbsp/>never<nbsp/>get<nbsp/>here
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="exit" ref="exit">
<callseq>
exit( <obj>anInteger</obj>=0 )
</callseq>
<desc>
<p/>
Initiates the termination of the Ruby script by raising the
<exception>SystemExit</exception> exception. This exception may be caught. The
optional parameter is used to return a status code to the
invoking environment.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ begin
exit
puts "never get here"
rescue SystemExit
puts "rescued a SystemExit exception"
end
puts "after begin block"
]]></fullcode>
begin
<nbsp/><nbsp/>exit
<nbsp/><nbsp/>puts<nbsp/>"never<nbsp/>get<nbsp/>here"
rescue<nbsp/>SystemExit
<nbsp/><nbsp/>puts<nbsp/>"rescued<nbsp/>a<nbsp/>SystemExit<nbsp/>exception"
end
puts<nbsp/>"after<nbsp/>begin<nbsp/>block"
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
rescued<nbsp/>a<nbsp/>SystemExit<nbsp/>exception
after<nbsp/>begin<nbsp/>block
</alltt>
</codefragment>
<p/>
Just prior to termination, Ruby executes
any <tt>at_exit</tt> functions and runs any object finalizers (see
<modulename>ObjectSpace</modulename> beginning on page 434).
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ at_exit { puts "at_exit function" }
ObjectSpace.define_finalizer(self, proc { puts "in finalizer" })
exit
]]></fullcode>
at_exit<nbsp/>{<nbsp/>puts<nbsp/>"at_exit<nbsp/>function"<nbsp/>}
ObjectSpace.define_finalizer(self,<nbsp/><nbsp/>proc<nbsp/>{<nbsp/>puts<nbsp/>"in<nbsp/>finalizer"<nbsp/>})
exit
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
at_exit<nbsp/>function
0x4019ac90
n<nbsp/>finals=>0
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="exit!" ref="exit_oh">
<callseq>
exit!( <obj>anInteger</obj>=-1 )
</callseq>
<desc>
<p/>
Similar to <mmm><file>kernel</file><front>Kernel</front><back>exit</back><mref>exit</mref></mmm>, but exception handling,
<meth>at_exit</meth> functions, and finalizers are bypassed.
<p/>
</desc>
</method>
<p/>
<method name="fail" ref="fail">
<callseq>
fail<br/>fail( <obj>aString</obj> )<br/>fail( <obj>anException</obj> <opt>, <obj>aString</obj>
<opt><obj>anArray</obj></opt></opt> )
</callseq>
<desc>
<p/>
Synonym for <mmm><file>kernel</file><front>Kernel</front><back>raise</back><mref>raise</mref></mmm>.
<p/>
</desc>
</method>
<p/>
<method name="fork" ref="fork">
<callseq>
fork <opt><block>{ <blockbody>block</blockbody> }</block>
</opt> <returns><obj>aFixnum</obj>
or <tt>nil</tt></returns>
</callseq>
<desc>
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 <meth>fork</meth> call returns twice, once in
the parent, returning the process id of the child, and once in
the child, returning <tt>nil</tt>. The child process can
exit using <mmm><file>kernel</file><front>Kernel</front><back>exit!</back><mref>exit_oh</mref></mmm> to avoid running any
<meth>at_exit</meth> functions. The parent process should use
<mmm><file>process</file><front>Process</front><back>wait</back><mref>wait</mref></mmm> to collect the termination statuses of its
children; otherwise, the operating system may accumulate zombie
processes.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[!- $stdout.sync = true
fork do
!- 3.times {|i| puts "Child: #{i}\n"; sleep .05 }
# 3.times {|i| puts "Child: #{i}" }
end
!- 3.times {|i| puts "Parent: #{i}\n"; sleep .05 }
# 3.times {|i| puts "Parent: #{i}" }
Process.wait
]]></fullcode>
fork<nbsp/>do
<nbsp/><nbsp/>3.times<nbsp/>{|i|<nbsp/>puts<nbsp/>"Child:<nbsp/>#{i}"<nbsp/>}
end
3.times<nbsp/>{|i|<nbsp/>puts<nbsp/>"Parent:<nbsp/>#{i}"<nbsp/>}
Process.wait
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Parent:<nbsp/>0
Child:<nbsp/>0
Child:<nbsp/>1
Parent:<nbsp/>1
Parent:<nbsp/>2
Child:<nbsp/>2
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="format" ref="format">
<callseq>
format( <obj>aString</obj> <optz>,
<obj>anObject</obj></optz> ) <returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <mmm><file>kernel</file><front>Kernel</front><back>sprintf</back><mref>sprintf</mref></mmm>.
<p/>
</desc>
</method>
<p/>
<method name="gets" ref="gets">
<callseq>
gets( <obj>aString</obj>=<var>$/</var> )
<returns><obj>aString</obj> or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Returns (and assigns to <var>$_</var>) the next line from
the list of files in <var>ARGV</var> (or <var>$*</var>), or
from standard input if no files are present on the command line.
Returns <tt>nil</tt> at end of file.
The optional argument specifies the
record separator. The separator is
included with the contents of each record. A separator of <tt>nil</tt>
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 <var>ARGV</var>, <meth>gets(nil)</meth> will read the
contents one file at a time.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ ARGV << "testfile"
print while gets
]]></fullcode>
ARGV<nbsp/><<<nbsp/>"testfile"
print<nbsp/>while<nbsp/>gets
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
This<nbsp/>is<nbsp/>line<nbsp/>one
This<nbsp/>is<nbsp/>line<nbsp/>two
This<nbsp/>is<nbsp/>line<nbsp/>three
And<nbsp/>so<nbsp/>on...
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="global_variables" ref="global_variables">
<callseq>
global_variables
<returns><obj>anArray</obj></returns>
</callseq>
<desc>
<p/>
Returns an array of the names of global variables.
<p/>
<codefragment>
<fullcode><![CDATA[ global_variables.grep /std/
]]></fullcode><rubycode>
<tr>
<td><tt>global_variables.grep<nbsp/>/std/</tt></td>
<td>»</td>
<td><tt>["$stderr",<nbsp/>"$stdout",<nbsp/>"$stdin"]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="gsub" ref="gsub">
<callseq>
gsub( <obj>pattern</obj>, <obj>replacement</obj> )
<returns><obj>aString</obj></returns><br/>gsub( <obj>pattern</obj> ) <block>{| | <blockbody>block</blockbody> }</block>
<p/>
<returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$_.gsub...</tt>, except that <var>$_</var>
receives the modified result.
<p/>
<codefragment>
<fullcode><![CDATA[ $_ = "quick brown fox"
gsub /[aeiou]/, '*'
$_
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>"quick<nbsp/>brown<nbsp/>fox"</tt></td>
</tr>
<tr>
<td><tt>gsub<nbsp/>/[aeiou]/,<nbsp/>'*'</tt></td>
<td>»</td>
<td><tt>"q**ck<nbsp/>br*wn<nbsp/>f*x"</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"q**ck<nbsp/>br*wn<nbsp/>f*x"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="gsub!" ref="gsub_oh">
<callseq>
gsub!( <obj>pattern</obj>, <obj>replacement</obj> )
<returns><obj>aString</obj> or <tt>nil</tt></returns><br/>gsub!( <obj>pattern</obj> ) <block>{| | <blockbody>block</blockbody> }</block>
<p/>
<returns><obj>aString</obj> or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Equivalent to <mmm><file>kernel</file><front>Kernel</front><back>gsub</back><mref>gsub</mref></mmm>, except <tt>nil</tt> is returned if
<var>$_</var> is not modified.
<p/>
<codefragment>
<fullcode><![CDATA[ $_ = "quick brown fox"
gsub! /cat/, '*'
$_
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>$_<nbsp/>=<nbsp/>"quick<nbsp/>brown<nbsp/>fox"</tt></td>
</tr>
<tr>
<td><tt>gsub!<nbsp/>/cat/,<nbsp/>'*'</tt></td>
<td>»</td>
<td><tt>nil</tt></td>
</tr>
<tr>
<td><tt>$_</tt></td>
<td>»</td>
<td><tt>"quick<nbsp/>brown<nbsp/>fox"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="iterator?" ref="iterator_qm">
<callseq>
iterator? <returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
<p/>
Synonym for <mmm><file>kernel</file><front>Kernel</front><back>block_given?</back><mref>block_given_qm</mref></mmm>. The <meth>iterator?</meth>
method will be removed in Ruby 1.8.
<p/>
</desc>
</method>
<p/>
<method name="lambda" ref="lambda">
<callseq>
lambda <block>{| | <blockbody>block</blockbody> }</block>
<returns><obj>aProc</obj></returns>
</callseq>
<desc>
<p/>
Synonym for <mmm><file>kernel</file><front>Kernel</front><back>proc</back><mref>proc</mref></mmm>.
<p/>
</desc>
</method>
<p/>
<method name="load" ref="load">
<callseq>
load( <obj>aFileName</obj>,
<obj>wrap</obj>=<const>false</const> ) <returns><const>true</const></returns>
</callseq>
<desc>
<p/>
Loads and executes the Ruby program in the file
<obj>aFileName</obj>. If the filename does not resolve to an absolute path, the
file is searched for in the library directories listed in
<var>$:</var>. If the optional <obj>wrap</obj> parameter is
<const>true</const>, 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/>
</desc>
</method>
<p/>
<method name="local_variables" ref="local_variables">
<callseq>
local_variables
<returns><obj>anArray</obj></returns>
</callseq>
<desc>
<p/>
Returns the names of the current local variables.
<p/>
<codefragment>
<fullcode><![CDATA[ fred = 1
for i in 1..10
# ...
end
local_variables
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>fred<nbsp/>=<nbsp/>1</tt></td>
</tr>
<tr>
<td colspan="3"><tt>for<nbsp/>i<nbsp/>in<nbsp/>1..10</tt></td>
</tr>
<tr>
<td colspan="3"><tt><nbsp/><nbsp/><nbsp/>#<nbsp/>...</tt></td>
</tr>
<tr>
<td colspan="3"><tt>end</tt></td>
</tr>
<tr>
<td><tt>local_variables</tt></td>
<td>»</td>
<td><tt>["fred",<nbsp/>"i"]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="loop" ref="loop">
<callseq>
loop <block>{| | <blockbody>block</blockbody> }</block>
<p/>
</callseq>
<desc>
<p/>
Repeatedly executes the block.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ loop {
print "Input: "
break if !gets or $_ =~ /^[qQ]/
# ...
}
]]></fullcode>
loop<nbsp/>{
<nbsp/><nbsp/>print<nbsp/>"Input:<nbsp/>"
<nbsp/><nbsp/>break<nbsp/>if<nbsp/>!gets<nbsp/>or<nbsp/>$_<nbsp/>=~<nbsp/>/^qQ/
<nbsp/><nbsp/>#<nbsp/>...
}
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="open" ref="open">
<callseq>
open( <obj>aString</obj>
<opt>, <obj>aMode</obj> <opt><obj>perm</obj></opt></opt> )
<returns><obj>anIO</obj> or <tt>nil</tt></returns><br/>open( <obj>aString</obj>
<opt>, <obj>aMode</obj> <opt><obj>perm</obj></opt></opt> ) <block>{| anIO | <blockbody>block</blockbody> }</block>
<p/>
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Creates an <classname>IO</classname> object connected to the given stream, file,
or subprocess.
<p/>
If <obj>aString</obj> does not start with a pipe character
(``<tt>|</tt>''), treat it as the name of a file to open using the
specified mode defaulting to ``<tt>r</tt>'' (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/>
If a block is specified, it will be invoked with the <classname>File</classname>
object as a parameter, and the file will be automatically
closed when the block terminates. The call always returns
<tt>nil</tt> in this case.
<p/>
If <obj>aString</obj> starts with a pipe character, a subprocess
is created, connected to the caller by a pair of pipes. The
returned <classname>IO</classname> object may be used to write to the standard
input and read from the standard output of this subprocess.
If the command following the ``<tt>|</tt>'' is a single minus sign,
Ruby forks, and this subprocess is connected to the parent.
In the subprocess, the <meth>open</meth> call returns <tt>nil</tt>. If
the command is not ``<tt>-</tt>'', the subprocess runs the command. If
a block is associated with an <tt>open("|-")</tt> call, that
block will be run twice---once in the parent and once in the
child. The block parameter will be an <classname>IO</classname> object in the
parent and <tt>nil</tt> in the child. The parent's <classname>IO</classname> object will
be connected to the child's <tt>$stdin</tt> and <tt>$stdout</tt>.
The subprocess will be terminated at the end of the
block.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ open("testfile") do |f|
print f.gets
end
]]></fullcode>
open("testfile")<nbsp/>do<nbsp/>|f|
<nbsp/><nbsp/>print<nbsp/>f.gets
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
This<nbsp/>is<nbsp/>line<nbsp/>one
</alltt>
</codefragment>
<p/>
Open a subprocess and read its output:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ cmd = open("|date")
print cmd.gets
cmd.close
]]></fullcode>
cmd<nbsp/>=<nbsp/>open("|date")
print<nbsp/>cmd.gets
cmd.close
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Sun<nbsp/>Mar<nbsp/><nbsp/>4<nbsp/>23:30:41<nbsp/>CST<nbsp/>2001
</alltt>
</codefragment>
<p/>
Open a subprocess running the same Ruby program:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ f = open("|-", "w+")
if f == nil
puts "in Child"
exit
else
puts "Got: #{f.gets}"
end
]]></fullcode>
f<nbsp/>=<nbsp/>open("|-",<nbsp/>"w+")
if<nbsp/>f<nbsp/>==<nbsp/>nil
<nbsp/><nbsp/>puts<nbsp/>"in<nbsp/>Child"
<nbsp/><nbsp/>exit
else
<nbsp/><nbsp/>puts<nbsp/>"Got:<nbsp/>#{f.gets}"
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Got:<nbsp/>in<nbsp/>Child
</alltt>
</codefragment>
<p/>
Open a subprocess using a block to receive the I/O object:
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ open("|-") do |f|
if f == nil
puts "in Child"
else
puts "Got: #{f.gets}"
end
end
]]></fullcode>
open("|-")<nbsp/>do<nbsp/>|f|
<nbsp/><nbsp/>if<nbsp/>f<nbsp/>==<nbsp/>nil
<nbsp/><nbsp/><nbsp/><nbsp/>puts<nbsp/>"in<nbsp/>Child"
<nbsp/><nbsp/>else
<nbsp/><nbsp/><nbsp/><nbsp/>puts<nbsp/>"Got:<nbsp/>#{f.gets}"
<nbsp/><nbsp/>end
end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Got:<nbsp/>in<nbsp/>Child
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="p" ref="p">
<callseq>
p( <optn><obj>anObject</obj></optn> )
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
For each object, directly writes <tt><obj>anObject</obj>.inspect</tt> followed by
the current output record separator to the program's standard
output. <meth>p</meth> bypasses the Ruby I/O libraries.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ p self
]]></fullcode>
p<nbsp/>self
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
main
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="print" ref="print">
<callseq>
print( <optz><obj>anObject</obj></optz> )
<returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Prints each object in turn to <var>$defout</var>. If the
output field separator (<var>$,</var>)
is not <tt>nil</tt>, its contents will appear
between each field.
If the output record separator
(<var>$\</var>)
is not <tt>nil</tt>, it will be appended to the output. If
no arguments are given, prints <var>$_</var>. Objects that aren't
strings will be converted by calling their <meth>to_s</meth>
method.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99
]]></fullcode>
print<nbsp/>"cat",<nbsp/>[1,2,3],<nbsp/>99,<nbsp/>"\n"
$,<nbsp/>=<nbsp/>",<nbsp/>"
$\<nbsp/>=<nbsp/>"\n"
print<nbsp/>"cat",<nbsp/>[1,2,3],<nbsp/>99
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
cat12399
cat,<nbsp/>1,<nbsp/>2,<nbsp/>3,<nbsp/>99
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="printf" ref="printf">
<callseq>
printf( <obj>anIO</obj>, <obj>aString</obj> <optz>, <obj>anObject</obj></optz> )
<returns><tt>nil</tt></returns><br/>printf( <obj>aString</obj> <optz>, <obj>anObject</obj></optz> )
<returns><tt>nil</tt></returns>
</callseq>
<desc>
Equivalent to:
<p/>
<obj>anIO.</obj><tt>write sprintf(</tt><obj>aString,<nbsp/>anObject</obj><tt> ...)</tt><br/><em>or</em><br/><tt>$defout.write sprintf(</tt><obj>aString,<nbsp/>anObject</obj><tt> ...)</tt>
<p/>
</desc>
</method>
<p/>
<method name="proc" ref="proc">
<callseq>
proc <block>{ <blockbody>block</blockbody> }</block>
<returns><obj>aProc</obj></returns>
</callseq>
<desc>
<p/>
Creates a new procedure object from the given block. Equivalent
to <ccm><file>proc</file><front>Proc</front><back>new</back><mref>new</mref></ccm>.
<p/>
<codefragment>
<fullcode><![CDATA[ aProc = proc { "hello" }
aProc.call
]]></fullcode><rubycode>
<tr>
<td colspan="3"><tt>aProc<nbsp/>=<nbsp/>proc<nbsp/>{<nbsp/>"hello"<nbsp/>}</tt></td>
</tr>
<tr>
<td><tt>aProc.call</tt></td>
<td>»</td>
<td><tt>"hello"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="putc" ref="putc">
<callseq>
putc( <obj>anInteger</obj> ) <returns><obj>anInteger</obj></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$defout.putc(</tt><obj>anInteger</obj><tt>)</tt>.
<p/>
</desc>
</method>
<p/>
<method name="puts" ref="puts">
<callseq>
puts( <optz><obj>args</obj></optz> ) <returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$defout.puts(</tt><obj>args</obj><tt>)</tt>.
<p/>
</desc>
</method>
<p/>
<method name="raise" ref="raise">
<callseq>
raise<br/>raise( <obj>aString</obj> )<br/>raise( <obj>anException</obj> <opt>, <obj>aString</obj>
<opt><obj>anArray</obj></opt></opt> )
</callseq>
<desc>
<p/>
With no arguments, raises the
exception in <var>$!</var> or raises a
<exception>RuntimeError</exception> if <var>$!</var> is <tt>nil</tt>.
With a single <classname>String</classname> argument, raises a
<exception>RuntimeError</exception> with the string as a message. Otherwise, the
first parameter should be the name of an <exception>Exception</exception> class
(or an object that returns an <exception>Exception</exception> when sent
<meth>exception</meth>). 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 <tt>rescue</tt> clause of
<tt>begin...end</tt> blocks.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ raise "Failed to create socket"
raise ArgumentError, "No parameters", caller
]]></fullcode>
raise<nbsp/>"Failed<nbsp/>to<nbsp/>create<nbsp/>socket"
raise<nbsp/>ArgumentError,<nbsp/>"No<nbsp/>parameters",<nbsp/>caller
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="rand" ref="rand">
<callseq>
rand( <obj>max</obj>=0 )
<returns><obj>aNumber</obj></returns>
</callseq>
<desc>
Converts <obj>max</obj> to an integer using max<sub>1</sub> =
max<tt>.to_i.abs</tt>.
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>. <mmm><file>kernel</file><front>Kernel</front><back>srand</back><mref>srand</mref></mmm> may be
used to ensure repeatable sequences of random numbers between
different runs of the program.
<p/>
<codefragment>
<fullcode><![CDATA[ srand 1234
[ rand, rand ]
[ rand(10), rand(1000) ]
srand 1234
[ rand, rand ]
]]></fullcode><rubycode>
<tr>
<td><tt>srand<nbsp/>1234</tt></td>
<td>»</td>
<td><tt>0</tt></td>
</tr>
<tr>
<td><tt>[<nbsp/>rand,<nbsp/><nbsp/>rand<nbsp/>]</tt></td>
<td>»</td>
<td><tt>[0.7408769294,<nbsp/>0.2145348572]</tt></td>
</tr>
<tr>
<td><tt>[<nbsp/>rand(10),<nbsp/>rand(1000)<nbsp/>]</tt></td>
<td>»</td>
<td><tt>[3,<nbsp/>323]</tt></td>
</tr>
<tr>
<td><tt>srand<nbsp/>1234</tt></td>
<td>»</td>
<td><tt>1234</tt></td>
</tr>
<tr>
<td><tt>[<nbsp/>rand,<nbsp/><nbsp/>rand<nbsp/>]</tt></td>
<td>»</td>
<td><tt>[0.7408769294,<nbsp/>0.2145348572]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="readline" ref="readline">
<callseq>
readline( <opt><obj>aString</obj>=<var>$/</var></opt> )
<returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Equivalent to <mmm><file>kernel</file><front>Kernel</front><back>gets</back><mref>gets</mref></mmm>, except <meth>readline</meth> raises
<exception>EOFError</exception> at end of file.
<p/>
</desc>
</method>
<p/>
<method name="readlines" ref="readlines">
<callseq>
readlines( <opt><obj>aString</obj>=<var>$/</var></opt> )
<returns><obj>anArray</obj></returns>
</callseq>
<desc>
<p/>
Returns an array containing the lines returned by
calling <tt>Kernel.gets(<obj>aString</obj>)</tt> until the end of file.
<p/>
</desc>
</method>
<p/>
<method name="require" ref="require">
<callseq>
require( <obj>aString</obj> )
<returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
Ruby tries to load the library named <obj>aString</obj>, returning
<const>true</const> if successful. If the filename does not resolve to an
absolute path, it will be searched for in the directories listed
in <var>$:</var>. If the file has the extension
``.rb'', it is loaded as a source file; if the extension is
``.so'', ``.o'', or ``.dll'',<footnote>Or whatever the default
shared library extension is on the current platform.</footnote> 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 <var>$"</var>. A
feature will not be loaded if it already appears in <var>$"</var>.
<meth>require</meth> returns <const>true</const> if the feature was
successfully loaded.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ require "my-library.rb"
require "db-driver"
]]></fullcode>
require<nbsp/>"my-library.rb"
require<nbsp/>"db-driver"
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="scan" ref="scan">
<callseq>
scan( <obj>pattern</obj> ) <returns><obj>anArray</obj></returns><br/>scan( <obj>pattern</obj> ) <block>{| | <blockbody>block</blockbody> }</block>
<returns>$_</returns>
</callseq>
<desc>
<p/>
Equivalent to calling <tt>$_.scan</tt>. See <cim><file>string</file><front>String</front><back>scan</back><mref>scan</mref></cim>
on page 378.
<p/>
</desc>
</method>
<p/>
<method name="select" ref="select">
<callseq>
select( <obj>readArray</obj> <opt>,
<obj>writeArray</obj>
<opt><obj>errorArray</obj> <opt><obj>timeout</obj></opt></opt></opt> )
<returns><obj>anArray</obj> or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Performs
a low-level <tt>select</tt> call, which waits for data to
become available from input/output devices. The first three
parameters are arrays of <classname>IO</classname> objects or <tt>nil</tt>. The last is a
timeout in seconds, which should be an <classname>Integer</classname> or a
<classname>Float</classname>. The call waits for data to become available for any
of the <classname>IO</classname> objects in <obj>readArray</obj>, for buffers to have
cleared sufficiently to enable writing to any of the devices in
<obj>writeArray</obj>, or for an error to occur on the devices in
<obj>errorArray</obj>. If one or more of these conditions are met, the
call returns a three-element array containing arrays of the
<classname>IO</classname> objects that were ready. Otherwise, if there is no
change in status for <obj>timeout</obj> seconds, the call returns
<tt>nil</tt>. If all parameters are <tt>nil</tt>, the current thread sleeps forever.
<p/>
<codefragment>
<fullcode><![CDATA[ select( [$stdin], nil, nil, 1.5 )
]]></fullcode><rubycode>
<tr>
<td><tt>select(<nbsp/>[$stdin],<nbsp/>nil,<nbsp/>nil,<nbsp/>1.5<nbsp/>)</tt></td>
<td>»</td>
<td><tt>[[#<IO:0x4019202c>],<nbsp/>[],<nbsp/>[]]</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="set_trace_func" ref="set_trace_func">
<callseq>
set_trace_func( <obj>aProc</obj> ) <returns><obj>aProc</obj></returns><br/>set_trace_func( <tt>nil</tt> ) <returns><tt>nil</tt></returns>
</callseq>
<desc>
Establishes <obj>aProc</obj> as the handler for tracing, or disables
tracing if the parameter is <tt>nil</tt>. <obj>aProc</obj>
takes up to six parameters: an event name, a filename, a line
number, an object id, a binding, and the name of a
class. <obj>aProc</obj> is invoked whenever an event
occurs. Events are:
<tt>c-call</tt> (call a C-language routine),
<tt>c-return</tt> (return from a C-language routine),
<tt>call</tt> (call a Ruby method),
<tt>class</tt> (start a class or module definition),
<tt>end</tt> (finish a class or module definition),
<tt>line</tt> (execute code on a new line),
<tt>raise</tt> (raise an exception), and
<tt>return</tt> (return from a Ruby method).
Tracing is disabled within the context of <obj>aProc</obj>.
<p/>
See the example starting on page 271 for more
information.
<p/>
</desc>
</method>
<p/>
<method name="singleton_method_added" ref="singleton_method_added">
<callseq>
singleton_method_added( <obj>aFixnum</obj> ) <returns><tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Invoked with a symbol id whenever a singleton method is added to
a module or a class. The default implementation in <modulename>Kernel</modulename>
ignores this, but subclasses may override the method to provide
specialized functionality.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ 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
]]></fullcode>
class<nbsp/>Test
<nbsp/><nbsp/>def<nbsp/>Test.singleton_method_added(id)
<nbsp/><nbsp/><nbsp/><nbsp/>puts<nbsp/>"Added<nbsp/>#{id.id2name}<nbsp/>to<nbsp/>Test"
<nbsp/><nbsp/>end
<nbsp/><nbsp/>def<nbsp/>a()<nbsp/>end
<nbsp/><nbsp/>def<nbsp/>Test.b()<nbsp/>end
end
def<nbsp/>Test.c()<nbsp/>end
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Added<nbsp/>singleton_method_added<nbsp/>to<nbsp/>Test
Added<nbsp/>b<nbsp/>to<nbsp/>Test
Added<nbsp/>c<nbsp/>to<nbsp/>Test
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="sleep" ref="sleep">
<callseq>
sleep( <opt><obj>aNumeric</obj></opt> )
<returns><obj>aFixnum</obj></returns>
</callseq>
<desc>
<p/>
Suspends the current thread for <obj>aNumber</obj> seconds (which may be a <classname>Float</classname>
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
<tt>SIGALRM</tt>, or if another thread
calls <cim><file>thread</file><front>Thread</front><back>run</back><mref>run</mref></cim>. An argument of zero causes <meth>sleep</meth>
to sleep forever.
<p/>
<codefragment>
<fullcode><![CDATA[ Time.new
sleep 1.2
Time.new
sleep 1.9
Time.new
]]></fullcode><rubycode>
<tr>
<td><tt>Time.new</tt></td>
<td>»</td>
<td><tt>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:30:41<nbsp/>CST<nbsp/>2001</tt></td>
</tr>
<tr>
<td><tt>sleep<nbsp/>1.2</tt></td>
<td>»</td>
<td><tt>2</tt></td>
</tr>
<tr>
<td><tt>Time.new</tt></td>
<td>»</td>
<td><tt>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:30:43<nbsp/>CST<nbsp/>2001</tt></td>
</tr>
<tr>
<td><tt>sleep<nbsp/>1.9</tt></td>
<td>»</td>
<td><tt>2</tt></td>
</tr>
<tr>
<td><tt>Time.new</tt></td>
<td>»</td>
<td><tt>Sun<nbsp/>Mar<nbsp/>04<nbsp/>23:30:45<nbsp/>CST<nbsp/>2001</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="split" ref="split">
<callseq>
split( <opt><obj>pattern</obj>
<opt><obj>limit</obj></opt></opt> ) <returns><obj>anArray</obj></returns>
</callseq>
<desc>
<p/>
Equivalent to
<tt>$_.split(<obj>pattern</obj>, <obj>limit</obj>)</tt>. See <cim><file>string</file><front>String</front><back>split</back><mref>split</mref></cim>
on page 379.
<p/>
</desc>
</method>
<p/>
<method name="sprintf" ref="sprintf">
<callseq>
sprintf( <obj>aFormatString</obj>
<optz>, <obj>arguments</obj></optz> ) <returns><obj>aString</obj></returns>
</callseq>
<desc>
<p/>
Returns the string resulting
from applying <nbsp/><obj>aFormatString</obj> 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 <meth>sprintf</meth> 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/>
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 <tt>%10.10s</tt> will always
contribute exactly ten characters to the result.)
<p/>
<figure type="table">
<caption><meth>sprintf</meth> flag characters</caption>
<table>
<th>
<td><b>Flag</b></td>
<td><b>Applies to</b></td>
<td><b>Meaning</b></td>
</th>
<tr>
<td><tt><visible_space/></tt> (space)</td>
<td>bdeEfgGioxXu</td>
<td>Leave a
space at the start of positive numbers.</td>
</tr>
<tr>
<td><tt>#</tt></td>
<td>beEfgGoxX</td>
<td>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><tt>+</tt></td>
<td>bdeEfgGioxXu</td>
<td>Add a leading plus sign to
positive numbers.</td>
</tr>
<tr>
<td><tt>-</tt></td>
<td>all</td>
<td>Left-justify the result of this conversion.</td>
</tr>
<tr>
<td><tt>0</tt> (zero)</td>
<td>all</td>
<td>Pad with zeros, not spaces.</td>
</tr>
<tr>
<td><tt>*</tt></td>
<td>all</td>
<td>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>
<bottomrule/></table>
<p/>
</figure>
<p/>
<figure type="table">
<caption><meth>sprintf</meth> field types</caption>
<table>
<th>
<td><b>Field</b></td>
<td><b>Conversion</b></td>
</th>
<tr>
<td>b</td>
<td>Convert argument as a binary number.</td>
</tr>
<tr>
<td>c</td>
<td>Argument is the numeric code for a single character.</td>
</tr>
<tr>
<td>d</td>
<td>Convert argument as a decimal number.</td>
</tr>
<tr>
<td>E</td>
<td>Equivalent to `e', but uses an uppercase E to indicate
the exponent.</td>
</tr>
<tr>
<td>e</td>
<td>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>f</td>
<td>Convert floating point argument as <tt>[<visible_space/>-]ddd.ddd</tt>,
where the precision determines the number of digits after
the decimal point.</td>
</tr>
<tr>
<td>G</td>
<td>Equivalent to `g', but use an uppercase `E' in exponent
form.</td>
</tr>
<tr>
<td>g</td>
<td>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 <tt>d.dddd</tt> form otherwise.</td>
</tr>
<tr>
<td>i</td>
<td>Identical to `d'.</td>
</tr>
<tr>
<td>o</td>
<td>Convert argument as an octal number.</td>
</tr>
<tr>
<td>s</td>
<td>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>u</td>
<td>Treat argument as an unsigned decimal number.</td>
</tr>
<tr>
<td>X</td>
<td>Convert argument as a hexadecimal number using uppercase letters.</td>
</tr>
<tr>
<td>x</td>
<td>Convert argument as a hexadecimal number.</td>
</tr>
<bottomrule/></table>
<p/>
</figure>
<p/>
<codefragment>
<fullcode><![CDATA[ sprintf("%d %04x", 123, 123)
sprintf("%08b '%4s'", 123, 123)
sprintf("%*2$s %d", "hello", 10)
sprintf("%*2$s %d", "hello", -10)
sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23)
]]></fullcode><rubycode>
<tr>
<td><tt>sprintf("%d<nbsp/>%04x",<nbsp/>123,<nbsp/>123)</tt></td>
<td>»</td>
<td><tt>"123<visible_space/>007b"</tt></td>
</tr>
<tr>
<td><tt>sprintf("%08b<nbsp/>'%4s'",<nbsp/>123,<nbsp/>123)</tt></td>
<td>»</td>
<td><tt>"01111011<visible_space/>'<visible_space/>123'"</tt></td>
</tr>
<tr>
<td><tt>sprintf("%*2$s<nbsp/>%d",<nbsp/>"hello",<nbsp/>10)</tt></td>
<td>»</td>
<td><tt>"<visible_space/><visible_space/><visible_space/><visible_space/><visible_space/>hello<visible_space/>10"</tt></td>
</tr>
<tr>
<td><tt>sprintf("%*2$s<nbsp/>%d",<nbsp/>"hello",<nbsp/>-10)</tt></td>
<td>»</td>
<td><tt>"hello<visible_space/><visible_space/><visible_space/><visible_space/><visible_space/><visible_space/>-10"</tt></td>
</tr>
<tr>
<td><tt>sprintf("%+g:%<nbsp/>g:%-g",<nbsp/>1.23,<nbsp/>1.23,<nbsp/>1.23)</tt></td>
<td>»</td>
<td><tt>"+1.23:<visible_space/>1.23:1.23"</tt></td>
</tr>
</rubycode>
<p/>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="srand" ref="srand">
<callseq>
srand( <opt><obj>aNumber</obj></opt> )
<returns><obj>oldSeed</obj></returns>
</callseq>
<desc>
<p/>
Seeds the pseudorandom number generator to the value of
<obj>aNumber</obj>.<meth>to_i.abs</meth>. If <obj>aNumber</obj> 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
<mmm><file>kernel</file><front>Kernel</front><back>rand</back><mref>rand</mref></mmm> is called without previously calling
<meth>srand</meth>, 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 <mmm><file>kernel</file><front>Kernel</front><back>rand</back><mref>rand</mref></mmm> on page 425.
<p/>
</desc>
</method>
<p/>
<method name="sub" ref="sub">
<callseq>
sub( <obj>pattern</obj>, <obj>replacement</obj> )
<returns>$_</returns><br/>sub( <obj>pattern</obj> ) { <obj>block</obj> }
<returns>$_</returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$_.sub(<obj>args</obj>)</tt>, except that <var>$_</var>
will be updated if substitution occurs.
<p/>
</desc>
</method>
<p/>
<method name="sub!" ref="sub_oh">
<callseq>
sub!( <obj>pattern</obj>, <obj>replacement</obj> )
<returns>$_ or <tt>nil</tt></returns><br/>sub!( <obj>pattern</obj> ) { <obj>block</obj> }
<returns>$_ or <tt>nil</tt></returns>
</callseq>
<desc>
<p/>
Equivalent to <tt>$_.sub!(<obj>args</obj>)</tt>.
<p/>
</desc>
</method>
<p/>
<method name="syscall" ref="syscall">
<callseq>
syscall( <obj>aFixnum</obj>
<optz>, <obj>args</obj></optz> )
<returns><obj>anInteger</obj></returns>
</callseq>
<desc>
<p/>
Calls the operating system function identified by <obj>aFixnum</obj>,
passing in the arguments, which must be either <classname>String</classname> objects, or
<classname>Integer</classname> objects that ultimately fit within a native <tt>long</tt>.
Up to nine parameters may be passed (14 on the
Atari-ST). The function identified
by <obj>Fixnum</obj> is system dependent. On some Unix systems, the
numbers may be obtained from a header file called
<tt>syscall.h</tt>.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
]]></fullcode>
syscall<nbsp/>4,<nbsp/>1,<nbsp/>"hello\n",<nbsp/>6<nbsp/><nbsp/><nbsp/>#<nbsp/>'4'<nbsp/>is<nbsp/>write(2)<nbsp/>on<nbsp/>our<nbsp/>box
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
hello
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="system" ref="system">
<callseq>
system( <obj>aCmd</obj> <optz>, <obj>args</obj></optz> )
<returns><const>true</const> or <const>false</const></returns>
</callseq>
<desc>
<p/>
Executes <obj>aCmd</obj> in a subshell, returning <const>true</const> if the
command was found and ran successfully, <const>false</const>
otherwise. A detailed error code is available in <var>$?</var>. The
arguments are processed in the same way as for <mmm><file>kernel</file><front>Kernel</front><back>exec</back><mref>exec</mref></mmm>
on page 419.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[!- Dir.chdir("testdir")
system("echo *")
system("echo", "*")
]]></fullcode>
system("echo<nbsp/>*")
system("echo",<nbsp/>"*")
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
config.h<nbsp/>main.rb
*
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="test" ref="test">
<callseq>
test(<obj>aCmd</obj>, <obj>file1</obj> <opt>, <obj>file2</obj></opt> )
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<p/>
Uses the integer <obj>aCmd</obj> to perform various tests on
<obj>file1</obj> (Table 23.3 on page 430) or on <obj>file1</obj> and
<obj>file2</obj> (Table 23.4).
<figure type="table">
<caption>File tests with a single argument</caption>
<table>
<th>
<td><b>Integer</b></td>
<td><b>Description</b></td>
<td><b>Returns</b></td>
</th>
<tr>
<td>?A</td>
<td>Last access time for <obj>file1</obj></td>
<td>Time</td>
</tr>
<tr>
<td>?b</td>
<td>True if <obj>file1</obj> is a block device</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?c</td>
<td>True if <obj>file1</obj> is a character device</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?C</td>
<td>Last change time for <obj>file1</obj></td>
<td>Time</td>
</tr>
<tr>
<td>?d</td>
<td>True if <obj>file1</obj> exists and is a directory</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?e</td>
<td>True if <obj>file1</obj> exists</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?f</td>
<td>True if <obj>file1</obj> exists and is a regular file</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?g</td>
<td>True if <obj>file1</obj> has the <tt>setgid</tt> bit set (false under
NT)</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?G</td>
<td>True if <obj>file1</obj> exists and has a group ownership equal to
the caller's group</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?k</td>
<td>True if <obj>file1</obj> exists and has the sticky bit set</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?l</td>
<td>True if <obj>file1</obj> exists and is a symbolic link</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?M</td>
<td>Last modification time for <obj>file1</obj></td>
<td>Time</td>
</tr>
<tr>
<td>?o</td>
<td>True if <obj>file1</obj> exists and is owned by the caller's
effective uid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?O</td>
<td>True if <obj>file1</obj> exists and is owned by the caller's
real uid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?p</td>
<td>True if <obj>file1</obj> exists and is a fifo</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?r</td>
<td>True if file is readable by the effective uid/gid of the
caller</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?R</td>
<td>True if file is readable by the real uid/gid of the
caller</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?s</td>
<td>If <obj>file1</obj> has nonzero size, return the size, otherwise
return <tt>nil</tt></td>
<td>Integer or <tt>nil</tt></td>
</tr>
<tr>
<td>?S</td>
<td>True if <obj>file1</obj> exists and is a socket</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?u</td>
<td>True if <obj>file1</obj> has the setuid bit set</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?w</td>
<td>True if <obj>file1</obj> exists and is writable by the effective
uid/gid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?W</td>
<td>True if <obj>file1</obj> exists and is writable by the real
uid/gid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?x</td>
<td>True if <obj>file1</obj> exists and is executable by the effective
uid/gid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?X</td>
<td>True if <obj>file1</obj> exists and is executable by the real
uid/gid</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<tr>
<td>?z</td>
<td>True if <obj>file1</obj> exists and has a zero length</td>
<td><const>true</const> or <const>false</const></td>
</tr>
<bottomrule/></table>
<p/>
</figure>
<p/>
<figure type="table">
<caption>File tests with two arguments</caption>
<table>
<th>
<td><b>Integer</b></td>
<td><b>Description</b></td>
</th>
<tr>
<td>?-</td>
<td>True if <obj>file1</obj> is a hard link to <obj>file2</obj></td>
</tr>
<tr>
<td>?=</td>
<td>True if the modification times of <obj>file1</obj> and <obj>file2</obj> are
equal</td>
</tr>
<tr>
<td>?<</td>
<td>True if the modification time of <obj>file1</obj> is prior to that
of <obj>file2</obj></td>
</tr>
<tr>
<td>?></td>
<td>True if the modification time of <obj>file1</obj> is after that
of <obj>file2</obj></td>
</tr>
<bottomrule/></table>
<p/>
</figure>
<p/>
</desc>
</method>
<p/>
<method name="throw" ref="throw">
<callseq>
throw( <obj>aSymbol</obj>
<opt>, <obj>anObject</obj></opt> )
</callseq>
<desc>
<p/>
Transfers control to the end of the active <meth>catch</meth> block
waiting for <obj>aSymbol</obj>. Raises <exception>NameError</exception> if
there is no <meth>catch</meth> block
for the symbol. The optional second
parameter supplies a return value for the <meth>catch</meth> block,
which otherwise defaults to <tt>nil</tt>. For examples, see
<mmm><file>kernel</file><front>Kernel</front><back>catch</back><mref>catch</mref></mmm> on page 417.
<p/>
</desc>
</method>
<p/>
<method name="trace_var" ref="trace_var">
<callseq>
trace_var( <obj>aSymbol</obj>, <obj>aCmd</obj> ) <returns><tt>nil</tt></returns><br/>trace_var( <obj>aSymbol</obj> ) <block>{| val | <blockbody>block</blockbody> }</block>
<p/>
<returns><tt>nil</tt></returns>
</callseq>
<desc>
Controls tracing of assignments to global variables. The
parameter <obj>aSymbol</obj> identifies the variable (as either a
string name or a symbol identifier).
<obj>cmd</obj> (which may be a string or a <classname>Proc</classname> object) or block
is executed whenever the variable is assigned. The block or
<classname>Proc</classname> object receives the variable's new value as a
parameter. Also see <mmm><file>kernel</file><front>Kernel</front><back>untrace_var</back><mref>untrace_var</mref></mmm>.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
$_ = "hello"
$_ = ' there'
]]></fullcode>
trace_var<nbsp/>:$_,<nbsp/>proc<nbsp/>{|v|<nbsp/>puts<nbsp/>"$_<nbsp/>is<nbsp/>now<nbsp/>'#{v}'"<nbsp/>}
$_<nbsp/>=<nbsp/>"hello"
$_<nbsp/>=<nbsp/>'<nbsp/>there'
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
$_<nbsp/>is<nbsp/>now<nbsp/>'hello'
$_<nbsp/>is<nbsp/>now<nbsp/>'<nbsp/>there'
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="trap" ref="trap">
<callseq>
trap( <obj>signal</obj>, <obj>cmd</obj> ) <returns><obj>anObject</obj></returns><br/>trap( <obj>signal</obj> ) <block>{| | <blockbody>block</blockbody> }</block>
<returns><obj>anObject</obj></returns>
</callseq>
<desc>
<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/>
The special signal name ``EXIT'' or signal
number zero will be invoked just prior to program termination.
<p/>
<meth>trap</meth> returns the previous handler for the given signal.
<p/>
<codefragment>
<alltt><fullcode><![CDATA[ trap 0, proc { puts "Terminating: #{$$}" }
trap("CLD") { puts "Child died" }
fork && Process.wait
]]></fullcode>
trap<nbsp/>0,<nbsp/>proc<nbsp/>{<nbsp/>puts<nbsp/>"Terminating:<nbsp/>#{$$}"<nbsp/>}
trap("CLD")<nbsp/>{<nbsp/>puts<nbsp/>"Child<nbsp/>died"<nbsp/>}
fork<nbsp/>&&<nbsp/>Process.wait
</alltt>
</codefragment>
<em>produces:</em>
<codefragment><alltt>
Terminating:<nbsp/>16422
Child<nbsp/>died
Terminating:<nbsp/>16421
</alltt>
</codefragment>
<p/>
</desc>
</method>
<p/>
<method name="untrace_var" ref="untrace_var">
<callseq>
untrace_var( <obj>aSymbol</obj> <opt>,
<obj>aCmd</obj></opt> ) <returns><obj>anArray</obj> or <tt>nil</tt></returns>
</callseq>
<desc>
Removes tracing
for the specified command on the given global variable and
returns <tt>nil</tt>. If no command is specified, removes all tracing for
that variable and returns an array containing the commands
actually removed.
<p/>
</desc>
</method>
<p/>
</methods>
<p/>
</class>
</ppdoc>
|