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