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
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="ref_c_float.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_integer.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Hash</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#_ob_cb">[ ]</a> <a href="#new">new</a> <a href="#_eq_eq"><i>==</i></a> <a href="#_ob_cb"><i>[ ]</i></a> <a href="#_ob_cb_eq"><i>[ ]=</i></a> <a href="#clear"><i>clear</i></a> <a href="#default"><i>default</i></a> <a href="#default_eq"><i>default=</i></a> <a href="#delete"><i>delete</i></a> <a href="#delete_if"><i>delete_if</i></a> <a href="#each"><i>each</i></a> <a href="#each_key"><i>each_key</i></a> <a href="#each_pair"><i>each_pair</i></a> <a href="#each_value"><i>each_value</i></a> <a href="#empty_qm"><i>empty?</i></a> <a href="#fetch"><i>fetch</i></a> <a href="#has_key_qm"><i>has_key?</i></a> <a href="#has_value_qm"><i>has_value?</i></a> <a href="#include_qm"><i>include?</i></a> <a href="#index"><i>index</i></a> <a href="#indexes"><i>indexes</i></a> <a href="#indices"><i>indices</i></a> <a href="#invert"><i>invert</i></a> <a href="#key_qm"><i>key?</i></a> <a href="#keys"><i>keys</i></a> <a href="#length"><i>length</i></a> <a href="#member_qm"><i>member?</i></a> <a href="#rehash"><i>rehash</i></a> <a href="#reject"><i>reject</i></a> <a href="#reject_oh"><i>reject!</i></a> <a href="#replace"><i>replace</i></a> <a href="#shift"><i>shift</i></a> <a href="#size"><i>size</i></a> <a href="#sort"><i>sort</i></a> <a href="#store"><i>store</i></a> <a href="#to_a"><i>to_a</i></a> <a href="#to_s"><i>to_s</i></a> <a href="#update"><i>update</i></a> <a href="#value_qm"><i>value?</i></a> <a href="#values"><i>values</i></a> <p></p><hr>
<P></P>
A <code>Hash</code> is a collection of key-value pairs. It is similar to
an <code>Array</code>, except that indexing is done via arbitrary keys of any
object type, not an integer index. The order in which you traverse a
hash by either key or value may seem arbitrary, and will generally not be
in the insertion order.
<P></P>
Hashes have a <em>default value</em> that is returned when accessing
keys that do not exist in the hash. By default, that value is
<code>nil</code>.
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">
mixins
</font></td></tr>
<tr><td>Enumerable:
</td><td>
collect, detect, each_with_index, entries, find, find_all, grep,
include?, map, max, member?, min, reject, select, sort, to_a</td></tr>
</table>
<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="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
Hash[ <i>[</i><i>key</i> => <i>value</i><i>]<sup>*</sup></i> ]
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Creates a new hash populated with the given objects. Equivalent
to the literal <code>{ <i>key</i>, <i>value</i>, ... }</code>.
Keys and values occur in pairs, so there must be an
even number of arguments.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Hash["a", 100, "b", 200]</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100, "b"=>200}</code></td>
</tr>
<tr>
<td valign="top"><code>Hash["a" => 100, "b" => 200]</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100, "b"=>200}</code></td>
</tr>
<tr>
<td valign="top"><code>{ "a" => 100, "b" => 200 }</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100, "b"=>200}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Hash.new( <i>anObject</i>=<code>nil</code> )
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Returns a new, empty hash. If <i>anObject</i> is
specified, it will be used as the <em>default value</em>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = Hash.new("Go Fish")</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h["a"] = 100</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h["b"] = 200</code></td>
</tr>
<tr>
<td valign="top"><code>h["a"]</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td valign="top"><code>h["c"]</code></td>
<td valign="top"></td>
<td valign="top"><code>"Go Fish"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_eq_eq"><b>==</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i> == <i>anOtherHash</i>
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Equality---Two hashes are equal if they each contain the same number
of keys and if each key-value pair is equal to (according to
<a href="ref_c_object.html#_eq_eq"><code>Object#==</code></a>) the corresponding elements in the other hash.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h1 = { "a" => 1, "c" => 2 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h2 = { "a" => 1, "c" => 2, 7 => 35 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h3 = { "a" => 1, "c" => 2, 7 => 35 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h4 = { "a" => 1, "d" => 2, "f" => 35 }</code></td>
</tr>
<tr>
<td valign="top"><code>h1 == h2</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>h2 == h3</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>h3 == h4</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>[ <i>aKeyObject</i> ]
-> <i>aValueObject</i>
</td></tr><td></td><td>
<P></P>
Element Reference---Retrieves the <i>aValueObject</i> stored for
<i>aKeyObject</i>. If not found, returns the <em>default value</em>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h["a"]</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td valign="top"><code>h["c"]</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb_eq"><b>[ ]=</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>[ <i>aKeyObject</i> ] = <i>aValueObject</i>
-> <i>aValueObject</i>
</td></tr><td></td><td>
<P></P>
Element Assignment---Associates the value given by
<i>aValueObject</i> with the key given by <i>aKeyObject</i>.
<i>aKeyObject</i> should not have its value changed while it is
in use as a key (a <code>String</code> passed as a key will be
duplicated and frozen).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h["a"] = 9</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h["c"] = 4</code></td>
</tr>
<tr>
<td valign="top"><code>h</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>9, "b"=>200, "c"=>4}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="clear"><b>clear</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.clear -> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Removes all key-value pairs from <i>hsh</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100, "b"=>200}</code></td>
</tr>
<tr>
<td valign="top"><code>h.clear</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="default"><b>default</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.default -> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Returns the ``<em>default value</em>''---that is, the value returned for a
key that does not exist in the hash. Defaults to <code>nil</code>.
See also <a href="ref_c_hash.html#default_eq"><code>Hash#default=</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="default_eq"><b>default=</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.default = <i>anObject</i>
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Sets the ``<em>default value</em>''---that is, the value returned for a
key that does not exist in the hash. Defaults to <code>nil</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h.default = "Go fish"</code></td>
</tr>
<tr>
<td valign="top"><code>h["a"]</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td valign="top"><code>h["z"]</code></td>
<td valign="top"></td>
<td valign="top"><code>"Go fish"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="delete"><b>delete</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.delete( <i>aKeyObject</i> )
-> <i>aValueObject</i><br><i>hsh</i>.delete( <i>aKeyObject</i> ) {| aKeyObject | block }
<P></P>
-> <i>aValueObject</i>
<P></P>
</td></tr><td></td><td>
<P></P>
Deletes and returns a key-value pair from <i>hsh</i> whose key is equal to
<i>aKeyObject</i>. If the key is not found, returns
the <em>default value</em>. If the optional code block is given
and the key is not found, pass in the key and return
the result of <i>block</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.delete("a")</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td valign="top"><code>h.delete("z")</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>h.delete("z") { |el| "#{el} not found" }</code></td>
<td valign="top"></td>
<td valign="top"><code>"z not found"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="delete_if"><b>delete_if</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.delete_if {| key, value | block }
<P></P>
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
evaluates to <code>true</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.delete_if {|key, value| key >= "b" }</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each"><b>each</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.each {| key, value | block }
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
and value as parameters.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
h = { "a" => 100, "b" => 200 }
h.each {|key, value| print key, " is ", value, "\n" }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a is 100
b is 200
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each_key"><b>each_key</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.each_key {| key | block }
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
as a parameter.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
h = { "a" => 100, "b" => 200 }
h.each_key {|key| puts key }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a
b
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each_pair"><b>each_pair</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.each_pair {| key, value | block }
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#each"><code>Hash#each</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each_value"><b>each_value</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.each_value {| value | block }
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Calls <i>block</i> once for each key in <i>hsh</i>, passing the value
as a parameter.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
h = { "a" => 100, "b" => 200 }
h.each_value {|value| puts value }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
100
200
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="empty_qm"><b>empty?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.empty? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>{}.empty?</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="fetch"><b>fetch</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.fetch( <i>aKeyObject</i> <i>[</i>, <i>aDefObject</i><i>]</i> )
-> <i>anObject</i><br><i>hsh</i>.fetch( <i>aKeyObject</i> ) {| aKeyObject | block }
<P></P>
-> <i>anObject</i>
</td></tr><td></td><td>
<P></P>
Returns a value from the hash for the given key. If the key
can't be found, there are several options: With no other
arguments, it will raise an <code>IndexError</code> exception; if
<i>aDefObject</i> is given, then that will be returned; if the
optional code block is specified, then that will be run and its
result returned.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.fetch("a")</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td valign="top"><code>h.fetch("z", "go fish")</code></td>
<td valign="top"></td>
<td valign="top"><code>"go fish"</code></td>
</tr>
<tr>
<td valign="top"><code>h.fetch("z") { |el| "go fish, #{el}"}</code></td>
<td valign="top"></td>
<td valign="top"><code>"go fish, z"</code></td>
</tr>
</table>
<P></P>
<P></P>
The following example shows that an exception is raised if the
key is not found and a default value is not supplied.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
h = { "a" => 100, "b" => 200 }
h.fetch("z")
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
prog.rb:2:in `fetch': key not found (IndexError)
from prog.rb:2
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="has_key_qm"><b>has_key?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.has_key?( <i>aKeyObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the given key is present in <i>hsh</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.has_key?("a")</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>h.has_key?("z")</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="has_value_qm"><b>has_value?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.has_value?( <i>aValueObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the given value is present for some key in
<i>hsh</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.has_value?(100)</code></td>
<td valign="top"></td>
<td valign="top"><code>true</code></td>
</tr>
<tr>
<td valign="top"><code>h.has_value?(999)</code></td>
<td valign="top"></td>
<td valign="top"><code>false</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="include_qm"><b>include?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.include?( <i>aKeyObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#has_key_qm"><code>Hash#has_key?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="index"><b>index</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.index( <i>aValueObject</i> )
-> <i>aKeyObject</i>
</td></tr><td></td><td>
<P></P>
Returns the key for a given value. If not found, returns the
<em>default value</em>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.index(200)</code></td>
<td valign="top"></td>
<td valign="top"><code>"b"</code></td>
</tr>
<tr>
<td valign="top"><code>h.index(999)</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="indexes"><b>indexes</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.indexes( <i>[</i><i>key</i><i>]<sup>+</sup></i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a new array consisting of values for the given key(s).
Will insert the <em>default value</em> for keys that are not found.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.indexes("a", "c")</code></td>
<td valign="top"></td>
<td valign="top"><code>[100, 300]</code></td>
</tr>
<tr>
<td valign="top"><code>h.indexes("a", "c", "z")</code></td>
<td valign="top"></td>
<td valign="top"><code>[100, 300, nil]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="indices"><b>indices</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.indices( <i>[</i><i>key</i><i>]<sup>+</sup></i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#indexes"><code>Hash#indexes</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="invert"><b>invert</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.invert -> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Returns a new hash created by using <i>hsh</i>'s values as keys, and the
keys as values.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.invert</code></td>
<td valign="top"></td>
<td valign="top"><code>{200=>"d", 300=>"y", 0=>"a", 100=>"n"}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="key_qm"><b>key?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.key?( <i>aKeyObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#has_key_qm"><code>Hash#has_key?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="keys"><b>keys</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.keys -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a new array populated with the keys from this hash.
See also <a href="ref_c_hash.html#values"><code>Hash#values</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.keys</code></td>
<td valign="top"></td>
<td valign="top"><code>["a", "b", "c", "d"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="length"><b>length</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.length -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the number of key-value pairs in the hash.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.length</code></td>
<td valign="top"></td>
<td valign="top"><code>4</code></td>
</tr>
<tr>
<td valign="top"><code>h.delete("a")</code></td>
<td valign="top"></td>
<td valign="top"><code>200</code></td>
</tr>
<tr>
<td valign="top"><code>h.length</code></td>
<td valign="top"></td>
<td valign="top"><code>3</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="member_qm"><b>member?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.member?( <i>aKeyObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#has_key_qm"><code>Hash#has_key?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="rehash"><b>rehash</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.rehash -> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Rebuilds the hash based on the current hash values for each key.
If values of key objects have changed since they were inserted,
this method will reindex <i>hsh</i>.
If <a href="ref_c_hash.html#rehash"><code>Hash#rehash</code></a> is called while an
iterator is traversing the hash, an <code>IndexError</code> will be
raised in the iterator.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = [ "a", "b" ]</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>c = [ "c", "d" ]</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h = { a => 100, c => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h[a]</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>a[0] = "z"</code></td>
</tr>
<tr>
<td valign="top"><code>h[a]</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
<tr>
<td valign="top"><code>h.rehash</code></td>
<td valign="top"></td>
<td valign="top"><code>{["c", "d"]=>300, ["z", "b"]=>100}</code></td>
</tr>
<tr>
<td valign="top"><code>h[a]</code></td>
<td valign="top"></td>
<td valign="top"><code>100</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="reject"><b>reject</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.reject {| key, value | block }
<P></P>
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Same as <a href="ref_c_hash.html#delete_if"><code>Hash#delete_if</code></a>, but works on (and returns) a
copy of the <i>hsh</i>. Equivalent to <code><i>hsh</i>.dup.delete_if</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="reject_oh"><b>reject!</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.reject! {| key, value | block }
<P></P>
-> <i>hsh</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Equivalent to <a href="ref_c_hash.html#delete_if"><code>Hash#delete_if</code></a>, but returns <code>nil</code> if no
changes were made.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="replace"><b>replace</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.replace( <i>anOtherHash</i> )
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Replaces the contents of <i>hsh</i> with the contents of
<i>anOtherHash</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.replace({ "c" => 300, "d" => 400 })</code></td>
<td valign="top"></td>
<td valign="top"><code>{"c"=>300, "d"=>400}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="shift"><b>shift</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.shift -> <i>anArray</i> or <code>nil</code>
</td></tr><td></td><td>
<P></P>
Removes a key-value pair
from <i>hsh</i>
and returns it as the two-item array <code>[</code>
<i>key, value</i> <code>]</code>, or <code>nil</code> if the
hash is empty.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { 1 => "a", 2 => "b", 3 => "c" }</code></td>
</tr>
<tr>
<td valign="top"><code>h.shift</code></td>
<td valign="top"></td>
<td valign="top"><code>[1, "a"]</code></td>
</tr>
<tr>
<td valign="top"><code>h</code></td>
<td valign="top"></td>
<td valign="top"><code>{2=>"b", 3=>"c"}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="size"><b>size</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.size -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#length"><code>Hash#length</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sort"><b>sort</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.sort
-> <i>anArray</i> <br><i>hsh</i>.sort {| a, b | block }
<P></P>
-> <i>anArray</i>
<P></P>
</td></tr><td></td><td>
<P></P>
Converts <i>hsh</i> to a nested array of <code>[</code> <i>key, value</i>
<code>]</code> arrays and sorts it, using <a href="ref_c_array.html#sort"><code>Array#sort</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 20, "b" => 30, "c" => 10 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.sort</code></td>
<td valign="top"></td>
<td valign="top"><code>[["a", 20], ["b", 30], ["c", 10]]</code></td>
</tr>
<tr>
<td valign="top"><code>h.sort {|a,b| a[1]<=>b[1]}</code></td>
<td valign="top"></td>
<td valign="top"><code>[["c", 10], ["a", 20], ["b", 30]]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="store"><b>store</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.store( <i>aKeyObject, aValueObject</i> )
-> <i>aValueObject</i>
</td></tr><td></td><td>
<P></P>
Synonym for Element Assignment (<a href="ref_c_hash.html#_ob_cb_eq"><code>Hash#[]=</code></a>).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_a"><b>to_a</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.to_a
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Converts <i>hsh</i> to a nested array of <code>[</code> <i>key, value</i>
<code>]</code> arrays.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.to_a</code></td>
<td valign="top"></td>
<td valign="top"><code>[["a", 100], ["c", 300], ["d", 400]]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="to_s"><b>to_s</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.to_s -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Converts <i>hsh</i> to a string by converting the hash to an array
of <code>[</code> <i>key, value</i> <code>]</code> pairs and then converting that array to a string using
<a href="ref_c_array.html#join"><code>Array#join</code></a> with the default separator.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.to_s</code></td>
<td valign="top"></td>
<td valign="top"><code>"a100c300d400"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="update"><b>update</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.update( <i>anOtherHash</i> )
-> <i>hsh</i>
</td></tr><td></td><td>
<P></P>
Adds the contents of <i>anOtherHash</i> to <i>hsh</i>, overwriting
entries with duplicate keys with those from <i>anOtherHash</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h1 = { "a" => 100, "b" => 200 }</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>h2 = { "b" => 254, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h1.update(h2)</code></td>
<td valign="top"></td>
<td valign="top"><code>{"a"=>100, "b"=>254, "c"=>300}</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="value_qm"><b>value?</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.value?( <i>aValueObject</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Synonym for <a href="ref_c_hash.html#has_value_qm"><code>Hash#has_value?</code></a>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="values"><b>values</b></a></font></td><td bgcolor="#ffaaaa">
<i>hsh</i>.values -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a new array populated with the values from <i>hsh</i>.
See also <a href="ref_c_hash.html#keys"><code>Hash#keys</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>h = { "a" => 100, "b" => 200, "c" => 300 }</code></td>
</tr>
<tr>
<td valign="top"><code>h.values</code></td>
<td valign="top"></td>
<td valign="top"><code>[100, 200, 300]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td></table>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="ref_c_float.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="builtins.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="ref_c_integer.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>
|