rbs 4.0.0.dev.4 → 4.1.0.pre.2

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