rbs 4.0.0.dev.4 → 4.0.0.dev.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +14 -14
  3. data/.github/workflows/bundle-update.yml +60 -0
  4. data/.github/workflows/c-check.yml +11 -8
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/dependabot.yml +1 -1
  7. data/.github/workflows/ruby.yml +17 -34
  8. data/.github/workflows/typecheck.yml +2 -2
  9. data/.github/workflows/valgrind.yml +42 -0
  10. data/.github/workflows/windows.yml +2 -2
  11. data/.rubocop.yml +1 -1
  12. data/README.md +1 -1
  13. data/Rakefile +32 -5
  14. data/config.yml +46 -0
  15. data/core/array.rbs +96 -46
  16. data/core/binding.rbs +0 -2
  17. data/core/builtin.rbs +2 -2
  18. data/core/comparable.rbs +13 -6
  19. data/core/complex.rbs +55 -41
  20. data/core/dir.rbs +4 -4
  21. data/core/encoding.rbs +7 -10
  22. data/core/enumerable.rbs +90 -3
  23. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  24. data/core/enumerator.rbs +63 -1
  25. data/core/errno.rbs +8 -0
  26. data/core/errors.rbs +28 -1
  27. data/core/exception.rbs +2 -2
  28. data/core/fiber.rbs +40 -20
  29. data/core/file.rbs +108 -78
  30. data/core/file_test.rbs +1 -1
  31. data/core/float.rbs +225 -69
  32. data/core/gc.rbs +417 -281
  33. data/core/hash.rbs +1023 -727
  34. data/core/integer.rbs +104 -110
  35. data/core/io/buffer.rbs +21 -10
  36. data/core/io/wait.rbs +11 -33
  37. data/core/io.rbs +82 -19
  38. data/core/kernel.rbs +70 -59
  39. data/core/marshal.rbs +1 -1
  40. data/core/match_data.rbs +1 -1
  41. data/core/math.rbs +42 -3
  42. data/core/method.rbs +63 -27
  43. data/core/module.rbs +103 -26
  44. data/core/nil_class.rbs +3 -3
  45. data/core/numeric.rbs +43 -35
  46. data/core/object.rbs +3 -3
  47. data/core/object_space.rbs +21 -15
  48. data/core/pathname.rbs +1272 -0
  49. data/core/proc.rbs +30 -25
  50. data/core/process.rbs +4 -2
  51. data/core/ractor.rbs +361 -509
  52. data/core/random.rbs +17 -0
  53. data/core/range.rbs +113 -16
  54. data/core/rational.rbs +56 -85
  55. data/core/rbs/unnamed/argf.rbs +2 -2
  56. data/core/rbs/unnamed/env_class.rbs +1 -1
  57. data/core/rbs/unnamed/random.rbs +4 -113
  58. data/core/regexp.rbs +25 -20
  59. data/core/ruby.rbs +53 -0
  60. data/core/ruby_vm.rbs +6 -4
  61. data/core/rubygems/errors.rbs +3 -70
  62. data/core/rubygems/rubygems.rbs +11 -79
  63. data/core/rubygems/version.rbs +2 -3
  64. data/core/set.rbs +488 -359
  65. data/core/signal.rbs +24 -14
  66. data/core/string.rbs +3171 -1241
  67. data/core/struct.rbs +1 -1
  68. data/core/symbol.rbs +17 -11
  69. data/core/thread.rbs +95 -33
  70. data/core/time.rbs +35 -9
  71. data/core/trace_point.rbs +7 -4
  72. data/core/unbound_method.rbs +14 -6
  73. data/docs/aliases.md +79 -0
  74. data/docs/collection.md +2 -2
  75. data/docs/encoding.md +56 -0
  76. data/docs/gem.md +0 -1
  77. data/docs/inline.md +470 -0
  78. data/docs/sigs.md +3 -3
  79. data/docs/syntax.md +33 -4
  80. data/docs/type_fingerprint.md +21 -0
  81. data/exe/rbs +1 -1
  82. data/ext/rbs_extension/ast_translation.c +77 -3
  83. data/ext/rbs_extension/ast_translation.h +3 -0
  84. data/ext/rbs_extension/class_constants.c +8 -2
  85. data/ext/rbs_extension/class_constants.h +4 -0
  86. data/ext/rbs_extension/extconf.rb +5 -1
  87. data/ext/rbs_extension/legacy_location.c +5 -5
  88. data/ext/rbs_extension/main.c +37 -20
  89. data/include/rbs/ast.h +85 -38
  90. data/include/rbs/defines.h +27 -0
  91. data/include/rbs/lexer.h +30 -11
  92. data/include/rbs/parser.h +6 -6
  93. data/include/rbs/string.h +0 -2
  94. data/include/rbs/util/rbs_allocator.h +34 -13
  95. data/include/rbs/util/rbs_assert.h +12 -1
  96. data/include/rbs/util/rbs_encoding.h +2 -0
  97. data/include/rbs/util/rbs_unescape.h +2 -1
  98. data/lib/rbs/ast/annotation.rb +1 -1
  99. data/lib/rbs/ast/comment.rb +1 -1
  100. data/lib/rbs/ast/declarations.rb +10 -10
  101. data/lib/rbs/ast/members.rb +14 -14
  102. data/lib/rbs/ast/ruby/annotations.rb +137 -0
  103. data/lib/rbs/ast/ruby/comment_block.rb +24 -0
  104. data/lib/rbs/ast/ruby/declarations.rb +198 -3
  105. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
  106. data/lib/rbs/ast/ruby/members.rb +159 -1
  107. data/lib/rbs/ast/type_param.rb +24 -4
  108. data/lib/rbs/buffer.rb +20 -15
  109. data/lib/rbs/cli/diff.rb +16 -15
  110. data/lib/rbs/cli/validate.rb +38 -51
  111. data/lib/rbs/cli.rb +52 -19
  112. data/lib/rbs/collection/config/lockfile_generator.rb +8 -0
  113. data/lib/rbs/collection/sources/git.rb +1 -0
  114. data/lib/rbs/definition.rb +1 -1
  115. data/lib/rbs/definition_builder/ancestor_builder.rb +62 -9
  116. data/lib/rbs/definition_builder/method_builder.rb +20 -0
  117. data/lib/rbs/definition_builder.rb +91 -2
  118. data/lib/rbs/diff.rb +7 -1
  119. data/lib/rbs/environment.rb +227 -74
  120. data/lib/rbs/environment_loader.rb +0 -6
  121. data/lib/rbs/errors.rb +27 -7
  122. data/lib/rbs/inline_parser.rb +341 -5
  123. data/lib/rbs/location_aux.rb +1 -1
  124. data/lib/rbs/locator.rb +5 -1
  125. data/lib/rbs/method_type.rb +5 -3
  126. data/lib/rbs/parser_aux.rb +2 -2
  127. data/lib/rbs/prototype/rb.rb +2 -2
  128. data/lib/rbs/prototype/rbi.rb +2 -0
  129. data/lib/rbs/prototype/runtime.rb +8 -0
  130. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  131. data/lib/rbs/resolver/type_name_resolver.rb +116 -38
  132. data/lib/rbs/subtractor.rb +3 -1
  133. data/lib/rbs/test/type_check.rb +16 -2
  134. data/lib/rbs/type_name.rb +1 -1
  135. data/lib/rbs/types.rb +27 -27
  136. data/lib/rbs/validator.rb +2 -2
  137. data/lib/rbs/version.rb +1 -1
  138. data/lib/rbs.rb +1 -1
  139. data/lib/rdoc/discover.rb +1 -1
  140. data/lib/rdoc_plugin/parser.rb +1 -1
  141. data/rbs.gemspec +3 -2
  142. data/schema/typeParam.json +17 -1
  143. data/sig/ast/ruby/annotations.rbs +124 -0
  144. data/sig/ast/ruby/comment_block.rbs +8 -0
  145. data/sig/ast/ruby/declarations.rbs +102 -4
  146. data/sig/ast/ruby/members.rbs +87 -1
  147. data/sig/cli/diff.rbs +5 -11
  148. data/sig/cli/validate.rbs +13 -4
  149. data/sig/cli.rbs +18 -18
  150. data/sig/definition.rbs +6 -1
  151. data/sig/environment.rbs +70 -12
  152. data/sig/errors.rbs +13 -6
  153. data/sig/inline_parser.rbs +39 -2
  154. data/sig/locator.rbs +0 -2
  155. data/sig/manifest.yaml +0 -1
  156. data/sig/method_builder.rbs +3 -1
  157. data/sig/method_types.rbs +1 -1
  158. data/sig/parser.rbs +16 -2
  159. data/sig/resolver/type_name_resolver.rbs +35 -7
  160. data/sig/source.rbs +3 -3
  161. data/sig/type_param.rbs +13 -8
  162. data/sig/types.rbs +4 -4
  163. data/src/ast.c +80 -1
  164. data/src/lexer.c +1392 -1313
  165. data/src/lexer.re +3 -0
  166. data/src/lexstate.c +58 -37
  167. data/src/location.c +4 -4
  168. data/src/parser.c +412 -145
  169. data/src/string.c +0 -48
  170. data/src/util/rbs_allocator.c +89 -71
  171. data/src/util/rbs_assert.c +1 -1
  172. data/src/util/rbs_buffer.c +2 -2
  173. data/src/util/rbs_constant_pool.c +10 -10
  174. data/src/util/rbs_encoding.c +4 -8
  175. data/src/util/rbs_unescape.c +56 -20
  176. data/stdlib/bigdecimal/0/big_decimal.rbs +100 -82
  177. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  178. data/stdlib/cgi/0/core.rbs +9 -393
  179. data/stdlib/cgi/0/manifest.yaml +1 -0
  180. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  181. data/stdlib/coverage/0/coverage.rbs +3 -1
  182. data/stdlib/date/0/date.rbs +67 -59
  183. data/stdlib/date/0/date_time.rbs +1 -1
  184. data/stdlib/delegate/0/delegator.rbs +10 -7
  185. data/stdlib/digest/0/digest.rbs +110 -0
  186. data/stdlib/erb/0/erb.rbs +737 -347
  187. data/stdlib/fileutils/0/fileutils.rbs +20 -14
  188. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  189. data/stdlib/json/0/json.rbs +82 -28
  190. data/stdlib/net-http/0/net-http.rbs +3 -0
  191. data/stdlib/objspace/0/objspace.rbs +9 -27
  192. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  193. data/stdlib/open3/0/open3.rbs +459 -1
  194. data/stdlib/openssl/0/openssl.rbs +331 -228
  195. data/stdlib/optparse/0/optparse.rbs +8 -3
  196. data/stdlib/pathname/0/pathname.rbs +9 -1379
  197. data/stdlib/psych/0/psych.rbs +4 -4
  198. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  199. data/stdlib/rdoc/0/code_object.rbs +2 -1
  200. data/stdlib/rdoc/0/parser.rbs +1 -1
  201. data/stdlib/rdoc/0/rdoc.rbs +1 -1
  202. data/stdlib/rdoc/0/store.rbs +1 -1
  203. data/stdlib/resolv/0/resolv.rbs +25 -68
  204. data/stdlib/ripper/0/ripper.rbs +2 -2
  205. data/stdlib/securerandom/0/manifest.yaml +2 -0
  206. data/stdlib/securerandom/0/securerandom.rbs +6 -19
  207. data/stdlib/singleton/0/singleton.rbs +3 -0
  208. data/stdlib/socket/0/socket.rbs +13 -1
  209. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  210. data/stdlib/stringio/0/stringio.rbs +1176 -85
  211. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  212. data/stdlib/tempfile/0/tempfile.rbs +3 -3
  213. data/stdlib/time/0/time.rbs +1 -1
  214. data/stdlib/timeout/0/timeout.rbs +63 -7
  215. data/stdlib/tsort/0/cyclic.rbs +3 -0
  216. data/stdlib/uri/0/common.rbs +16 -2
  217. data/stdlib/uri/0/file.rbs +1 -1
  218. data/stdlib/uri/0/generic.rbs +24 -16
  219. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  220. data/stdlib/zlib/0/gzip_reader.rbs +2 -2
  221. data/stdlib/zlib/0/gzip_writer.rbs +1 -1
  222. data/stdlib/zlib/0/zstream.rbs +1 -0
  223. metadata +30 -4
data/core/hash.rbs CHANGED
@@ -1,67 +1,68 @@
1
1
  # <!-- rdoc-file=hash.c -->
2
- # A `Hash` maps each of its unique keys to a specific value.
2
+ # A Hash object maps each of its unique keys to a specific value.
3
3
  #
4
- # A `Hash` has certain similarities to an Array, but:
5
- # * An Array index is always an Integer.
6
- # * A `Hash` key can be (almost) any object.
4
+ # A hash has certain similarities to an Array, but:
7
5
  #
8
- # ### `Hash` Data Syntax
6
+ # * An array index is always an integer.
7
+ # * A hash key can be (almost) any object.
9
8
  #
10
- # The older syntax for `Hash` data uses the "hash rocket," `=>`:
9
+ # ### Hash Data Syntax
10
+ #
11
+ # The original syntax for a hash entry uses the "hash rocket," `=>`:
11
12
  #
12
13
  # h = {:foo => 0, :bar => 1, :baz => 2}
13
- # h # => {:foo=>0, :bar=>1, :baz=>2}
14
+ # h # => {foo: 0, bar: 1, baz: 2}
14
15
  #
15
- # Alternatively, but only for a `Hash` key that's a Symbol, you can use a newer
16
- # JSON-style syntax, where each bareword becomes a Symbol:
16
+ # Alternatively, but only for a key that's a symbol, you can use a newer
17
+ # JSON-style syntax, where each bareword becomes a symbol:
17
18
  #
18
19
  # h = {foo: 0, bar: 1, baz: 2}
19
- # h # => {:foo=>0, :bar=>1, :baz=>2}
20
+ # h # => {foo: 0, bar: 1, baz: 2}
20
21
  #
21
- # You can also use a String in place of a bareword:
22
+ # You can also use a string in place of a bareword:
22
23
  #
23
24
  # h = {'foo': 0, 'bar': 1, 'baz': 2}
24
- # h # => {:foo=>0, :bar=>1, :baz=>2}
25
+ # h # => {foo: 0, bar: 1, baz: 2}
25
26
  #
26
27
  # And you can mix the styles:
27
28
  #
28
29
  # h = {foo: 0, :bar => 1, 'baz': 2}
29
- # h # => {:foo=>0, :bar=>1, :baz=>2}
30
+ # h # => {foo: 0, bar: 1, baz: 2}
30
31
  #
31
32
  # But it's an error to try the JSON-style syntax for a key that's not a bareword
32
- # or a String:
33
+ # or a string:
33
34
  #
34
35
  # # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
35
36
  # h = {0: 'zero'}
36
37
  #
37
- # `Hash` value can be omitted, meaning that value will be fetched from the
38
- # context by the name of the key:
38
+ # The value can be omitted, meaning that value will be fetched from the context
39
+ # by the name of the key:
39
40
  #
40
41
  # x = 0
41
42
  # y = 100
42
43
  # h = {x:, y:}
43
- # h # => {:x=>0, :y=>100}
44
+ # h # => {x: 0, y: 100}
44
45
  #
45
46
  # ### Common Uses
46
47
  #
47
- # You can use a `Hash` to give names to objects:
48
+ # You can use a hash to give names to objects:
48
49
  #
49
50
  # person = {name: 'Matz', language: 'Ruby'}
50
- # person # => {:name=>"Matz", :language=>"Ruby"}
51
+ # person # => {name: "Matz", language: "Ruby"}
51
52
  #
52
- # You can use a `Hash` to give names to method arguments:
53
+ # You can use a hash to give names to method arguments:
53
54
  #
54
55
  # def some_method(hash)
55
56
  # p hash
56
57
  # end
57
- # some_method({foo: 0, bar: 1, baz: 2}) # => {:foo=>0, :bar=>1, :baz=>2}
58
+ # some_method({foo: 0, bar: 1, baz: 2}) # => {foo: 0, bar: 1, baz: 2}
58
59
  #
59
- # Note: when the last argument in a method call is a `Hash`, the curly braces
60
- # may be omitted:
60
+ # Note: when the last argument in a method call is a hash, the curly braces may
61
+ # be omitted:
61
62
  #
62
- # some_method(foo: 0, bar: 1, baz: 2) # => {:foo=>0, :bar=>1, :baz=>2}
63
+ # some_method(foo: 0, bar: 1, baz: 2) # => {foo: 0, bar: 1, baz: 2}
63
64
  #
64
- # You can use a `Hash` to initialize an object:
65
+ # You can use a hash to initialize an object:
65
66
  #
66
67
  # class Dev
67
68
  # attr_accessor :name, :language
@@ -73,98 +74,91 @@
73
74
  # matz = Dev.new(name: 'Matz', language: 'Ruby')
74
75
  # matz # => #<Dev: @name="Matz", @language="Ruby">
75
76
  #
76
- # ### Creating a `Hash`
77
+ # ### Creating a Hash
77
78
  #
78
- # You can create a `Hash` object explicitly with:
79
+ # You can create a Hash object explicitly with:
79
80
  #
80
81
  # * A [hash literal](rdoc-ref:syntax/literals.rdoc@Hash+Literals).
81
82
  #
82
- # You can convert certain objects to Hashes with:
83
- #
84
- # * Method #Hash.
83
+ # You can convert certain objects to hashes with:
85
84
  #
86
- # You can create a `Hash` by calling method Hash.new.
85
+ # * Method Kernel#Hash.
87
86
  #
88
- # Create an empty `Hash`:
87
+ # You can create a hash by calling method Hash.new:
89
88
  #
89
+ # # Create an empty hash.
90
90
  # h = Hash.new
91
91
  # h # => {}
92
92
  # h.class # => Hash
93
93
  #
94
- # You can create a `Hash` by calling method Hash.[].
95
- #
96
- # Create an empty `Hash`:
94
+ # You can create a hash by calling method Hash.[]:
97
95
  #
96
+ # # Create an empty hash.
98
97
  # h = Hash[]
99
98
  # h # => {}
100
- #
101
- # Create a `Hash` with initial entries:
102
- #
99
+ # # Create a hash with initial entries.
103
100
  # h = Hash[foo: 0, bar: 1, baz: 2]
104
- # h # => {:foo=>0, :bar=>1, :baz=>2}
101
+ # h # => {foo: 0, bar: 1, baz: 2}
105
102
  #
106
- # You can create a `Hash` by using its literal form (curly braces).
107
- #
108
- # Create an empty `Hash`:
103
+ # You can create a hash by using its literal form (curly braces):
109
104
  #
105
+ # # Create an empty hash.
110
106
  # h = {}
111
107
  # h # => {}
112
- #
113
- # Create a `Hash` with initial entries:
114
- #
108
+ # # Create a +Hash+ with initial entries.
115
109
  # h = {foo: 0, bar: 1, baz: 2}
116
- # h # => {:foo=>0, :bar=>1, :baz=>2}
110
+ # h # => {foo: 0, bar: 1, baz: 2}
117
111
  #
118
- # ### `Hash` Value Basics
112
+ # ### Hash Value Basics
119
113
  #
120
- # The simplest way to retrieve a `Hash` value (instance method #[]):
114
+ # The simplest way to retrieve a hash value (instance method #[]):
121
115
  #
122
116
  # h = {foo: 0, bar: 1, baz: 2}
123
117
  # h[:foo] # => 0
124
118
  #
125
- # The simplest way to create or update a `Hash` value (instance method #[]=):
119
+ # The simplest way to create or update a hash value (instance method #[]=):
126
120
  #
127
121
  # h = {foo: 0, bar: 1, baz: 2}
128
122
  # h[:bat] = 3 # => 3
129
- # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
123
+ # h # => {foo: 0, bar: 1, baz: 2, bat: 3}
130
124
  # h[:foo] = 4 # => 4
131
- # h # => {:foo=>4, :bar=>1, :baz=>2, :bat=>3}
125
+ # h # => {foo: 4, bar: 1, baz: 2, bat: 3}
132
126
  #
133
- # The simplest way to delete a `Hash` entry (instance method #delete):
127
+ # The simplest way to delete a hash entry (instance method #delete):
134
128
  #
135
129
  # h = {foo: 0, bar: 1, baz: 2}
136
130
  # h.delete(:bar) # => 1
137
- # h # => {:foo=>0, :baz=>2}
131
+ # h # => {foo: 0, baz: 2}
138
132
  #
139
133
  # ### Entry Order
140
134
  #
141
- # A `Hash` object presents its entries in the order of their creation. This is
135
+ # A Hash object presents its entries in the order of their creation. This is
142
136
  # seen in:
143
137
  #
144
138
  # * Iterative methods such as `each`, `each_key`, `each_pair`, `each_value`.
145
139
  # * Other order-sensitive methods such as `shift`, `keys`, `values`.
146
- # * The String returned by method `inspect`.
140
+ # * The string returned by method `inspect`.
147
141
  #
148
- # A new `Hash` has its initial ordering per the given entries:
142
+ # A new hash has its initial ordering per the given entries:
149
143
  #
150
144
  # h = Hash[foo: 0, bar: 1]
151
- # h # => {:foo=>0, :bar=>1}
145
+ # h # => {foo: 0, bar: 1}
152
146
  #
153
147
  # New entries are added at the end:
154
148
  #
155
149
  # h[:baz] = 2
156
- # h # => {:foo=>0, :bar=>1, :baz=>2}
150
+ # h # => {foo: 0, bar: 1, baz: 2}
157
151
  #
158
152
  # Updating a value does not affect the order:
159
153
  #
160
154
  # h[:baz] = 3
161
- # h # => {:foo=>0, :bar=>1, :baz=>3}
155
+ # h # => {foo: 0, bar: 1, baz: 3}
162
156
  #
163
157
  # But re-creating a deleted entry can affect the order:
164
158
  #
165
159
  # h.delete(:foo)
166
160
  # h[:foo] = 5
167
- # h # => {:bar=>1, :baz=>3, :foo=>5}
161
+ # h # => {bar: 1, baz: 3, foo: 5}
168
162
  #
169
163
  # ### `Hash` Keys
170
164
  #
@@ -257,94 +251,86 @@
257
251
  #
258
252
  # reviews.length #=> 1
259
253
  #
260
- # ### Default Values
254
+ # ### Key Not Found?
261
255
  #
262
- # The methods #[], #values_at and #dig need to return the value associated to a
263
- # certain key. When that key is not found, that value will be determined by its
264
- # default proc (if any) or else its default (initially `nil`).
256
+ # When a method tries to retrieve and return the value for a key and that key
257
+ # *is found*, the returned value is the value associated with the key.
265
258
  #
266
- # You can retrieve the default value with method #default:
259
+ # But what if the key *is not found*? In that case, certain methods will return
260
+ # a default value while other will raise a KeyError.
267
261
  #
268
- # h = Hash.new
269
- # h.default # => nil
262
+ # #### Nil Return Value
270
263
  #
271
- # You can set the default value by passing an argument to method Hash.new or
272
- # with method #default=
264
+ # If you want `nil` returned for a not-found key, you can call:
273
265
  #
274
- # h = Hash.new(-1)
275
- # h.default # => -1
276
- # h.default = 0
277
- # h.default # => 0
266
+ # * #[](key) (usually written as `#[key]`.
267
+ # * #assoc(key).
268
+ # * #dig(key, *identifiers).
269
+ # * #values_at(*keys).
278
270
  #
279
- # This default value is returned for #[], #values_at and #dig when a key is not
280
- # found:
271
+ # You can override these behaviors for #[], #dig, and #values_at (but not
272
+ # #assoc); see [Hash Default](rdoc-ref:Hash@Hash+Default).
281
273
  #
282
- # counts = {foo: 42}
283
- # counts.default # => nil (default)
284
- # counts[:foo] = 42
285
- # counts[:bar] # => nil
286
- # counts.default = 0
287
- # counts[:bar] # => 0
288
- # counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
289
- # counts.dig(:bar) # => 0
274
+ # #### KeyError
290
275
  #
291
- # Note that the default value is used without being duplicated. It is not
292
- # advised to set the default value to a mutable object:
276
+ # If you want KeyError raised for a not-found key, you can call:
293
277
  #
294
- # synonyms = Hash.new([])
295
- # synonyms[:hello] # => []
296
- # synonyms[:hello] << :hi # => [:hi], but this mutates the default!
297
- # synonyms.default # => [:hi]
298
- # synonyms[:world] << :universe
299
- # synonyms[:world] # => [:hi, :universe], oops
300
- # synonyms.keys # => [], oops
278
+ # * #fetch(key).
279
+ # * #fetch_values(*keys).
301
280
  #
302
- # To use a mutable object as default, it is recommended to use a default proc
281
+ # #### Hash Default
303
282
  #
304
- # #### Default Proc
283
+ # For certain methods (#[], #dig, and #values_at), the return value for a
284
+ # not-found key is determined by two hash properties:
305
285
  #
306
- # When the default proc for a `Hash` is set (i.e., not `nil`), the default value
307
- # returned by method #[] is determined by the default proc alone.
286
+ # * *default value*: returned by method #default.
287
+ # * *default proc*: returned by method #default_proc.
308
288
  #
309
- # You can retrieve the default proc with method #default_proc:
289
+ # In the simple case, both values are `nil`, and the methods return `nil` for a
290
+ # not-found key; see [Nil Return Value](rdoc-ref:Hash@Nil+Return+Value) above.
310
291
  #
311
- # h = Hash.new
312
- # h.default_proc # => nil
292
+ # Note that this entire section ("Hash Default"):
313
293
  #
314
- # You can set the default proc by calling Hash.new with a block or calling the
315
- # method #default_proc=
294
+ # * Applies *only* to methods #[], #dig, and #values_at.
295
+ # * Does *not* apply to methods #assoc, #fetch, or #fetch_values, which are
296
+ # not affected by the default value or default proc.
316
297
  #
317
- # h = Hash.new { |hash, key| "Default value for #{key}" }
318
- # h.default_proc.class # => Proc
319
- # h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
320
- # h.default_proc.class # => Proc
298
+ # ##### Any-Key Default
321
299
  #
322
- # When the default proc is set (i.e., not `nil`) and method #[] is called with
323
- # with a non-existent key, #[] calls the default proc with both the `Hash`
324
- # object itself and the missing key, then returns the proc's return value:
300
+ # You can define an any-key default for a hash; that is, a value that will be
301
+ # returned for *any* not-found key:
325
302
  #
326
- # h = Hash.new { |hash, key| "Default value for #{key}" }
327
- # h[:nosuch] # => "Default value for nosuch"
303
+ # * The value of #default_proc *must be* `nil`.
304
+ # * The value of #default (which may be any object, including `nil`) will be
305
+ # returned for a not-found key.
328
306
  #
329
- # Note that in the example above no entry for key `:nosuch` is created:
307
+ # You can set the default value when the hash is created with Hash.new and
308
+ # option `default_value`, or later with method #default=.
330
309
  #
331
- # h.include?(:nosuch) # => false
310
+ # Note: although the value of #default may be any object, it may not be a good
311
+ # idea to use a mutable object.
332
312
  #
333
- # However, the proc itself can add a new entry:
313
+ # ##### Per-Key Defaults
334
314
  #
335
- # synonyms = Hash.new { |hash, key| hash[key] = [] }
336
- # synonyms.include?(:hello) # => false
337
- # synonyms[:hello] << :hi # => [:hi]
338
- # synonyms[:world] << :universe # => [:universe]
339
- # synonyms.keys # => [:hello, :world]
315
+ # You can define a per-key default for a hash; that is, a Proc that will return
316
+ # a value based on the key itself.
340
317
  #
341
- # Note that setting the default proc will clear the default value and vice
342
- # versa.
318
+ # You can set the default proc when the hash is created with Hash.new and a
319
+ # block, or later with method #default_proc=.
343
320
  #
344
- # Be aware that a default proc that modifies the hash is not thread-safe in the
345
- # sense that multiple threads can call into the default proc concurrently for
321
+ # Note that the proc can modify `self`, but modifying `self` in this way is not
322
+ # thread-safe; multiple threads can concurrently call into the default proc for
346
323
  # the same key.
347
324
  #
325
+ # #### Method Default
326
+ #
327
+ # For two methods, you can specify a default value for a not-found key that has
328
+ # effect only for a single method call (and not for any subsequent calls):
329
+ #
330
+ # * For method #fetch, you can specify an any-key default:
331
+ # * For either method #fetch or method #fetch_values, you can specify a
332
+ # per-key default via a block.
333
+ #
348
334
  # ### What's Here
349
335
  #
350
336
  # First, what's elsewhere. Class `Hash`:
@@ -366,7 +352,6 @@
366
352
  # * [Converting](rdoc-ref:Hash@Methods+for+Converting)
367
353
  # * [Transforming Keys and
368
354
  # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values)
369
- # * [And more....](rdoc-ref:Hash@Other+Methods)
370
355
  #
371
356
  # Class `Hash` also includes methods from module Enumerable.
372
357
  #
@@ -422,7 +407,7 @@
422
407
  # * #keys: Returns an array containing all keys in `self`.
423
408
  # * #rassoc: Returns a 2-element array consisting of the key and value of the
424
409
  # first-found entry having a given value.
425
- # * #values: Returns an array containing all values in `self`/
410
+ # * #values: Returns an array containing all values in `self`.
426
411
  # * #values_at: Returns an array containing values for given keys.
427
412
  #
428
413
  # #### Methods for Assigning
@@ -466,6 +451,7 @@
466
451
  #
467
452
  # #### Methods for Converting
468
453
  #
454
+ # * #flatten: Returns an array that is a 1-dimensional flattening of `self`.
469
455
  # * #inspect (aliased as #to_s): Returns a new String containing the hash
470
456
  # entries.
471
457
  # * #to_a: Returns a new array of 2-element arrays; each nested array contains
@@ -477,15 +463,12 @@
477
463
  #
478
464
  # #### Methods for Transforming Keys and Values
479
465
  #
466
+ # * #invert: Returns a hash with the each key-value pair inverted.
480
467
  # * #transform_keys: Returns a copy of `self` with modified keys.
481
468
  # * #transform_keys!: Modifies keys in `self`
482
469
  # * #transform_values: Returns a copy of `self` with modified values.
483
470
  # * #transform_values!: Modifies values in `self`.
484
471
  #
485
- # #### Other Methods
486
- # * #flatten: Returns an array that is a 1-dimensional flattening of `self`.
487
- # * #invert: Returns a hash with the each key-value pair inverted.
488
- #
489
472
  class Hash[unchecked out K, unchecked out V] < Object
490
473
  include Enumerable[[ K, V ]]
491
474
 
@@ -498,32 +481,38 @@ class Hash[unchecked out K, unchecked out V] < Object
498
481
  # <!--
499
482
  # rdoc-file=hash.c
500
483
  # - Hash[] -> new_empty_hash
501
- # - Hash[hash] -> new_hash
484
+ # - Hash[other_hash] -> new_hash
502
485
  # - Hash[ [*2_element_arrays] ] -> new_hash
503
486
  # - Hash[*objects] -> new_hash
504
487
  # -->
505
- # Returns a new `Hash` object populated with the given objects, if any. See
488
+ # Returns a new Hash object populated with the given objects, if any. See
506
489
  # Hash::new.
507
490
  #
508
- # With no argument, returns a new empty `Hash`.
491
+ # With no argument given, returns a new empty hash.
509
492
  #
510
- # When the single given argument is a `Hash`, returns a new `Hash` populated
511
- # with the entries from the given `Hash`, excluding the default value or proc.
493
+ # With a single argument `other_hash` given that is a hash, returns a new hash
494
+ # initialized with the entries from that hash (but not with its `default` or
495
+ # `default_proc`):
512
496
  #
513
497
  # h = {foo: 0, bar: 1, baz: 2}
514
- # Hash[h] # => {:foo=>0, :bar=>1, :baz=>2}
498
+ # Hash[h] # => {foo: 0, bar: 1, baz: 2}
515
499
  #
516
- # When the single given argument is an Array of 2-element Arrays, returns a new
517
- # `Hash` object wherein each 2-element array forms a key-value entry:
500
+ # With a single argument `2_element_arrays` given that is an array of 2-element
501
+ # arrays, returns a new hash wherein each given 2-element array forms a
502
+ # key-value entry:
518
503
  #
519
- # Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {:foo=>0, :bar=>1}
504
+ # Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {foo: 0, bar: 1}
520
505
  #
521
- # When the argument count is an even number; returns a new `Hash` object wherein
522
- # each successive pair of arguments has become a key-value entry:
506
+ # With an even number of arguments `objects` given, returns a new hash wherein
507
+ # each successive pair of arguments is a key-value entry:
523
508
  #
524
- # Hash[:foo, 0, :bar, 1] # => {:foo=>0, :bar=>1}
509
+ # Hash[:foo, 0, :bar, 1] # => {foo: 0, bar: 1}
525
510
  #
526
- # Raises an exception if the argument list does not conform to any of the above.
511
+ # Raises ArgumentError if the argument list does not conform to any of the
512
+ # above.
513
+ #
514
+ # See also [Methods for Creating a
515
+ # Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash).
527
516
  #
528
517
  def self.[]: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
529
518
  | [U, V] (Array[[ U, V ]]) -> ::Hash[U, V]
@@ -531,159 +520,221 @@ class Hash[unchecked out K, unchecked out V] < Object
531
520
 
532
521
  # <!--
533
522
  # rdoc-file=hash.c
534
- # - Hash.try_convert(obj) -> obj, new_hash, or nil
523
+ # - Hash.try_convert(object) -> object, new_hash, or nil
535
524
  # -->
536
- # If `obj` is a `Hash` object, returns `obj`.
537
- #
538
- # Otherwise if `obj` responds to `:to_hash`, calls `obj.to_hash` and returns the
539
- # result.
525
+ # If `object` is a hash, returns `object`.
540
526
  #
541
- # Returns `nil` if `obj` does not respond to `:to_hash`
527
+ # Otherwise if `object` responds to `:to_hash`, calls `object.to_hash`; returns
528
+ # the result if it is a hash, or raises TypeError if not.
542
529
  #
543
- # Raises an exception unless `obj.to_hash` returns a `Hash` object.
530
+ # Otherwise if `object` does not respond to `:to_hash`, returns `nil`.
544
531
  #
545
532
  def self.try_convert: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
546
533
  | (untyped) -> (::Hash[untyped, untyped] | nil)
547
534
 
548
535
  # <!--
549
536
  # rdoc-file=hash.c
550
- # - hash < other_hash -> true or false
537
+ # - self < other -> true or false
551
538
  # -->
552
- # Returns `true` if `hash` is a proper subset of `other_hash`, `false`
553
- # otherwise:
554
- # h1 = {foo: 0, bar: 1}
555
- # h2 = {foo: 0, bar: 1, baz: 2}
556
- # h1 < h2 # => true
557
- # h2 < h1 # => false
558
- # h1 < h1 # => false
539
+ # Returns whether the entries of `self` are a proper subset of the entries of
540
+ # `other`:
541
+ #
542
+ # h = {foo: 0, bar: 1}
543
+ # h < {foo: 0, bar: 1, baz: 2} # => true # Proper subset.
544
+ # h < {baz: 2, bar: 1, foo: 0} # => true # Order may differ.
545
+ # h < h # => false # Not a proper subset.
546
+ # h < {bar: 1, foo: 0} # => false # Not a proper subset.
547
+ # h < {foo: 0, bar: 1, baz: 2} # => false # Different key.
548
+ # h < {foo: 0, bar: 1, baz: 2} # => false # Different value.
549
+ #
550
+ # See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
551
+ #
552
+ # Raises TypeError if `other_hash` is not a hash and cannot be converted to a
553
+ # hash.
554
+ #
555
+ # Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
559
556
  #
560
557
  def <: [A, B] (::Hash[A, B]) -> bool
561
558
 
562
559
  # <!--
563
560
  # rdoc-file=hash.c
564
- # - hash <= other_hash -> true or false
561
+ # - self <= other -> true or false
565
562
  # -->
566
- # Returns `true` if `hash` is a subset of `other_hash`, `false` otherwise:
567
- # h1 = {foo: 0, bar: 1}
568
- # h2 = {foo: 0, bar: 1, baz: 2}
569
- # h1 <= h2 # => true
570
- # h2 <= h1 # => false
571
- # h1 <= h1 # => true
563
+ # Returns whether the entries of `self` are a subset of the entries of `other`:
564
+ #
565
+ # h0 = {foo: 0, bar: 1}
566
+ # h1 = {foo: 0, bar: 1, baz: 2}
567
+ # h0 <= h0 # => true
568
+ # h0 <= h1 # => true
569
+ # h1 <= h0 # => false
570
+ #
571
+ # See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
572
+ #
573
+ # Raises TypeError if `other_hash` is not a hash and cannot be converted to a
574
+ # hash.
575
+ #
576
+ # Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
572
577
  #
573
578
  def <=: [A, B] (::Hash[A, B]) -> bool
574
579
 
575
580
  # <!--
576
581
  # rdoc-file=hash.c
577
- # - hash == object -> true or false
582
+ # - self == object -> true or false
578
583
  # -->
584
+ # Returns whether `self` and `object` are equal.
585
+ #
579
586
  # Returns `true` if all of the following are true:
580
- # * `object` is a `Hash` object.
581
- # * `hash` and `object` have the same keys (regardless of order).
582
- # * For each key `key`, `hash[key] == object[key]`.
587
+ #
588
+ # * `object` is a `Hash` object (or can be converted to one).
589
+ # * `self` and `object` have the same keys (regardless of order).
590
+ # * For each key `key`, `self[key] == object[key]`.
583
591
  #
584
592
  # Otherwise, returns `false`.
585
593
  #
586
- # Equal:
587
- # h1 = {foo: 0, bar: 1, baz: 2}
588
- # h2 = {foo: 0, bar: 1, baz: 2}
589
- # h1 == h2 # => true
590
- # h3 = {baz: 2, bar: 1, foo: 0}
591
- # h1 == h3 # => true
594
+ # Examples:
595
+ #
596
+ # h = {foo: 0, bar: 1}
597
+ # h == {foo: 0, bar: 1} # => true # Equal entries (same order)
598
+ # h == {bar: 1, foo: 0} # => true # Equal entries (different order).
599
+ # h == 1 # => false # Object not a hash.
600
+ # h == {} # => false # Different number of entries.
601
+ # h == {foo: 0, bar: 1} # => false # Different key.
602
+ # h == {foo: 0, bar: 1} # => false # Different value.
603
+ #
604
+ # Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
592
605
  #
593
606
  def ==: (untyped other) -> bool
594
607
 
595
608
  # <!--
596
609
  # rdoc-file=hash.c
597
- # - hash > other_hash -> true or false
610
+ # - self > other_hash -> true or false
598
611
  # -->
599
- # Returns `true` if `hash` is a proper superset of `other_hash`, `false`
600
- # otherwise:
601
- # h1 = {foo: 0, bar: 1, baz: 2}
602
- # h2 = {foo: 0, bar: 1}
603
- # h1 > h2 # => true
604
- # h2 > h1 # => false
605
- # h1 > h1 # => false
612
+ # Returns `true` if the entries of `self` are a proper superset of the entries
613
+ # of `other_hash`, `false` otherwise:
614
+ #
615
+ # h = {foo: 0, bar: 1, baz: 2}
616
+ # h > {foo: 0, bar: 1} # => true # Proper superset.
617
+ # h > {bar: 1, foo: 0} # => true # Order may differ.
618
+ # h > h # => false # Not a proper superset.
619
+ # h > {baz: 2, bar: 1, foo: 0} # => false # Not a proper superset.
620
+ # h > {foo: 0, bar: 1} # => false # Different key.
621
+ # h > {foo: 0, bar: 1} # => false # Different value.
622
+ #
623
+ # See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
624
+ #
625
+ # Raises TypeError if `other_hash` is not a hash and cannot be converted to a
626
+ # hash.
627
+ #
628
+ # Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
606
629
  #
607
630
  def >: [A, B] (::Hash[A, B]) -> bool
608
631
 
609
632
  # <!--
610
633
  # rdoc-file=hash.c
611
- # - hash >= other_hash -> true or false
634
+ # - self >= other_hash -> true or false
612
635
  # -->
613
- # Returns `true` if `hash` is a superset of `other_hash`, `false` otherwise:
614
- # h1 = {foo: 0, bar: 1, baz: 2}
615
- # h2 = {foo: 0, bar: 1}
616
- # h1 >= h2 # => true
617
- # h2 >= h1 # => false
618
- # h1 >= h1 # => true
636
+ # Returns `true` if the entries of `self` are a superset of the entries of
637
+ # `other_hash`, `false` otherwise:
638
+ #
639
+ # h0 = {foo: 0, bar: 1, baz: 2}
640
+ # h1 = {foo: 0, bar: 1}
641
+ # h0 >= h1 # => true
642
+ # h0 >= h0 # => true
643
+ # h1 >= h0 # => false
644
+ #
645
+ # See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
646
+ #
647
+ # Raises TypeError if `other_hash` is not a hash and cannot be converted to a
648
+ # hash.
649
+ #
650
+ # Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
619
651
  #
620
652
  def >=: [A, B] (::Hash[A, B]) -> bool
621
653
 
622
654
  # <!--
623
655
  # rdoc-file=hash.c
624
- # - hash[key] -> value
656
+ # - self[key] -> object
625
657
  # -->
626
- # Returns the value associated with the given `key`, if found:
627
- # h = {foo: 0, bar: 1, baz: 2}
628
- # h[:foo] # => 0
658
+ # Searches for a hash key equivalent to the given `key`; see [Hash Key
659
+ # Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
629
660
  #
630
- # If `key` is not found, returns a default value (see [Default
631
- # Values](rdoc-ref:Hash@Default+Values)):
632
- # h = {foo: 0, bar: 1, baz: 2}
633
- # h[:nosuch] # => nil
661
+ # If the key is found, returns its value:
662
+ #
663
+ # {foo: 0, bar: 1, baz: 2}
664
+ # h[:bar] # => 1
665
+ #
666
+ # Otherwise, returns a default value (see [Hash
667
+ # Default](rdoc-ref:Hash@Hash+Default)).
668
+ #
669
+ # Related: #[]=; see also [Methods for
670
+ # Fetching](rdoc-ref:Hash@Methods+for+Fetching).
634
671
  #
635
672
  def []: %a{implicitly-returns-nil} (K arg0) -> V
636
673
 
637
674
  # <!--
638
675
  # rdoc-file=hash.c
639
- # - hash[key] = value -> value
640
- # - hash.store(key, value)
676
+ # - self[key] = object -> object
641
677
  # -->
642
- # Associates the given `value` with the given `key`; returns `value`.
678
+ # Associates the given `object` with the given `key`; returns `object`.
679
+ #
680
+ # Searches for a hash key equivalent to the given `key`; see [Hash Key
681
+ # Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
682
+ #
683
+ # If the key is found, replaces its value with the given `object`; the ordering
684
+ # is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
643
685
  #
644
- # If the given `key` exists, replaces its value with the given `value`; the
645
- # ordering is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
646
686
  # h = {foo: 0, bar: 1}
647
687
  # h[:foo] = 2 # => 2
648
- # h.store(:bar, 3) # => 3
649
- # h # => {:foo=>2, :bar=>3}
688
+ # h[:foo] # => 2
689
+ #
690
+ # If `key` is not found, creates a new entry for the given `key` and `object`;
691
+ # the new entry is last in the order (see [Entry
692
+ # Order](rdoc-ref:Hash@Entry+Order)):
650
693
  #
651
- # If `key` does not exist, adds the `key` and `value`; the new entry is last in
652
- # the order (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
653
694
  # h = {foo: 0, bar: 1}
654
695
  # h[:baz] = 2 # => 2
655
- # h.store(:bat, 3) # => 3
656
- # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
696
+ # h[:baz] # => 2
697
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
698
+ #
699
+ # Related: #[]; see also [Methods for
700
+ # Assigning](rdoc-ref:Hash@Methods+for+Assigning).
657
701
  #
658
702
  def []=: (K arg0, V arg1) -> V
659
703
 
660
704
  # <!--
661
705
  # rdoc-file=hash.c
662
- # - hash.any? -> true or false
663
- # - hash.any?(object) -> true or false
664
- # - hash.any? {|key, value| ... } -> true or false
706
+ # - any? -> true or false
707
+ # - any?(entry) -> true or false
708
+ # - any? {|key, value| ... } -> true or false
665
709
  # -->
666
710
  # Returns `true` if any element satisfies a given criterion; `false` otherwise.
667
711
  #
668
- # If `self` has no element, returns `false` and argument or block are not used.
712
+ # If `self` has no element, returns `false` and argument or block are not used;
713
+ # otherwise behaves as below.
669
714
  #
670
- # With no argument and no block, returns `true` if `self` is non-empty; `false`
671
- # if empty.
715
+ # With no argument and no block, returns `true` if `self` is non-empty, `false`
716
+ # otherwise.
717
+ #
718
+ # With argument `entry` and no block, returns `true` if for any key `key`
719
+ # `self.assoc(key) == entry`, `false` otherwise:
672
720
  #
673
- # With argument `object` and no block, returns `true` if for any key `key`
674
- # `h.assoc(key) == object`:
675
721
  # h = {foo: 0, bar: 1, baz: 2}
722
+ # h.assoc(:bar) # => [:bar, 1]
676
723
  # h.any?([:bar, 1]) # => true
677
724
  # h.any?([:bar, 0]) # => false
678
- # h.any?([:baz, 1]) # => false
679
725
  #
680
- # With no argument and a block, calls the block with each key-value pair;
681
- # returns `true` if the block returns any truthy value, `false` otherwise:
726
+ # With no argument and a block given, calls the block with each key-value pair;
727
+ # returns `true` if the block returns a truthy value, `false` otherwise:
728
+ #
682
729
  # h = {foo: 0, bar: 1, baz: 2}
683
730
  # h.any? {|key, value| value < 3 } # => true
684
731
  # h.any? {|key, value| value > 3 } # => false
685
732
  #
686
- # Related: Enumerable#any?
733
+ # With both argument `entry` and a block given, issues a warning and ignores the
734
+ # block.
735
+ #
736
+ # Related: Enumerable#any? (which this method overrides); see also [Methods for
737
+ # Fetching](rdoc-ref:Hash@Methods+for+Fetching).
687
738
  #
688
739
  def any?: () -> bool
689
740
  | (untyped pattern) -> bool
@@ -691,81 +742,99 @@ class Hash[unchecked out K, unchecked out V] < Object
691
742
 
692
743
  # <!--
693
744
  # rdoc-file=hash.c
694
- # - hash.assoc(key) -> new_array or nil
745
+ # - assoc(key) -> entry or nil
695
746
  # -->
696
- # If the given `key` is found, returns a 2-element Array containing that key and
697
- # its value:
747
+ # If the given `key` is found, returns its entry as a 2-element array containing
748
+ # that key and its value:
749
+ #
698
750
  # h = {foo: 0, bar: 1, baz: 2}
699
751
  # h.assoc(:bar) # => [:bar, 1]
700
752
  #
701
- # Returns `nil` if key `key` is not found.
753
+ # Returns `nil` if the key is not found.
754
+ #
755
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
702
756
  #
703
757
  def assoc: (K arg0) -> [ K, V ]?
704
758
 
705
759
  # <!--
706
760
  # rdoc-file=hash.c
707
- # - hash.clear -> self
761
+ # - clear -> self
708
762
  # -->
709
- # Removes all hash entries; returns `self`.
763
+ # Removes all entries from `self`; returns emptied `self`.
764
+ #
765
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
710
766
  #
711
767
  def clear: () -> self
712
768
 
713
769
  # <!--
714
770
  # rdoc-file=hash.c
715
- # - hash.compact -> new_hash
771
+ # - compact -> new_hash
716
772
  # -->
717
773
  # Returns a copy of `self` with all `nil`-valued entries removed:
774
+ #
718
775
  # h = {foo: 0, bar: nil, baz: 2, bat: nil}
719
- # h1 = h.compact
720
- # h1 # => {:foo=>0, :baz=>2}
776
+ # h.compact # => {foo: 0, baz: 2}
777
+ #
778
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
721
779
  #
722
780
  def compact: () -> ::Hash[K, V]
723
781
 
724
782
  # <!--
725
783
  # rdoc-file=hash.c
726
- # - hash.compact! -> self or nil
784
+ # - compact! -> self or nil
727
785
  # -->
728
- # Returns `self` with all its `nil`-valued entries removed (in place):
786
+ # If `self` contains any `nil`-valued entries, returns `self` with all
787
+ # `nil`-valued entries removed; returns `nil` otherwise:
788
+ #
729
789
  # h = {foo: 0, bar: nil, baz: 2, bat: nil}
730
- # h.compact! # => {:foo=>0, :baz=>2}
790
+ # h.compact!
791
+ # h # => {foo: 0, baz: 2}
792
+ # h.compact! # => nil
731
793
  #
732
- # Returns `nil` if no entries were removed.
794
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
733
795
  #
734
796
  def compact!: () -> self?
735
797
 
736
798
  # <!--
737
799
  # rdoc-file=hash.c
738
- # - hash.compare_by_identity -> self
800
+ # - compare_by_identity -> self
739
801
  # -->
740
- # Sets `self` to consider only identity in comparing keys; two keys are
741
- # considered the same only if they are the same object; returns `self`.
802
+ # Sets `self` to compare keys using *identity* (rather than mere *equality*);
803
+ # returns `self`:
804
+ #
805
+ # By default, two keys are considered to be the same key if and only if they are
806
+ # *equal* objects (per method #eql?):
742
807
  #
743
- # By default, these two object are considered to be the same key, so `s1` will
744
- # overwrite `s0`:
745
- # s0 = 'x'
746
- # s1 = 'x'
747
808
  # h = {}
748
- # h.compare_by_identity? # => false
749
- # h[s0] = 0
750
- # h[s1] = 1
809
+ # h['x'] = 0
810
+ # h['x'] = 1 # Overwrites.
751
811
  # h # => {"x"=>1}
752
812
  #
753
- # After calling #compare_by_identity, the keys are considered to be different,
754
- # and therefore do not overwrite each other:
755
- # h = {}
756
- # h.compare_by_identity # => {}
757
- # h.compare_by_identity? # => true
758
- # h[s0] = 0
759
- # h[s1] = 1
760
- # h # => {"x"=>0, "x"=>1}
813
+ # When this method has been called, two keys are considered to be the same key
814
+ # if and only if they are the *same* object:
815
+ #
816
+ # h.compare_by_identity
817
+ # h['x'] = 2 # Does not overwrite.
818
+ # h # => {"x"=>1, "x"=>2}
819
+ #
820
+ # Related: #compare_by_identity?; see also [Methods for
821
+ # Comparing](rdoc-ref:Hash@Methods+for+Comparing).
761
822
  #
762
823
  def compare_by_identity: () -> self
763
824
 
764
825
  # <!--
765
826
  # rdoc-file=hash.c
766
- # - hash.compare_by_identity? -> true or false
827
+ # - compare_by_identity? -> true or false
767
828
  # -->
768
- # Returns `true` if #compare_by_identity has been called, `false` otherwise.
829
+ # Returns whether #compare_by_identity has been called:
830
+ #
831
+ # h = {}
832
+ # h.compare_by_identity? # => false
833
+ # h.compare_by_identity
834
+ # h.compare_by_identity? # => true
835
+ #
836
+ # Related: #compare_by_identity; see also [Methods for
837
+ # Comparing](rdoc-ref:Hash@Methods+for+Comparing).
769
838
  #
770
839
  def compare_by_identity?: () -> bool
771
840
 
@@ -778,12 +847,12 @@ class Hash[unchecked out K, unchecked out V] < Object
778
847
 
779
848
  # <!--
780
849
  # rdoc-file=hash.c
781
- # - hash.default -> object
782
- # - hash.default(key) -> object
850
+ # - default -> object
851
+ # - default(key) -> object
783
852
  # -->
784
853
  # Returns the default value for the given `key`. The returned value will be
785
- # determined either by the default proc or by the default value. See [Default
786
- # Values](rdoc-ref:Hash@Default+Values).
854
+ # determined either by the default proc or by the default value. See [Hash
855
+ # Default](rdoc-ref:Hash@Hash+Default).
787
856
  #
788
857
  # With no argument, returns the current default value:
789
858
  # h = {}
@@ -799,7 +868,7 @@ class Hash[unchecked out K, unchecked out V] < Object
799
868
 
800
869
  # <!--
801
870
  # rdoc-file=hash.c
802
- # - hash.default = value -> object
871
+ # - default = value -> object
803
872
  # -->
804
873
  # Sets the default value to `value`; returns `value`:
805
874
  # h = {}
@@ -807,16 +876,16 @@ class Hash[unchecked out K, unchecked out V] < Object
807
876
  # h.default = false # => false
808
877
  # h.default # => false
809
878
  #
810
- # See [Default Values](rdoc-ref:Hash@Default+Values).
879
+ # See [Hash Default](rdoc-ref:Hash@Hash+Default).
811
880
  #
812
881
  def default=: (V arg0) -> V
813
882
 
814
883
  # <!--
815
884
  # rdoc-file=hash.c
816
- # - hash.default_proc -> proc or nil
885
+ # - default_proc -> proc or nil
817
886
  # -->
818
- # Returns the default proc for `self` (see [Default
819
- # Values](rdoc-ref:Hash@Default+Values)):
887
+ # Returns the default proc for `self` (see [Hash
888
+ # Default](rdoc-ref:Hash@Hash+Default)):
820
889
  # h = {}
821
890
  # h.default_proc # => nil
822
891
  # h.default_proc = proc {|hash, key| "Default value for #{key}" }
@@ -826,10 +895,10 @@ class Hash[unchecked out K, unchecked out V] < Object
826
895
 
827
896
  # <!--
828
897
  # rdoc-file=hash.c
829
- # - hash.default_proc = proc -> proc
898
+ # - default_proc = proc -> proc
830
899
  # -->
831
- # Sets the default proc for `self` to `proc` (see [Default
832
- # Values](rdoc-ref:Hash@Default+Values)):
900
+ # Sets the default proc for `self` to `proc` (see [Hash
901
+ # Default](rdoc-ref:Hash@Hash+Default)):
833
902
  # h = {}
834
903
  # h.default_proc # => nil
835
904
  # h.default_proc = proc { |hash, key| "Default value for #{key}" }
@@ -841,208 +910,200 @@ class Hash[unchecked out K, unchecked out V] < Object
841
910
 
842
911
  # <!--
843
912
  # rdoc-file=hash.c
844
- # - hash.delete(key) -> value or nil
845
- # - hash.delete(key) {|key| ... } -> object
913
+ # - delete(key) -> value or nil
914
+ # - delete(key) {|key| ... } -> object
846
915
  # -->
847
- # Deletes the entry for the given `key` and returns its associated value.
916
+ # If an entry for the given `key` is found, deletes the entry and returns its
917
+ # associated value; otherwise returns `nil` or calls the given block.
918
+ #
919
+ # With no block given and `key` found, deletes the entry and returns its value:
848
920
  #
849
- # If no block is given and `key` is found, deletes the entry and returns the
850
- # associated value:
851
921
  # h = {foo: 0, bar: 1, baz: 2}
852
922
  # h.delete(:bar) # => 1
853
- # h # => {:foo=>0, :baz=>2}
923
+ # h # => {foo: 0, baz: 2}
924
+ #
925
+ # With no block given and `key` not found, returns `nil`.
854
926
  #
855
- # If no block given and `key` is not found, returns `nil`.
927
+ # With a block given and `key` found, ignores the block, deletes the entry, and
928
+ # returns its value:
856
929
  #
857
- # If a block is given and `key` is found, ignores the block, deletes the entry,
858
- # and returns the associated value:
859
930
  # h = {foo: 0, bar: 1, baz: 2}
860
931
  # h.delete(:baz) { |key| raise 'Will never happen'} # => 2
861
- # h # => {:foo=>0, :bar=>1}
932
+ # h # => {foo: 0, bar: 1}
862
933
  #
863
- # If a block is given and `key` is not found, calls the block and returns the
934
+ # With a block given and `key` not found, calls the block and returns the
864
935
  # block's return value:
936
+ #
865
937
  # h = {foo: 0, bar: 1, baz: 2}
866
938
  # h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
867
- # h # => {:foo=>0, :bar=>1, :baz=>2}
939
+ # h # => {foo: 0, bar: 1, baz: 2}
940
+ #
941
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
868
942
  #
869
943
  def delete: (K arg0) -> V?
870
944
  | [U] (K arg0) { (K arg0) -> U } -> (U | V)
871
945
 
872
946
  # <!--
873
947
  # rdoc-file=hash.c
874
- # - hash.delete_if {|key, value| ... } -> self
875
- # - hash.delete_if -> new_enumerator
948
+ # - delete_if {|key, value| ... } -> self
949
+ # - delete_if -> new_enumerator
876
950
  # -->
877
- # If a block given, calls the block with each key-value pair; deletes each entry
878
- # for which the block returns a truthy value; returns `self`:
879
- # h = {foo: 0, bar: 1, baz: 2}
880
- # h.delete_if {|key, value| value > 0 } # => {:foo=>0}
951
+ # With a block given, calls the block with each key-value pair, deletes each
952
+ # entry for which the block returns a truthy value, and returns `self`:
881
953
  #
882
- # If no block given, returns a new Enumerator:
883
954
  # h = {foo: 0, bar: 1, baz: 2}
884
- # e = h.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
885
- # e.each { |key, value| value > 0 } # => {:foo=>0}
955
+ # h.delete_if {|key, value| value > 0 } # => {foo: 0}
956
+ #
957
+ # With no block given, returns a new Enumerator.
958
+ #
959
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
886
960
  #
887
961
  def delete_if: () { (K, V) -> boolish } -> self
888
962
  | () -> ::Enumerator[[ K, V ], self]
889
963
 
890
964
  # <!--
891
965
  # rdoc-file=hash.c
892
- # - hash.dig(key, *identifiers) -> object
966
+ # - dig(key, *identifiers) -> object
893
967
  # -->
894
- # Finds and returns the object in nested objects that is specified by `key` and
895
- # `identifiers`. The nested objects may be instances of various classes. See
896
- # [Dig Methods](rdoc-ref:dig_methods.rdoc).
968
+ # Finds and returns an object found in nested objects, as specified by `key` and
969
+ # `identifiers`.
970
+ #
971
+ # The nested objects may be instances of various classes. See [Dig
972
+ # Methods](rdoc-ref:dig_methods.rdoc).
973
+ #
974
+ # Nested hashes:
897
975
  #
898
- # Nested Hashes:
899
976
  # h = {foo: {bar: {baz: 2}}}
900
- # h.dig(:foo) # => {:bar=>{:baz=>2}}
901
- # h.dig(:foo, :bar) # => {:baz=>2}
977
+ # h.dig(:foo) # => {bar: {baz: 2}}
978
+ # h.dig(:foo, :bar) # => {baz: 2}
902
979
  # h.dig(:foo, :bar, :baz) # => 2
903
980
  # h.dig(:foo, :bar, :BAZ) # => nil
904
981
  #
905
- # Nested Hashes and Arrays:
982
+ # Nested hashes and arrays:
983
+ #
906
984
  # h = {foo: {bar: [:a, :b, :c]}}
907
985
  # h.dig(:foo, :bar, 2) # => :c
908
986
  #
909
- # This method will use the [default values](rdoc-ref:Hash@Default+Values) for
910
- # keys that are not present:
987
+ # If no such object is found, returns the [hash
988
+ # default](rdoc-ref:Hash@Hash+Default):
989
+ #
911
990
  # h = {foo: {bar: [:a, :b, :c]}}
912
991
  # h.dig(:hello) # => nil
913
992
  # h.default_proc = -> (hash, _key) { hash }
914
- # h.dig(:hello, :world) # => h
915
- # h.dig(:hello, :world, :foo, :bar, 2) # => :c
993
+ # h.dig(:hello, :world)
994
+ # # => {:foo=>{:bar=>[:a, :b, :c]}}
995
+ #
996
+ # Related: [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
916
997
  #
917
998
  def dig: (K, *untyped) -> untyped
918
999
 
919
1000
  # <!-- rdoc-file=hash.c -->
920
- # Calls the given block with each key-value pair; returns `self`:
1001
+ # With a block given, calls the block with each key-value pair; returns `self`:
1002
+ #
921
1003
  # h = {foo: 0, bar: 1, baz: 2}
922
- # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
1004
+ # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo: 0, bar: 1, baz: 2}
923
1005
  #
924
1006
  # Output:
1007
+ #
925
1008
  # foo: 0
926
1009
  # bar: 1
927
1010
  # baz: 2
928
1011
  #
929
- # Returns a new Enumerator if no block given:
930
- # h = {foo: 0, bar: 1, baz: 2}
931
- # e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
932
- # h1 = e.each {|key, value| puts "#{key}: #{value}"}
933
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1012
+ # With no block given, returns a new Enumerator.
934
1013
  #
935
- # Output:
936
- # foo: 0
937
- # bar: 1
938
- # baz: 2
1014
+ # Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
939
1015
  #
940
1016
  def each: () { ([ K, V ] arg0) -> untyped } -> self
941
1017
  | () -> ::Enumerator[[ K, V ], self]
942
1018
 
943
1019
  # <!--
944
1020
  # rdoc-file=hash.c
945
- # - hash.each_key {|key| ... } -> self
946
- # - hash.each_key -> new_enumerator
1021
+ # - each_key {|key| ... } -> self
1022
+ # - each_key -> new_enumerator
947
1023
  # -->
948
- # Calls the given block with each key; returns `self`:
1024
+ # With a block given, calls the block with each key; returns `self`:
1025
+ #
949
1026
  # h = {foo: 0, bar: 1, baz: 2}
950
- # h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
1027
+ # h.each_key {|key| puts key } # => {foo: 0, bar: 1, baz: 2}
951
1028
  #
952
1029
  # Output:
953
1030
  # foo
954
1031
  # bar
955
1032
  # baz
956
1033
  #
957
- # Returns a new Enumerator if no block given:
958
- # h = {foo: 0, bar: 1, baz: 2}
959
- # e = h.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>
960
- # h1 = e.each {|key| puts key }
961
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1034
+ # With no block given, returns a new Enumerator.
962
1035
  #
963
- # Output:
964
- # foo
965
- # bar
966
- # baz
1036
+ # Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
967
1037
  #
968
1038
  def each_key: () { (K arg0) -> untyped } -> ::Hash[K, V]
969
1039
  | () -> ::Enumerator[K, self]
970
1040
 
971
1041
  # <!--
972
1042
  # rdoc-file=hash.c
973
- # - hash.each {|key, value| ... } -> self
974
- # - hash.each_pair {|key, value| ... } -> self
975
- # - hash.each -> new_enumerator
976
- # - hash.each_pair -> new_enumerator
1043
+ # - each_pair {|key, value| ... } -> self
1044
+ # - each_pair -> new_enumerator
977
1045
  # -->
978
- # Calls the given block with each key-value pair; returns `self`:
1046
+ # With a block given, calls the block with each key-value pair; returns `self`:
1047
+ #
979
1048
  # h = {foo: 0, bar: 1, baz: 2}
980
- # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
1049
+ # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo: 0, bar: 1, baz: 2}
981
1050
  #
982
1051
  # Output:
1052
+ #
983
1053
  # foo: 0
984
1054
  # bar: 1
985
1055
  # baz: 2
986
1056
  #
987
- # Returns a new Enumerator if no block given:
988
- # h = {foo: 0, bar: 1, baz: 2}
989
- # e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
990
- # h1 = e.each {|key, value| puts "#{key}: #{value}"}
991
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1057
+ # With no block given, returns a new Enumerator.
992
1058
  #
993
- # Output:
994
- # foo: 0
995
- # bar: 1
996
- # baz: 2
1059
+ # Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
997
1060
  #
998
1061
  alias each_pair each
999
1062
 
1000
1063
  # <!--
1001
1064
  # rdoc-file=hash.c
1002
- # - hash.each_value {|value| ... } -> self
1003
- # - hash.each_value -> new_enumerator
1065
+ # - each_value {|value| ... } -> self
1066
+ # - each_value -> new_enumerator
1004
1067
  # -->
1005
- # Calls the given block with each value; returns `self`:
1068
+ # With a block given, calls the block with each value; returns `self`:
1069
+ #
1006
1070
  # h = {foo: 0, bar: 1, baz: 2}
1007
- # h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
1071
+ # h.each_value {|value| puts value } # => {foo: 0, bar: 1, baz: 2}
1008
1072
  #
1009
1073
  # Output:
1010
1074
  # 0
1011
1075
  # 1
1012
1076
  # 2
1013
1077
  #
1014
- # Returns a new Enumerator if no block given:
1015
- # h = {foo: 0, bar: 1, baz: 2}
1016
- # e = h.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>
1017
- # h1 = e.each {|value| puts value }
1018
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1078
+ # With no block given, returns a new Enumerator.
1019
1079
  #
1020
- # Output:
1021
- # 0
1022
- # 1
1023
- # 2
1080
+ # Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
1024
1081
  #
1025
1082
  def each_value: () { (V arg0) -> untyped } -> self
1026
1083
  | () -> ::Enumerator[V, self]
1027
1084
 
1028
1085
  # <!--
1029
1086
  # rdoc-file=hash.c
1030
- # - hash.empty? -> true or false
1087
+ # - empty? -> true or false
1031
1088
  # -->
1032
1089
  # Returns `true` if there are no hash entries, `false` otherwise:
1090
+ #
1033
1091
  # {}.empty? # => true
1034
- # {foo: 0, bar: 1, baz: 2}.empty? # => false
1092
+ # {foo: 0}.empty? # => false
1093
+ #
1094
+ # Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1035
1095
  #
1036
1096
  def empty?: () -> bool
1037
1097
 
1038
1098
  # <!--
1039
1099
  # rdoc-file=hash.c
1040
- # - hash.eql?(object) -> true or false
1100
+ # - eql?(object) -> true or false
1041
1101
  # -->
1042
1102
  # Returns `true` if all of the following are true:
1043
- # * `object` is a `Hash` object.
1044
- # * `hash` and `object` have the same keys (regardless of order).
1045
- # * For each key `key`, `h[key].eql?(object[key])`.
1103
+ #
1104
+ # * The given `object` is a `Hash` object.
1105
+ # * `self` and `object` have the same keys (regardless of order).
1106
+ # * For each key `key`, `self[key].eql?(object[key])`.
1046
1107
  #
1047
1108
  # Otherwise, returns `false`.
1048
1109
  #
@@ -1052,378 +1113,427 @@ class Hash[unchecked out K, unchecked out V] < Object
1052
1113
  # h3 = {baz: 2, bar: 1, foo: 0}
1053
1114
  # h1.eql? h3 # => true
1054
1115
  #
1116
+ # Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1117
+ #
1055
1118
  def eql?: (untyped) -> bool
1056
1119
 
1057
1120
  # <!--
1058
1121
  # rdoc-file=hash.c
1059
- # - hsh.except(*keys) -> a_hash
1122
+ # - except(*keys) -> new_hash
1060
1123
  # -->
1061
- # Returns a new `Hash` excluding entries for the given `keys`:
1062
- # h = { a: 100, b: 200, c: 300 }
1063
- # h.except(:a) #=> {:b=>200, :c=>300}
1124
+ # Returns a copy of `self` that excludes entries for the given `keys`; any
1125
+ # `keys` that are not found are ignored:
1064
1126
  #
1065
- # Any given `keys` that are not found are ignored.
1127
+ # h = {foo:0, bar: 1, baz: 2} # => {:foo=>0, :bar=>1, :baz=>2}
1128
+ # h.except(:baz, :foo) # => {:bar=>1}
1129
+ # h.except(:bar, :nosuch) # => {:foo=>0, :baz=>2}
1130
+ #
1131
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1066
1132
  #
1067
1133
  def except: (*K) -> ::Hash[K, V]
1068
1134
 
1069
1135
  # <!--
1070
1136
  # rdoc-file=hash.c
1071
- # - hash.fetch(key) -> object
1072
- # - hash.fetch(key, default_value) -> object
1073
- # - hash.fetch(key) {|key| ... } -> object
1137
+ # - fetch(key) -> object
1138
+ # - fetch(key, default_value) -> object
1139
+ # - fetch(key) {|key| ... } -> object
1074
1140
  # -->
1075
- # Returns the value for the given `key`, if found.
1141
+ # With no block given, returns the value for the given `key`, if found;
1142
+ #
1076
1143
  # h = {foo: 0, bar: 1, baz: 2}
1077
- # h.fetch(:bar) # => 1
1144
+ # h.fetch(:bar) # => 1
1078
1145
  #
1079
- # If `key` is not found and no block was given, returns `default_value`:
1080
- # {}.fetch(:nosuch, :default) # => :default
1146
+ # If the key is not found, returns `default_value`, if given, or raises KeyError
1147
+ # otherwise:
1081
1148
  #
1082
- # If `key` is not found and a block was given, yields `key` to the block and
1083
- # returns the block's return value:
1084
- # {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
1149
+ # h.fetch(:nosuch, :default) # => :default
1150
+ # h.fetch(:nosuch) # Raises KeyError.
1085
1151
  #
1086
- # Raises KeyError if neither `default_value` nor a block was given.
1152
+ # With a block given, calls the block with `key` and returns the block's return
1153
+ # value:
1154
+ #
1155
+ # {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
1087
1156
  #
1088
1157
  # Note that this method does not use the values of either #default or
1089
1158
  # #default_proc.
1090
1159
  #
1160
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
1161
+ #
1091
1162
  def fetch: (K arg0) -> V
1092
1163
  | [X] (K arg0, X arg1) -> (V | X)
1093
1164
  | [X] (K arg0) { (K arg0) -> X } -> (V | X)
1094
1165
 
1095
1166
  # <!--
1096
1167
  # rdoc-file=hash.c
1097
- # - hash.fetch_values(*keys) -> new_array
1098
- # - hash.fetch_values(*keys) {|key| ... } -> new_array
1168
+ # - fetch_values(*keys) -> new_array
1169
+ # - fetch_values(*keys) {|key| ... } -> new_array
1099
1170
  # -->
1100
- # Returns a new Array containing the values associated with the given keys
1101
- # *keys:
1171
+ # When all given `keys` are found, returns a new array containing the values
1172
+ # associated with the given `keys`:
1173
+ #
1102
1174
  # h = {foo: 0, bar: 1, baz: 2}
1103
1175
  # h.fetch_values(:baz, :foo) # => [2, 0]
1104
1176
  #
1105
- # Returns a new empty Array if no arguments given.
1177
+ # When any given `keys` are not found and a block is given, calls the block with
1178
+ # each unfound key and uses the block's return value as the value for that key:
1106
1179
  #
1107
- # When a block is given, calls the block with each missing key, treating the
1108
- # block's return value as the value for that key:
1109
- # h = {foo: 0, bar: 1, baz: 2}
1110
- # values = h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
1111
- # values # => [1, 0, "bad", "bam"]
1180
+ # h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
1181
+ # # => [1, 0, "bad", "bam"]
1112
1182
  #
1113
- # When no block is given, raises an exception if any given key is not found.
1183
+ # When any given `keys` are not found and no block is given, raises KeyError.
1184
+ #
1185
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
1114
1186
  #
1115
1187
  def fetch_values: (*K) -> ::Array[V]
1116
1188
  | [X] (*K) { (K) -> X } -> ::Array[V | X]
1117
1189
 
1118
1190
  # <!-- rdoc-file=hash.c -->
1119
- # Returns a new `Hash` object whose entries are those for which the block
1120
- # returns a truthy value:
1121
- # h = {foo: 0, bar: 1, baz: 2}
1122
- # h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
1191
+ # With a block given, calls the block with each entry's key and value; returns a
1192
+ # new hash whose entries are those for which the block returns a truthy value:
1123
1193
  #
1124
- # Returns a new Enumerator if no block given:
1125
1194
  # h = {foo: 0, bar: 1, baz: 2}
1126
- # e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
1127
- # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
1195
+ # h.select {|key, value| value < 2 } # => {foo: 0, bar: 1}
1196
+ #
1197
+ # With no block given, returns a new Enumerator.
1198
+ #
1199
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1128
1200
  #
1129
1201
  def filter: () { (K, V) -> boolish } -> ::Hash[K, V]
1130
1202
  | () -> ::Enumerator[[ K, V ], ::Hash[K, V]]
1131
1203
 
1132
1204
  # <!-- rdoc-file=hash.c -->
1133
- # Returns `self`, whose entries are those for which the block returns a truthy
1134
- # value:
1135
- # h = {foo: 0, bar: 1, baz: 2}
1136
- # h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
1205
+ # With a block given, calls the block with each entry's key and value; removes
1206
+ # from `self` each entry for which the block returns `false` or `nil`.
1137
1207
  #
1138
- # Returns `nil` if no entries were removed.
1208
+ # Returns `self` if any entries were removed, `nil` otherwise:
1139
1209
  #
1140
- # Returns a new Enumerator if no block given:
1141
1210
  # h = {foo: 0, bar: 1, baz: 2}
1142
- # e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
1143
- # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
1211
+ # h.select! {|key, value| value < 2 } # => {foo: 0, bar: 1}
1212
+ # h.select! {|key, value| value < 2 } # => nil
1213
+ #
1214
+ # With no block given, returns a new Enumerator.
1215
+ #
1216
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1144
1217
  #
1145
1218
  def filter!: () { (K, V) -> boolish } -> self?
1146
1219
  | () -> ::Enumerator[[ K, V ], self?]
1147
1220
 
1148
1221
  # <!--
1149
1222
  # rdoc-file=hash.c
1150
- # - hash.flatten -> new_array
1151
- # - hash.flatten(level) -> new_array
1223
+ # - flatten(depth = 1) -> new_array
1152
1224
  # -->
1153
- # Returns a new Array object that is a 1-dimensional flattening of `self`.
1225
+ # With positive integer `depth`, returns a new array that is a recursive
1226
+ # flattening of `self` to the given `depth`.
1227
+ #
1228
+ # At each level of recursion:
1229
+ #
1230
+ # * Each element whose value is an array is "flattened" (that is, replaced by
1231
+ # its individual array elements); see Array#flatten.
1232
+ # * Each element whose value is not an array is unchanged. even if the value
1233
+ # is an object that has instance method flatten (such as a hash).
1234
+ #
1235
+ # Examples; note that entry `foo: {bar: 1, baz: 2}` is never flattened.
1236
+ #
1237
+ # h = {foo: {bar: 1, baz: 2}, bat: [:bam, [:bap, [:bah]]]}
1238
+ # h.flatten(1) # => [:foo, {:bar=>1, :baz=>2}, :bat, [:bam, [:bap, [:bah]]]]
1239
+ # h.flatten(2) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, [:bap, [:bah]]]
1240
+ # h.flatten(3) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, [:bah]]
1241
+ # h.flatten(4) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
1242
+ # h.flatten(5) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
1154
1243
  #
1155
- # ---
1244
+ # With negative integer `depth`, flattens all levels:
1156
1245
  #
1157
- # By default, nested Arrays are not flattened:
1158
- # h = {foo: 0, bar: [:bat, 3], baz: 2}
1159
- # h.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]
1246
+ # h.flatten(-1) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
1160
1247
  #
1161
- # Takes the depth of recursive flattening from Integer argument `level`:
1162
- # h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
1163
- # h.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
1164
- # h.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
1165
- # h.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
1166
- # h.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]
1248
+ # With `depth` zero, returns the equivalent of #to_a:
1167
1249
  #
1168
- # When `level` is negative, flattens all nested Arrays:
1169
- # h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
1170
- # h.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
1171
- # h.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]
1250
+ # h.flatten(0) # => [[:foo, {:bar=>1, :baz=>2}], [:bat, [:bam, [:bap, [:bah]]]]]
1172
1251
  #
1173
- # When `level` is zero, returns the equivalent of #to_a :
1174
- # h = {foo: 0, bar: [:bat, 3], baz: 2}
1175
- # h.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
1176
- # h.flatten(0) == h.to_a # => true
1252
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1177
1253
  #
1178
1254
  def flatten: () -> ::Array[K | V]
1179
1255
  | (1 level) -> ::Array[K | V]
1180
1256
  | (Integer level) -> Array[untyped]
1181
1257
 
1182
1258
  # <!-- rdoc-file=hash.c -->
1183
- # Returns `true` if `key` is a key in `self`, otherwise `false`.
1259
+ # Returns whether `key` is a key in `self`:
1260
+ #
1261
+ # h = {foo: 0, bar: 1, baz: 2}
1262
+ # h.include?(:bar) # => true
1263
+ # h.include?(:BAR) # => false
1264
+ #
1265
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1184
1266
  #
1185
1267
  def has_key?: (K arg0) -> bool
1186
1268
 
1187
1269
  # <!--
1188
1270
  # rdoc-file=hash.c
1189
- # - hash.has_value?(value) -> true or false
1190
- # - hash.value?(value) -> true or false
1271
+ # - has_value?(value) -> true or false
1191
1272
  # -->
1192
- # Returns `true` if `value` is a value in `self`, otherwise `false`.
1273
+ # Returns whether `value` is a value in `self`.
1274
+ #
1275
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1193
1276
  #
1194
1277
  def has_value?: (V arg0) -> bool
1195
1278
 
1196
1279
  # <!--
1197
1280
  # rdoc-file=hash.c
1198
- # - hash.hash -> an_integer
1281
+ # - hash -> an_integer
1199
1282
  # -->
1200
- # Returns the Integer hash-code for the hash.
1283
+ # Returns the integer hash-code for the hash.
1284
+ #
1285
+ # Two hashes have the same hash-code if their content is the same (regardless of
1286
+ # order):
1201
1287
  #
1202
- # Two `Hash` objects have the same hash-code if their content is the same
1203
- # (regardless of order):
1204
1288
  # h1 = {foo: 0, bar: 1, baz: 2}
1205
1289
  # h2 = {baz: 2, bar: 1, foo: 0}
1206
1290
  # h2.hash == h1.hash # => true
1207
1291
  # h2.eql? h1 # => true
1208
1292
  #
1293
+ # Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1294
+ #
1209
1295
  def hash: () -> Integer
1210
1296
 
1211
1297
  # <!--
1212
1298
  # rdoc-file=hash.c
1213
- # - hash.include?(key) -> true or false
1214
- # - hash.has_key?(key) -> true or false
1215
- # - hash.key?(key) -> true or false
1216
- # - hash.member?(key) -> true or false
1299
+ # - include?(key) -> true or false
1217
1300
  # -->
1218
- # Returns `true` if `key` is a key in `self`, otherwise `false`.
1301
+ # Returns whether `key` is a key in `self`:
1302
+ #
1303
+ # h = {foo: 0, bar: 1, baz: 2}
1304
+ # h.include?(:bar) # => true
1305
+ # h.include?(:BAR) # => false
1306
+ #
1307
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1219
1308
  #
1220
1309
  alias include? has_key?
1221
1310
 
1222
1311
  # <!--
1223
1312
  # rdoc-file=hash.c
1224
- # - hash.inspect -> new_string
1313
+ # - inspect -> new_string
1225
1314
  # -->
1226
- # Returns a new String containing the hash entries:
1315
+ # Returns a new string containing the hash entries:
1227
1316
  #
1228
1317
  # h = {foo: 0, bar: 1, baz: 2}
1229
1318
  # h.inspect # => "{foo: 0, bar: 1, baz: 2}"
1230
1319
  #
1320
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1321
+ #
1231
1322
  def inspect: () -> String
1232
1323
 
1233
1324
  # <!--
1234
1325
  # rdoc-file=hash.c
1235
- # - hash.invert -> new_hash
1326
+ # - invert -> new_hash
1236
1327
  # -->
1237
- # Returns a new `Hash` object with the each key-value pair inverted:
1328
+ # Returns a new hash with each key-value pair inverted:
1329
+ #
1238
1330
  # h = {foo: 0, bar: 1, baz: 2}
1239
1331
  # h1 = h.invert
1240
1332
  # h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
1241
1333
  #
1242
- # Overwrites any repeated new keys: (see [Entry
1334
+ # Overwrites any repeated new keys (see [Entry
1243
1335
  # Order](rdoc-ref:Hash@Entry+Order)):
1336
+ #
1244
1337
  # h = {foo: 0, bar: 0, baz: 0}
1245
1338
  # h.invert # => {0=>:baz}
1246
1339
  #
1340
+ # Related: see [Methods for Transforming Keys and
1341
+ # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
1342
+ #
1247
1343
  def invert: () -> ::Hash[V, K]
1248
1344
 
1249
1345
  # <!--
1250
1346
  # rdoc-file=hash.c
1251
- # - hash.keep_if {|key, value| ... } -> self
1252
- # - hash.keep_if -> new_enumerator
1347
+ # - keep_if {|key, value| ... } -> self
1348
+ # - keep_if -> new_enumerator
1253
1349
  # -->
1254
- # Calls the block for each key-value pair; retains the entry if the block
1255
- # returns a truthy value; otherwise deletes the entry; returns `self`.
1256
- # h = {foo: 0, bar: 1, baz: 2}
1257
- # h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
1350
+ # With a block given, calls the block for each key-value pair; retains the entry
1351
+ # if the block returns a truthy value; otherwise deletes the entry; returns
1352
+ # `self`:
1258
1353
  #
1259
- # Returns a new Enumerator if no block given:
1260
1354
  # h = {foo: 0, bar: 1, baz: 2}
1261
- # e = h.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
1262
- # e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
1355
+ # h.keep_if { |key, value| key.start_with?('b') } # => {bar: 1, baz: 2}
1356
+ #
1357
+ # With no block given, returns a new Enumerator.
1358
+ #
1359
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1263
1360
  #
1264
1361
  def keep_if: () { (K, V) -> boolish } -> self
1265
1362
  | () -> ::Enumerator[[ K, V ], self]
1266
1363
 
1267
1364
  # <!--
1268
1365
  # rdoc-file=hash.c
1269
- # - hash.key(value) -> key or nil
1366
+ # - key(value) -> key or nil
1270
1367
  # -->
1271
1368
  # Returns the key for the first-found entry with the given `value` (see [Entry
1272
1369
  # Order](rdoc-ref:Hash@Entry+Order)):
1370
+ #
1273
1371
  # h = {foo: 0, bar: 2, baz: 2}
1274
1372
  # h.key(0) # => :foo
1275
1373
  # h.key(2) # => :bar
1276
1374
  #
1277
1375
  # Returns `nil` if no such value is found.
1278
1376
  #
1377
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
1378
+ #
1279
1379
  def key: (V) -> K?
1280
1380
 
1281
1381
  # <!-- rdoc-file=hash.c -->
1282
- # Returns `true` if `key` is a key in `self`, otherwise `false`.
1382
+ # Returns whether `key` is a key in `self`:
1383
+ #
1384
+ # h = {foo: 0, bar: 1, baz: 2}
1385
+ # h.include?(:bar) # => true
1386
+ # h.include?(:BAR) # => false
1387
+ #
1388
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1283
1389
  #
1284
1390
  alias key? has_key?
1285
1391
 
1286
1392
  # <!--
1287
1393
  # rdoc-file=hash.c
1288
- # - hash.keys -> new_array
1394
+ # - keys -> new_array
1289
1395
  # -->
1290
- # Returns a new Array containing all keys in `self`:
1396
+ # Returns a new array containing all keys in `self`:
1397
+ #
1291
1398
  # h = {foo: 0, bar: 1, baz: 2}
1292
1399
  # h.keys # => [:foo, :bar, :baz]
1293
1400
  #
1401
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
1402
+ #
1294
1403
  def keys: () -> ::Array[K]
1295
1404
 
1296
1405
  # <!-- rdoc-file=hash.c -->
1297
1406
  # Returns the count of entries in `self`:
1298
1407
  #
1299
- # {foo: 0, bar: 1, baz: 2}.length # => 3
1408
+ # {foo: 0, bar: 1, baz: 2}.size # => 3
1409
+ #
1410
+ # Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1300
1411
  #
1301
1412
  def length: () -> Integer
1302
1413
 
1303
1414
  # <!-- rdoc-file=hash.c -->
1304
- # Returns `true` if `key` is a key in `self`, otherwise `false`.
1415
+ # Returns whether `key` is a key in `self`:
1416
+ #
1417
+ # h = {foo: 0, bar: 1, baz: 2}
1418
+ # h.include?(:bar) # => true
1419
+ # h.include?(:BAR) # => false
1420
+ #
1421
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1305
1422
  #
1306
1423
  alias member? has_key?
1307
1424
 
1308
1425
  # <!--
1309
1426
  # rdoc-file=hash.c
1310
- # - hash.merge -> copy_of_self
1311
- # - hash.merge(*other_hashes) -> new_hash
1312
- # - hash.merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
1427
+ # - merge(*other_hashes) -> new_hash
1428
+ # - merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
1313
1429
  # -->
1314
- # Returns the new `Hash` formed by merging each of `other_hashes` into a copy of
1315
- # `self`.
1430
+ # Each argument `other_hash` in `other_hashes` must be a hash.
1316
1431
  #
1317
- # Each argument in `other_hashes` must be a `Hash`.
1432
+ # With arguments `other_hashes` given and no block, returns the new hash formed
1433
+ # by merging each successive `other_hash` into a copy of `self`; returns that
1434
+ # copy; for each successive entry in `other_hash`:
1318
1435
  #
1319
- # ---
1320
- #
1321
- # With arguments and no block:
1322
- # * Returns the new `Hash` object formed by merging each successive `Hash` in
1323
- # `other_hashes` into `self`.
1324
- # * Each new-key entry is added at the end.
1325
- # * Each duplicate-key entry's value overwrites the previous value.
1436
+ # * For a new key, the entry is added at the end of `self`.
1437
+ # * For duplicate key, the entry overwrites the entry in `self`, whose
1438
+ # position is unchanged.
1326
1439
  #
1327
1440
  # Example:
1441
+ #
1328
1442
  # h = {foo: 0, bar: 1, baz: 2}
1329
1443
  # h1 = {bat: 3, bar: 4}
1330
1444
  # h2 = {bam: 5, bat:6}
1331
- # h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
1445
+ # h.merge(h1, h2) # => {foo: 0, bar: 4, baz: 2, bat: 6, bam: 5}
1332
1446
  #
1333
- # With arguments and a block:
1334
- # * Returns a new `Hash` object that is the merge of `self` and each given
1335
- # hash.
1336
- # * The given hashes are merged left to right.
1337
- # * Each new-key entry is added at the end.
1338
- # * For each duplicate key:
1339
- # * Calls the block with the key and the old and new values.
1340
- # * The block's return value becomes the new value for the entry.
1447
+ # With arguments `other_hashes` and a block given, behaves as above except that
1448
+ # for a duplicate key the overwriting entry takes it value not from the entry in
1449
+ # `other_hash`, but instead from the block:
1450
+ #
1451
+ # * The block is called with the duplicate key and the values from both `self`
1452
+ # and `other_hash`.
1453
+ # * The block's return value becomes the new value for the entry in `self`.
1341
1454
  #
1342
1455
  # Example:
1456
+ #
1343
1457
  # h = {foo: 0, bar: 1, baz: 2}
1344
1458
  # h1 = {bat: 3, bar: 4}
1345
1459
  # h2 = {bam: 5, bat:6}
1346
- # h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
1347
- # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
1460
+ # h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
1461
+ # # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
1348
1462
  #
1349
- # With no arguments:
1350
- # * Returns a copy of `self`.
1351
- # * The block, if given, is ignored.
1463
+ # With no arguments, returns a copy of `self`; the block, if given, is ignored.
1352
1464
  #
1353
- # Example:
1354
- # h = {foo: 0, bar: 1, baz: 2}
1355
- # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1356
- # h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
1357
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1465
+ # Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1358
1466
  #
1359
1467
  def merge: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V]
1360
1468
  | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> C } -> ::Hash[A | K, B | V | C]
1361
1469
 
1362
1470
  # <!-- rdoc-file=hash.c -->
1363
- # Merges each of `other_hashes` into `self`; returns `self`.
1471
+ # Updates values and/or adds entries to `self`; returns `self`.
1364
1472
  #
1365
- # Each argument in `other_hashes` must be a `Hash`.
1473
+ # Each argument `other_hash` in `other_hashes` must be a hash.
1366
1474
  #
1367
- # With arguments and no block:
1368
- # * Returns `self`, after the given hashes are merged into it.
1369
- # * The given hashes are merged left to right.
1370
- # * Each new entry is added at the end.
1371
- # * Each duplicate-key entry's value overwrites the previous value.
1475
+ # With no block given, for each successive entry `key`/`new_value` in each
1476
+ # successive `other_hash`:
1372
1477
  #
1373
- # Example:
1374
- # h = {foo: 0, bar: 1, baz: 2}
1375
- # h1 = {bat: 3, bar: 4}
1376
- # h2 = {bam: 5, bat:6}
1377
- # h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
1478
+ # * If `key` is in `self`, sets `self[key] = new_value`, whose position is
1479
+ # unchanged:
1378
1480
  #
1379
- # With arguments and a block:
1380
- # * Returns `self`, after the given hashes are merged.
1381
- # * The given hashes are merged left to right.
1382
- # * Each new-key entry is added at the end.
1383
- # * For each duplicate key:
1384
- # * Calls the block with the key and the old and new values.
1385
- # * The block's return value becomes the new value for the entry.
1481
+ # h0 = {foo: 0, bar: 1, baz: 2}
1482
+ # h1 = {bar: 3, foo: -1}
1483
+ # h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
1386
1484
  #
1387
- # Example:
1388
- # h = {foo: 0, bar: 1, baz: 2}
1389
- # h1 = {bat: 3, bar: 4}
1390
- # h2 = {bam: 5, bat:6}
1391
- # h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
1392
- # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
1485
+ # * If `key` is not in `self`, adds the entry at the end of `self`:
1393
1486
  #
1394
- # With no arguments:
1395
- # * Returns `self`, unmodified.
1396
- # * The block, if given, is ignored.
1487
+ # h = {foo: 0, bar: 1, baz: 2}
1488
+ # h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4}
1397
1489
  #
1398
- # Example:
1399
- # h = {foo: 0, bar: 1, baz: 2}
1400
- # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1401
- # h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
1402
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1490
+ # With a block given, for each successive entry `key`/`new_value` in each
1491
+ # successive `other_hash`:
1492
+ #
1493
+ # * If `key` is in `self`, fetches `old_value` from `self[key]`, calls the
1494
+ # block with `key`, `old_value`, and `new_value`, and sets `self[key] =
1495
+ # new_value`, whose position is unchanged :
1496
+ #
1497
+ # season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3}
1498
+ # today = {AB: 3, H: 1, W: 1}
1499
+ # yesterday = {AB: 4, H: 2, HR: 1}
1500
+ # season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value }
1501
+ # # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3}
1502
+ #
1503
+ # * If `key` is not in `self`, adds the entry at the end of `self`:
1504
+ #
1505
+ # h = {foo: 0, bar: 1, baz: 2}
1506
+ # h.update({bat: 3}) { fail 'Cannot happen' }
1507
+ # # => {foo: 0, bar: 1, baz: 2, bat: 3}
1508
+ #
1509
+ # Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1403
1510
  #
1404
1511
  def merge!: (*::Hash[K, V] other_hashes) -> self
1405
1512
  | (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self
1406
1513
 
1407
1514
  # <!--
1408
1515
  # rdoc-file=hash.c
1409
- # - hash.rassoc(value) -> new_array or nil
1516
+ # - rassoc(value) -> new_array or nil
1410
1517
  # -->
1411
- # Returns a new 2-element Array consisting of the key and value of the
1412
- # first-found entry whose value is `==` to value (see [Entry
1413
- # Order](rdoc-ref:Hash@Entry+Order)):
1518
+ # Searches `self` for the first entry whose value is `==` to the given `value`;
1519
+ # see [Entry Order](rdoc-ref:Hash@Entry+Order).
1520
+ #
1521
+ # If the entry is found, returns its key and value as a 2-element array; returns
1522
+ # `nil` if not found:
1523
+ #
1414
1524
  # h = {foo: 0, bar: 1, baz: 1}
1415
1525
  # h.rassoc(1) # => [:bar, 1]
1416
1526
  #
1417
- # Returns `nil` if no such value found.
1527
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
1418
1528
  #
1419
1529
  def rassoc: (V) -> [ K, V ]?
1420
1530
 
1421
1531
  # <!--
1422
1532
  # rdoc-file=hash.c
1423
- # - hash.rehash -> self
1533
+ # - rehash -> self
1424
1534
  # -->
1425
- # Rebuilds the hash table by recomputing the hash index for each key; returns
1426
- # `self`.
1535
+ # Rebuilds the hash table for `self` by recomputing the hash index for each key;
1536
+ # returns `self`. Calling this method ensures that the hash table is valid.
1427
1537
  #
1428
1538
  # The hash table becomes invalid if the hash value of a key has changed after
1429
1539
  # the entry was created. See [Modifying an Active Hash
@@ -1433,40 +1543,41 @@ class Hash[unchecked out K, unchecked out V] < Object
1433
1543
 
1434
1544
  # <!--
1435
1545
  # rdoc-file=hash.c
1436
- # - hash.reject {|key, value| ... } -> new_hash
1437
- # - hash.reject -> new_enumerator
1546
+ # - reject {|key, value| ... } -> new_hash
1547
+ # - reject -> new_enumerator
1438
1548
  # -->
1439
- # Returns a new `Hash` object whose entries are all those from `self` for which
1440
- # the block returns `false` or `nil`:
1441
- # h = {foo: 0, bar: 1, baz: 2}
1442
- # h1 = h.reject {|key, value| key.start_with?('b') }
1443
- # h1 # => {:foo=>0}
1549
+ # With a block given, returns a copy of `self` with zero or more entries
1550
+ # removed; calls the block with each key-value pair; excludes the entry in the
1551
+ # copy if the block returns a truthy value, includes it otherwise:
1444
1552
  #
1445
- # Returns a new Enumerator if no block given:
1446
1553
  # h = {foo: 0, bar: 1, baz: 2}
1447
- # e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
1448
- # h1 = e.each {|key, value| key.start_with?('b') }
1449
- # h1 # => {:foo=>0}
1554
+ # h.reject {|key, value| key.start_with?('b') }
1555
+ # # => {foo: 0}
1556
+ #
1557
+ # With no block given, returns a new Enumerator.
1558
+ #
1559
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1450
1560
  #
1451
1561
  def reject: () -> ::Enumerator[[ K, V ], ::Hash[K, V]]
1452
1562
  | () { (K, V) -> boolish } -> ::Hash[K, V]
1453
1563
 
1454
1564
  # <!--
1455
1565
  # rdoc-file=hash.c
1456
- # - hash.reject! {|key, value| ... } -> self or nil
1457
- # - hash.reject! -> new_enumerator
1566
+ # - reject! {|key, value| ... } -> self or nil
1567
+ # - reject! -> new_enumerator
1458
1568
  # -->
1459
- # Returns `self`, whose remaining entries are those for which the block returns
1460
- # `false` or `nil`:
1461
- # h = {foo: 0, bar: 1, baz: 2}
1462
- # h.reject! {|key, value| value < 2 } # => {:baz=>2}
1569
+ # With a block given, calls the block with each entry's key and value; removes
1570
+ # the entry from `self` if the block returns a truthy value.
1463
1571
  #
1464
- # Returns `nil` if no entries are removed.
1572
+ # Return `self` if any entries were removed, `nil` otherwise:
1465
1573
  #
1466
- # Returns a new Enumerator if no block given:
1467
1574
  # h = {foo: 0, bar: 1, baz: 2}
1468
- # e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
1469
- # e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
1575
+ # h.reject! {|key, value| value < 2 } # => {baz: 2}
1576
+ # h.reject! {|key, value| value < 2 } # => nil
1577
+ #
1578
+ # With no block given, returns a new Enumerator.
1579
+ #
1580
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1470
1581
  #
1471
1582
  def reject!: () -> ::Enumerator[[ K, V ], self?]
1472
1583
  | () { (K, V) -> boolish } -> self?
@@ -1474,147 +1585,182 @@ class Hash[unchecked out K, unchecked out V] < Object
1474
1585
  # <!-- rdoc-file=hash.c -->
1475
1586
  # Replaces the entire contents of `self` with the contents of `other_hash`;
1476
1587
  # returns `self`:
1588
+ #
1477
1589
  # h = {foo: 0, bar: 1, baz: 2}
1478
- # h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
1590
+ # h.replace({bat: 3, bam: 4}) # => {bat: 3, bam: 4}
1591
+ #
1592
+ # Also replaces the default value or proc of `self` with the default value or
1593
+ # proc of `other_hash`.
1594
+ #
1595
+ # h = {}
1596
+ # other = Hash.new(:ok)
1597
+ # h.replace(other)
1598
+ # h.default # => :ok
1599
+ #
1600
+ # Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1479
1601
  #
1480
1602
  def replace: (Hash[K, V]) -> self
1481
1603
 
1482
1604
  # <!--
1483
1605
  # rdoc-file=hash.c
1484
- # - hash.select {|key, value| ... } -> new_hash
1485
- # - hash.select -> new_enumerator
1606
+ # - select {|key, value| ... } -> new_hash
1607
+ # - select -> new_enumerator
1486
1608
  # -->
1487
- # Returns a new `Hash` object whose entries are those for which the block
1488
- # returns a truthy value:
1489
- # h = {foo: 0, bar: 1, baz: 2}
1490
- # h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
1609
+ # With a block given, calls the block with each entry's key and value; returns a
1610
+ # new hash whose entries are those for which the block returns a truthy value:
1491
1611
  #
1492
- # Returns a new Enumerator if no block given:
1493
1612
  # h = {foo: 0, bar: 1, baz: 2}
1494
- # e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
1495
- # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
1613
+ # h.select {|key, value| value < 2 } # => {foo: 0, bar: 1}
1614
+ #
1615
+ # With no block given, returns a new Enumerator.
1616
+ #
1617
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1496
1618
  #
1497
1619
  alias select filter
1498
1620
 
1499
1621
  # <!--
1500
1622
  # rdoc-file=hash.c
1501
- # - hash.select! {|key, value| ... } -> self or nil
1502
- # - hash.select! -> new_enumerator
1623
+ # - select! {|key, value| ... } -> self or nil
1624
+ # - select! -> new_enumerator
1503
1625
  # -->
1504
- # Returns `self`, whose entries are those for which the block returns a truthy
1505
- # value:
1506
- # h = {foo: 0, bar: 1, baz: 2}
1507
- # h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
1626
+ # With a block given, calls the block with each entry's key and value; removes
1627
+ # from `self` each entry for which the block returns `false` or `nil`.
1508
1628
  #
1509
- # Returns `nil` if no entries were removed.
1629
+ # Returns `self` if any entries were removed, `nil` otherwise:
1510
1630
  #
1511
- # Returns a new Enumerator if no block given:
1512
1631
  # h = {foo: 0, bar: 1, baz: 2}
1513
- # e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
1514
- # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
1632
+ # h.select! {|key, value| value < 2 } # => {foo: 0, bar: 1}
1633
+ # h.select! {|key, value| value < 2 } # => nil
1634
+ #
1635
+ # With no block given, returns a new Enumerator.
1636
+ #
1637
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1515
1638
  #
1516
1639
  alias select! filter!
1517
1640
 
1518
1641
  # <!--
1519
1642
  # rdoc-file=hash.c
1520
- # - hash.shift -> [key, value] or nil
1643
+ # - shift -> [key, value] or nil
1521
1644
  # -->
1522
- # Removes the first hash entry (see [Entry Order](rdoc-ref:Hash@Entry+Order));
1523
- # returns a 2-element Array containing the removed key and value:
1645
+ # Removes and returns the first entry of `self` as a 2-element array; see [Entry
1646
+ # Order](rdoc-ref:Hash@Entry+Order):
1647
+ #
1524
1648
  # h = {foo: 0, bar: 1, baz: 2}
1525
1649
  # h.shift # => [:foo, 0]
1526
- # h # => {:bar=>1, :baz=>2}
1650
+ # h # => {bar: 1, baz: 2}
1651
+ #
1652
+ # Returns `nil` if `self` is empty.
1527
1653
  #
1528
- # Returns nil if the hash is empty.
1654
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1529
1655
  #
1530
1656
  def shift: () -> [ K, V ]?
1531
1657
 
1532
1658
  # <!--
1533
1659
  # rdoc-file=hash.c
1534
- # - hash.length -> integer
1535
- # - hash.size -> integer
1660
+ # - size -> integer
1536
1661
  # -->
1537
1662
  # Returns the count of entries in `self`:
1538
1663
  #
1539
- # {foo: 0, bar: 1, baz: 2}.length # => 3
1664
+ # {foo: 0, bar: 1, baz: 2}.size # => 3
1665
+ #
1666
+ # Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1540
1667
  #
1541
1668
  alias size length
1542
1669
 
1543
1670
  # <!--
1544
1671
  # rdoc-file=hash.c
1545
- # - hash.slice(*keys) -> new_hash
1672
+ # - slice(*keys) -> new_hash
1546
1673
  # -->
1547
- # Returns a new `Hash` object containing the entries for the given `keys`:
1674
+ # Returns a new hash containing the entries from `self` for the given `keys`;
1675
+ # ignores any keys that are not found:
1676
+ #
1548
1677
  # h = {foo: 0, bar: 1, baz: 2}
1549
- # h.slice(:baz, :foo) # => {:baz=>2, :foo=>0}
1678
+ # h.slice(:baz, :foo, :nosuch) # => {baz: 2, foo: 0}
1550
1679
  #
1551
- # Any given `keys` that are not found are ignored.
1680
+ # Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
1552
1681
  #
1553
1682
  def slice: (*K) -> ::Hash[K, V]
1554
1683
 
1555
1684
  # <!-- rdoc-file=hash.c -->
1556
- # Associates the given `value` with the given `key`; returns `value`.
1685
+ # Associates the given `object` with the given `key`; returns `object`.
1686
+ #
1687
+ # Searches for a hash key equivalent to the given `key`; see [Hash Key
1688
+ # Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
1689
+ #
1690
+ # If the key is found, replaces its value with the given `object`; the ordering
1691
+ # is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
1557
1692
  #
1558
- # If the given `key` exists, replaces its value with the given `value`; the
1559
- # ordering is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
1560
1693
  # h = {foo: 0, bar: 1}
1561
1694
  # h[:foo] = 2 # => 2
1562
- # h.store(:bar, 3) # => 3
1563
- # h # => {:foo=>2, :bar=>3}
1695
+ # h[:foo] # => 2
1696
+ #
1697
+ # If `key` is not found, creates a new entry for the given `key` and `object`;
1698
+ # the new entry is last in the order (see [Entry
1699
+ # Order](rdoc-ref:Hash@Entry+Order)):
1564
1700
  #
1565
- # If `key` does not exist, adds the `key` and `value`; the new entry is last in
1566
- # the order (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
1567
1701
  # h = {foo: 0, bar: 1}
1568
1702
  # h[:baz] = 2 # => 2
1569
- # h.store(:bat, 3) # => 3
1570
- # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
1703
+ # h[:baz] # => 2
1704
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
1705
+ #
1706
+ # Related: #[]; see also [Methods for
1707
+ # Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1571
1708
  #
1572
1709
  alias store []=
1573
1710
 
1574
1711
  # <!--
1575
1712
  # rdoc-file=hash.c
1576
- # - hash.to_a -> new_array
1713
+ # - to_a -> new_array
1577
1714
  # -->
1578
- # Returns a new Array of 2-element Array objects; each nested Array contains a
1579
- # key-value pair from `self`:
1715
+ # Returns all elements of `self` as an array of 2-element arrays; each nested
1716
+ # array contains a key-value pair from `self`:
1717
+ #
1580
1718
  # h = {foo: 0, bar: 1, baz: 2}
1581
1719
  # h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
1582
1720
  #
1721
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1722
+ #
1583
1723
  def to_a: () -> ::Array[[ K, V ]]
1584
1724
 
1585
1725
  # <!--
1586
1726
  # rdoc-file=hash.c
1587
- # - hash.to_h -> self or new_hash
1588
- # - hash.to_h {|key, value| ... } -> new_hash
1727
+ # - to_h {|key, value| ... } -> new_hash
1728
+ # - to_h -> self or new_hash
1589
1729
  # -->
1590
- # For an instance of `Hash`, returns `self`.
1730
+ # With a block given, returns a new hash whose content is based on the block;
1731
+ # the block is called with each entry's key and value; the block should return a
1732
+ # 2-element array containing the key and value to be included in the returned
1733
+ # array:
1591
1734
  #
1592
- # For a subclass of `Hash`, returns a new `Hash` containing the content of
1735
+ # h = {foo: 0, bar: 1, baz: 2}
1736
+ # h.to_h {|key, value| [value, key] }
1737
+ # # => {0 => :foo, 1 => :bar, 2 => :baz}
1738
+ #
1739
+ # With no block given, returns `self` if `self` is an instance of `Hash`; if
1740
+ # `self` is a subclass of `Hash`, returns a new hash containing the content of
1593
1741
  # `self`.
1594
1742
  #
1595
- # When a block is given, returns a new `Hash` object whose content is based on
1596
- # the block; the block should return a 2-element Array object specifying the
1597
- # key-value pair to be included in the returned Array:
1598
- # h = {foo: 0, bar: 1, baz: 2}
1599
- # h1 = h.to_h {|key, value| [value, key] }
1600
- # h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
1743
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1601
1744
  #
1602
1745
  def to_h: () -> Hash[K, V]
1603
1746
  | [A, B] () { (K, V) -> [ A, B ] } -> Hash[A, B]
1604
1747
 
1605
1748
  # <!--
1606
1749
  # rdoc-file=hash.c
1607
- # - hash.to_hash -> self
1750
+ # - to_hash -> self
1608
1751
  # -->
1609
1752
  # Returns `self`.
1610
1753
  #
1754
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1755
+ #
1611
1756
  def to_hash: () -> self
1612
1757
 
1613
1758
  # <!--
1614
1759
  # rdoc-file=hash.c
1615
- # - hash.to_proc -> proc
1760
+ # - to_proc -> proc
1616
1761
  # -->
1617
1762
  # Returns a Proc object that maps a key to its value:
1763
+ #
1618
1764
  # h = {foo: 0, bar: 1, baz: 2}
1619
1765
  # proc = h.to_proc
1620
1766
  # proc.class # => Proc
@@ -1622,227 +1768,366 @@ class Hash[unchecked out K, unchecked out V] < Object
1622
1768
  # proc.call(:bar) # => 1
1623
1769
  # proc.call(:nosuch) # => nil
1624
1770
  #
1771
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1772
+ #
1625
1773
  def to_proc: () -> ^(K) -> V?
1626
1774
 
1627
1775
  # <!-- rdoc-file=hash.c -->
1628
- # Returns a new String containing the hash entries:
1776
+ # Returns a new string containing the hash entries:
1629
1777
  #
1630
1778
  # h = {foo: 0, bar: 1, baz: 2}
1631
1779
  # h.inspect # => "{foo: 0, bar: 1, baz: 2}"
1632
1780
  #
1781
+ # Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
1782
+ #
1633
1783
  alias to_s inspect
1634
1784
 
1635
1785
  # <!--
1636
1786
  # rdoc-file=hash.c
1637
- # - hash.transform_keys {|key| ... } -> new_hash
1638
- # - hash.transform_keys(hash2) -> new_hash
1639
- # - hash.transform_keys(hash2) {|other_key| ...} -> new_hash
1640
- # - hash.transform_keys -> new_enumerator
1787
+ # - transform_keys {|old_key| ... } -> new_hash
1788
+ # - transform_keys(other_hash) -> new_hash
1789
+ # - transform_keys(other_hash) {|old_key| ...} -> new_hash
1790
+ # - transform_keys -> new_enumerator
1641
1791
  # -->
1642
- # Returns a new `Hash` object; each entry has:
1643
- # * A key provided by the block.
1644
- # * The value from `self`.
1792
+ # With an argument, a block, or both given, derives a new hash `new_hash` from
1793
+ # `self`, the argument, and/or the block; all, some, or none of its keys may be
1794
+ # different from those in `self`.
1645
1795
  #
1646
- # An optional hash argument can be provided to map keys to new keys. Any key not
1647
- # given will be mapped using the provided block, or remain the same if no block
1648
- # is given.
1796
+ # With a block given and no argument, `new_hash` has keys determined only by the
1797
+ # block.
1798
+ #
1799
+ # For each key/value pair `old_key/value` in `self`, calls the block with
1800
+ # `old_key`; the block's return value becomes `new_key`; sets `new_hash[new_key]
1801
+ # = value`; a duplicate key overwrites:
1649
1802
  #
1650
- # Transform keys:
1651
1803
  # h = {foo: 0, bar: 1, baz: 2}
1652
- # h1 = h.transform_keys {|key| key.to_s }
1653
- # h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
1804
+ # h.transform_keys {|old_key| old_key.to_s }
1805
+ # # => {"foo" => 0, "bar" => 1, "baz" => 2}
1806
+ # h.transform_keys {|old_key| 'xxx' }
1807
+ # # => {"xxx" => 2}
1654
1808
  #
1655
- # h.transform_keys(foo: :bar, bar: :foo)
1656
- # #=> {bar: 0, foo: 1, baz: 2}
1809
+ # With argument `other_hash` given and no block, `new_hash` may have new keys
1810
+ # provided by `other_hash` and unchanged keys provided by `self`.
1657
1811
  #
1658
- # h.transform_keys(foo: :hello, &:to_s)
1659
- # #=> {:hello=>0, "bar"=>1, "baz"=>2}
1812
+ # For each key/value pair `old_key/old_value` in `self`, looks for key `old_key`
1813
+ # in `other_hash`:
1660
1814
  #
1661
- # Overwrites values for duplicate keys:
1662
- # h = {foo: 0, bar: 1, baz: 2}
1663
- # h1 = h.transform_keys {|key| :bat }
1664
- # h1 # => {:bat=>2}
1815
+ # * If `old_key` is found, its value `other_hash[old_key]` is taken as
1816
+ # `new_key`; sets `new_hash[new_key] = value`; a duplicate key overwrites:
1817
+ #
1818
+ # h = {foo: 0, bar: 1, baz: 2}
1819
+ # h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO)
1820
+ # # => {FOO: 0, BAR: 1, BAZ: 2}
1821
+ # h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO)
1822
+ # # => {FOO: 2}
1823
+ #
1824
+ # * If `old_key` is not found, sets `new_hash[old_key] = value`; a duplicate
1825
+ # key overwrites:
1826
+ #
1827
+ # h = {foo: 0, bar: 1, baz: 2}
1828
+ # h.transform_keys({})
1829
+ # # => {foo: 0, bar: 1, baz: 2}
1830
+ # h.transform_keys(baz: :foo)
1831
+ # # => {foo: 2, bar: 1}
1832
+ #
1833
+ # Unused keys in `other_hash` are ignored:
1665
1834
  #
1666
- # Returns a new Enumerator if no block given:
1667
1835
  # h = {foo: 0, bar: 1, baz: 2}
1668
- # e = h.transform_keys # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_keys>
1669
- # h1 = e.each { |key| key.to_s }
1670
- # h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
1836
+ # h.transform_keys(bat: 3)
1837
+ # # => {foo: 0, bar: 1, baz: 2}
1838
+ #
1839
+ # With both argument `other_hash` and a block given, `new_hash` has new keys
1840
+ # specified by `other_hash` or by the block, and unchanged keys provided by
1841
+ # `self`.
1842
+ #
1843
+ # For each pair `old_key` and `value` in `self`:
1844
+ #
1845
+ # * If `other_hash` has key `old_key` (with value `new_key`), does not call
1846
+ # the block for that key; sets `new_hash[new_key] = value`; a duplicate key
1847
+ # overwrites:
1848
+ #
1849
+ # h = {foo: 0, bar: 1, baz: 2}
1850
+ # h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' }
1851
+ # # => {FOO: 0, BAR: 1, BAZ: 2}
1852
+ #
1853
+ # * If `other_hash` does not have key `old_key`, calls the block with
1854
+ # `old_key` and takes its return value as `new_key`; sets `new_hash[new_key]
1855
+ # = value`; a duplicate key overwrites:
1856
+ #
1857
+ # h = {foo: 0, bar: 1, baz: 2}
1858
+ # h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse }
1859
+ # # => {"oof" => 0, "rab" => 1, BAZ: 2}
1860
+ # h.transform_keys(baz: :BAZ) {|key| 'ook' }
1861
+ # # => {"ook" => 1, BAZ: 2}
1862
+ #
1863
+ # With no argument and no block given, returns a new Enumerator.
1864
+ #
1865
+ # Related: see [Methods for Transforming Keys and
1866
+ # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
1671
1867
  #
1672
1868
  def transform_keys: () -> Enumerator[K, Hash[untyped, V]]
1673
1869
  | [A] () { (K) -> A } -> Hash[A, V]
1674
1870
 
1675
1871
  # <!--
1676
1872
  # rdoc-file=hash.c
1677
- # - hash.transform_keys! {|key| ... } -> self
1678
- # - hash.transform_keys!(hash2) -> self
1679
- # - hash.transform_keys!(hash2) {|other_key| ...} -> self
1680
- # - hash.transform_keys! -> new_enumerator
1873
+ # - transform_keys! {|old_key| ... } -> self
1874
+ # - transform_keys!(other_hash) -> self
1875
+ # - transform_keys!(other_hash) {|old_key| ...} -> self
1876
+ # - transform_keys! -> new_enumerator
1681
1877
  # -->
1682
- # Same as Hash#transform_keys but modifies the receiver in place instead of
1683
- # returning a new hash.
1878
+ # With an argument, a block, or both given, derives keys from the argument, the
1879
+ # block, and `self`; all, some, or none of the keys in `self` may be changed.
1880
+ #
1881
+ # With a block given and no argument, derives keys only from the block; all,
1882
+ # some, or none of the keys in `self` may be changed.
1883
+ #
1884
+ # For each key/value pair `old_key/value` in `self`, calls the block with
1885
+ # `old_key`; the block's return value becomes `new_key`; removes the entry for
1886
+ # `old_key`: `self.delete(old_key)`; sets `self[new_key] = value`; a duplicate
1887
+ # key overwrites:
1888
+ #
1889
+ # h = {foo: 0, bar: 1, baz: 2}
1890
+ # h.transform_keys! {|old_key| old_key.to_s }
1891
+ # # => {"foo" => 0, "bar" => 1, "baz" => 2}
1892
+ # h = {foo: 0, bar: 1, baz: 2}
1893
+ # h.transform_keys! {|old_key| 'xxx' }
1894
+ # # => {"xxx" => 2}
1895
+ #
1896
+ # With argument `other_hash` given and no block, derives keys for `self` from
1897
+ # `other_hash` and `self`; all, some, or none of the keys in `self` may be
1898
+ # changed.
1899
+ #
1900
+ # For each key/value pair `old_key/old_value` in `self`, looks for key `old_key`
1901
+ # in `other_hash`:
1902
+ #
1903
+ # * If `old_key` is found, takes value `other_hash[old_key]` as `new_key`;
1904
+ # removes the entry for `old_key`: `self.delete(old_key)`; sets
1905
+ # `self[new_key] = value`; a duplicate key overwrites:
1906
+ #
1907
+ # h = {foo: 0, bar: 1, baz: 2}
1908
+ # h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO)
1909
+ # # => {FOO: 0, BAR: 1, BAZ: 2}
1910
+ # h = {foo: 0, bar: 1, baz: 2}
1911
+ # h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO)
1912
+ # # => {FOO: 2}
1913
+ #
1914
+ # * If `old_key` is not found, does nothing:
1915
+ #
1916
+ # h = {foo: 0, bar: 1, baz: 2}
1917
+ # h.transform_keys!({})
1918
+ # # => {foo: 0, bar: 1, baz: 2}
1919
+ # h.transform_keys!(baz: :foo)
1920
+ # # => {foo: 2, bar: 1}
1921
+ #
1922
+ # Unused keys in `other_hash` are ignored:
1923
+ #
1924
+ # h = {foo: 0, bar: 1, baz: 2}
1925
+ # h.transform_keys!(bat: 3)
1926
+ # # => {foo: 0, bar: 1, baz: 2}
1927
+ #
1928
+ # With both argument `other_hash` and a block given, derives keys from
1929
+ # `other_hash`, the block, and `self`; all, some, or none of the keys in `self`
1930
+ # may be changed.
1931
+ #
1932
+ # For each pair `old_key` and `value` in `self`:
1933
+ #
1934
+ # * If `other_hash` has key `old_key` (with value `new_key`), does not call
1935
+ # the block for that key; removes the entry for `old_key`:
1936
+ # `self.delete(old_key)`; sets `self[new_key] = value`; a duplicate key
1937
+ # overwrites:
1938
+ #
1939
+ # h = {foo: 0, bar: 1, baz: 2}
1940
+ # h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' }
1941
+ # # => {FOO: 0, BAR: 1, BAZ: 2}
1942
+ #
1943
+ # * If `other_hash` does not have key `old_key`, calls the block with
1944
+ # `old_key` and takes its return value as `new_key`; removes the entry for
1945
+ # `old_key`: `self.delete(old_key)`; sets `self[new_key] = value`; a
1946
+ # duplicate key overwrites:
1947
+ #
1948
+ # h = {foo: 0, bar: 1, baz: 2}
1949
+ # h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse }
1950
+ # # => {"oof" => 0, "rab" => 1, BAZ: 2}
1951
+ # h = {foo: 0, bar: 1, baz: 2}
1952
+ # h.transform_keys!(baz: :BAZ) {|key| 'ook' }
1953
+ # # => {"ook" => 1, BAZ: 2}
1954
+ #
1955
+ # With no argument and no block given, returns a new Enumerator.
1956
+ #
1957
+ # Related: see [Methods for Transforming Keys and
1958
+ # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
1684
1959
  #
1685
1960
  def transform_keys!: () -> Enumerator[K, self]
1686
1961
  | () { (K) -> K } -> self
1687
1962
 
1688
1963
  # <!--
1689
1964
  # rdoc-file=hash.c
1690
- # - hash.transform_values {|value| ... } -> new_hash
1691
- # - hash.transform_values -> new_enumerator
1965
+ # - transform_values {|value| ... } -> new_hash
1966
+ # - transform_values -> new_enumerator
1692
1967
  # -->
1693
- # Returns a new `Hash` object; each entry has:
1694
- # * A key from `self`.
1695
- # * A value provided by the block.
1968
+ # With a block given, returns a new hash `new_hash`; for each pair `key`/`value`
1969
+ # in `self`, calls the block with `value` and captures its return as
1970
+ # `new_value`; adds to `new_hash` the entry `key`/`new_value`:
1696
1971
  #
1697
- # Transform values:
1698
1972
  # h = {foo: 0, bar: 1, baz: 2}
1699
1973
  # h1 = h.transform_values {|value| value * 100}
1700
- # h1 # => {:foo=>0, :bar=>100, :baz=>200}
1974
+ # h1 # => {foo: 0, bar: 100, baz: 200}
1701
1975
  #
1702
- # Returns a new Enumerator if no block given:
1703
- # h = {foo: 0, bar: 1, baz: 2}
1704
- # e = h.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
1705
- # h1 = e.each { |value| value * 100}
1706
- # h1 # => {:foo=>0, :bar=>100, :baz=>200}
1976
+ # With no block given, returns a new Enumerator.
1977
+ #
1978
+ # Related: see [Methods for Transforming Keys and
1979
+ # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
1707
1980
  #
1708
1981
  def transform_values: () -> Enumerator[V, Hash[K, untyped]]
1709
1982
  | [A] () { (V) -> A } -> Hash[K, A]
1710
1983
 
1711
1984
  # <!--
1712
1985
  # rdoc-file=hash.c
1713
- # - hash.transform_values! {|value| ... } -> self
1714
- # - hash.transform_values! -> new_enumerator
1986
+ # - transform_values! {|old_value| ... } -> self
1987
+ # - transform_values! -> new_enumerator
1715
1988
  # -->
1716
- # Returns `self`, whose keys are unchanged, and whose values are determined by
1717
- # the given block.
1718
- # h = {foo: 0, bar: 1, baz: 2}
1719
- # h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
1989
+ # With a block given, changes the values of `self` as determined by the block;
1990
+ # returns `self`.
1991
+ #
1992
+ # For each entry `key`/`old_value` in `self`, calls the block with `old_value`,
1993
+ # captures its return value as `new_value`, and sets `self[key] = new_value`:
1720
1994
  #
1721
- # Returns a new Enumerator if no block given:
1722
1995
  # h = {foo: 0, bar: 1, baz: 2}
1723
- # e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!>
1724
- # h1 = e.each {|value| value * 100}
1725
- # h1 # => {:foo=>0, :bar=>100, :baz=>200}
1996
+ # h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200}
1997
+ #
1998
+ # With no block given, returns a new Enumerator.
1999
+ #
2000
+ # Related: see [Methods for Transforming Keys and
2001
+ # Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
1726
2002
  #
1727
2003
  def transform_values!: () -> Enumerator[V, self]
1728
2004
  | () { (V) -> V } -> self
1729
2005
 
1730
2006
  # <!--
1731
2007
  # rdoc-file=hash.c
1732
- # - hash.merge! -> self
1733
- # - hash.merge!(*other_hashes) -> self
1734
- # - hash.merge!(*other_hashes) { |key, old_value, new_value| ... } -> self
2008
+ # - update(*other_hashes) -> self
2009
+ # - update(*other_hashes) { |key, old_value, new_value| ... } -> self
1735
2010
  # -->
1736
- # Merges each of `other_hashes` into `self`; returns `self`.
2011
+ # Updates values and/or adds entries to `self`; returns `self`.
1737
2012
  #
1738
- # Each argument in `other_hashes` must be a `Hash`.
2013
+ # Each argument `other_hash` in `other_hashes` must be a hash.
1739
2014
  #
1740
- # With arguments and no block:
1741
- # * Returns `self`, after the given hashes are merged into it.
1742
- # * The given hashes are merged left to right.
1743
- # * Each new entry is added at the end.
1744
- # * Each duplicate-key entry's value overwrites the previous value.
2015
+ # With no block given, for each successive entry `key`/`new_value` in each
2016
+ # successive `other_hash`:
1745
2017
  #
1746
- # Example:
1747
- # h = {foo: 0, bar: 1, baz: 2}
1748
- # h1 = {bat: 3, bar: 4}
1749
- # h2 = {bam: 5, bat:6}
1750
- # h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
2018
+ # * If `key` is in `self`, sets `self[key] = new_value`, whose position is
2019
+ # unchanged:
1751
2020
  #
1752
- # With arguments and a block:
1753
- # * Returns `self`, after the given hashes are merged.
1754
- # * The given hashes are merged left to right.
1755
- # * Each new-key entry is added at the end.
1756
- # * For each duplicate key:
1757
- # * Calls the block with the key and the old and new values.
1758
- # * The block's return value becomes the new value for the entry.
2021
+ # h0 = {foo: 0, bar: 1, baz: 2}
2022
+ # h1 = {bar: 3, foo: -1}
2023
+ # h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
1759
2024
  #
1760
- # Example:
1761
- # h = {foo: 0, bar: 1, baz: 2}
1762
- # h1 = {bat: 3, bar: 4}
1763
- # h2 = {bam: 5, bat:6}
1764
- # h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
1765
- # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
2025
+ # * If `key` is not in `self`, adds the entry at the end of `self`:
1766
2026
  #
1767
- # With no arguments:
1768
- # * Returns `self`, unmodified.
1769
- # * The block, if given, is ignored.
2027
+ # h = {foo: 0, bar: 1, baz: 2}
2028
+ # h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4}
1770
2029
  #
1771
- # Example:
1772
- # h = {foo: 0, bar: 1, baz: 2}
1773
- # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1774
- # h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
1775
- # h1 # => {:foo=>0, :bar=>1, :baz=>2}
2030
+ # With a block given, for each successive entry `key`/`new_value` in each
2031
+ # successive `other_hash`:
2032
+ #
2033
+ # * If `key` is in `self`, fetches `old_value` from `self[key]`, calls the
2034
+ # block with `key`, `old_value`, and `new_value`, and sets `self[key] =
2035
+ # new_value`, whose position is unchanged :
2036
+ #
2037
+ # season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3}
2038
+ # today = {AB: 3, H: 1, W: 1}
2039
+ # yesterday = {AB: 4, H: 2, HR: 1}
2040
+ # season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value }
2041
+ # # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3}
2042
+ #
2043
+ # * If `key` is not in `self`, adds the entry at the end of `self`:
2044
+ #
2045
+ # h = {foo: 0, bar: 1, baz: 2}
2046
+ # h.update({bat: 3}) { fail 'Cannot happen' }
2047
+ # # => {foo: 0, bar: 1, baz: 2, bat: 3}
2048
+ #
2049
+ # Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1776
2050
  #
1777
2051
  alias update merge!
1778
2052
 
1779
2053
  # <!-- rdoc-file=hash.c -->
1780
- # Returns `true` if `value` is a value in `self`, otherwise `false`.
2054
+ # Returns whether `value` is a value in `self`.
2055
+ #
2056
+ # Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
1781
2057
  #
1782
2058
  alias value? has_value?
1783
2059
 
1784
2060
  # <!--
1785
2061
  # rdoc-file=hash.c
1786
- # - hash.values -> new_array
2062
+ # - values -> new_array
1787
2063
  # -->
1788
- # Returns a new Array containing all values in `self`:
2064
+ # Returns a new array containing all values in `self`:
2065
+ #
1789
2066
  # h = {foo: 0, bar: 1, baz: 2}
1790
2067
  # h.values # => [0, 1, 2]
1791
2068
  #
2069
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
2070
+ #
1792
2071
  def values: () -> ::Array[V]
1793
2072
 
1794
2073
  # <!--
1795
2074
  # rdoc-file=hash.c
1796
- # - hash.values_at(*keys) -> new_array
2075
+ # - values_at(*keys) -> new_array
1797
2076
  # -->
1798
- # Returns a new Array containing values for the given `keys`:
2077
+ # Returns a new array containing values for the given `keys`:
2078
+ #
1799
2079
  # h = {foo: 0, bar: 1, baz: 2}
1800
2080
  # h.values_at(:baz, :foo) # => [2, 0]
1801
2081
  #
1802
- # The [default values](rdoc-ref:Hash@Default+Values) are returned for any keys
1803
- # that are not found:
2082
+ # The [hash default](rdoc-ref:Hash@Hash+Default) is returned for each key that
2083
+ # is not found:
2084
+ #
1804
2085
  # h.values_at(:hello, :foo) # => [nil, 0]
1805
2086
  #
2087
+ # Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
2088
+ #
1806
2089
  def values_at: (*K arg0) -> ::Array[V?]
1807
2090
 
1808
2091
  private
1809
2092
 
1810
2093
  # <!--
1811
2094
  # rdoc-file=hash.rb
1812
- # - Hash.new(default_value = nil) -> new_hash
1813
- # - Hash.new(default_value = nil, capacity: size) -> new_hash
1814
- # - Hash.new {|hash, key| ... } -> new_hash
1815
- # - Hash.new(capacity: size) {|hash, key| ... } -> new_hash
2095
+ # - Hash.new(default_value = nil, capacity: 0) -> new_hash
2096
+ # - Hash.new(capacity: 0) {|self, key| ... } -> new_hash
1816
2097
  # -->
1817
- # Returns a new empty `Hash` object.
2098
+ # Returns a new empty Hash object.
1818
2099
  #
1819
- # The initial default value and initial default proc for the new hash depend on
1820
- # which form above was used. See [Default Values](rdoc-ref:Hash@Default+Values).
2100
+ # Initializes the values of Hash#default and Hash#default_proc, which determine
2101
+ # the behavior when a given key is not found; see [Key Not
2102
+ # Found?](rdoc-ref:Hash@Key+Not+Found-3F).
1821
2103
  #
1822
- # If neither an argument nor a block is given, initializes both the default
1823
- # value and the default proc to `nil`:
1824
- # h = Hash.new
1825
- # h.default # => nil
1826
- # h.default_proc # => nil
2104
+ # By default, a hash has `nil` values for both `default` and `default_proc`:
1827
2105
  #
1828
- # If argument `default_value` is given but no block is given, initializes the
1829
- # default value to the given `default_value` and the default proc to `nil`:
1830
- # h = Hash.new(false)
1831
- # h.default # => false
1832
- # h.default_proc # => nil
2106
+ # h = Hash.new # => {}
2107
+ # h.default # => nil
2108
+ # h.default_proc # => nil
1833
2109
  #
1834
- # If a block is given but no `default_value`, stores the block as the default
1835
- # proc and sets the default value to `nil`:
1836
- # h = Hash.new {|hash, key| "Default value for #{key}" }
1837
- # h.default # => nil
1838
- # h.default_proc.class # => Proc
1839
- # h[:nosuch] # => "Default value for nosuch"
2110
+ # With argument `default_value` given, sets the `default` value for the hash:
1840
2111
  #
1841
- # If both a block and a `default_value` are given, raises an `ArgumentError`
2112
+ # h = Hash.new(false) # => {}
2113
+ # h.default # => false
2114
+ # h.default_proc # => nil
1842
2115
  #
1843
- # If the optional keyword argument `capacity` is given, the hash will be
1844
- # allocated with enough capacity to accommodate this many keys without having to
1845
- # be resized.
2116
+ # With a block given, sets the `default_proc` value:
2117
+ #
2118
+ # h = Hash.new {|hash, key| "Hash #{hash}: Default value for #{key}" }
2119
+ # h.default # => nil
2120
+ # h.default_proc # => #<Proc:0x00000289b6fa7048 (irb):185>
2121
+ # h[:nosuch] # => "Hash {}: Default value for nosuch"
2122
+ #
2123
+ # Raises ArgumentError if both `default_value` and a block are given.
2124
+ #
2125
+ # If optional keyword argument `capacity` is given with a positive integer value
2126
+ # `n`, initializes the hash with enough capacity to accommodate `n` entries
2127
+ # without resizing.
2128
+ #
2129
+ # See also [Methods for Creating a
2130
+ # Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash).
1846
2131
  #
1847
2132
  def initialize: (?capacity: int) -> void
1848
2133
  | (V default, ?capacity: int) -> void
@@ -1850,12 +2135,23 @@ class Hash[unchecked out K, unchecked out V] < Object
1850
2135
 
1851
2136
  # <!--
1852
2137
  # rdoc-file=hash.c
1853
- # - hash.replace(other_hash) -> self
2138
+ # - replace(other_hash) -> self
1854
2139
  # -->
1855
2140
  # Replaces the entire contents of `self` with the contents of `other_hash`;
1856
2141
  # returns `self`:
2142
+ #
1857
2143
  # h = {foo: 0, bar: 1, baz: 2}
1858
- # h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
2144
+ # h.replace({bat: 3, bam: 4}) # => {bat: 3, bam: 4}
2145
+ #
2146
+ # Also replaces the default value or proc of `self` with the default value or
2147
+ # proc of `other_hash`.
2148
+ #
2149
+ # h = {}
2150
+ # other = Hash.new(:ok)
2151
+ # h.replace(other)
2152
+ # h.default # => :ok
2153
+ #
2154
+ # Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
1859
2155
  #
1860
2156
  def initialize_copy: (self object) -> self
1861
2157
  end