rbs 3.9.5 → 3.10.0.pre.1

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