rbs 2.0.0 → 2.2.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 (208) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +34 -0
  3. data/.github/workflows/ruby.yml +5 -0
  4. data/CHANGELOG.md +82 -0
  5. data/README.md +6 -1
  6. data/Rakefile +56 -21
  7. data/core/array.rbs +2866 -1086
  8. data/core/basic_object.rbs +150 -30
  9. data/core/binding.rbs +33 -0
  10. data/core/builtin.rbs +4 -4
  11. data/core/class.rbs +43 -5
  12. data/core/comparable.rbs +57 -0
  13. data/core/complex.rbs +170 -4
  14. data/core/constants.rbs +51 -0
  15. data/core/deprecated.rbs +7 -0
  16. data/core/dir.rbs +305 -20
  17. data/core/encoding.rbs +1214 -77
  18. data/core/enumerable.rbs +2173 -234
  19. data/core/enumerator.rbs +448 -182
  20. data/core/env.rbs +448 -1
  21. data/core/errno.rbs +1 -10
  22. data/core/errors.rbs +152 -2
  23. data/core/exception.rbs +201 -127
  24. data/core/false_class.rbs +27 -0
  25. data/core/fiber.rbs +118 -37
  26. data/core/fiber_error.rbs +8 -9
  27. data/core/file.rbs +1060 -142
  28. data/core/file_test.rbs +287 -32
  29. data/core/float.rbs +776 -300
  30. data/core/gc.rbs +185 -34
  31. data/core/global_variables.rbs +5 -1
  32. data/core/hash.rbs +1582 -649
  33. data/core/integer.rbs +974 -204
  34. data/core/io/buffer.rbs +710 -0
  35. data/core/io/wait.rbs +29 -8
  36. data/core/io.rbs +2438 -417
  37. data/core/kernel.rbs +2319 -318
  38. data/core/marshal.rbs +37 -2
  39. data/core/match_data.rbs +123 -6
  40. data/core/math.rbs +126 -6
  41. data/core/method.rbs +226 -102
  42. data/core/module.rbs +421 -45
  43. data/core/nil_class.rbs +64 -0
  44. data/core/numeric.rbs +620 -142
  45. data/core/object.rbs +453 -81
  46. data/core/object_space.rbs +92 -2
  47. data/core/proc.rbs +482 -285
  48. data/core/process.rbs +443 -34
  49. data/core/ractor.rbs +232 -9
  50. data/core/random.rbs +151 -52
  51. data/core/range.rbs +885 -160
  52. data/core/rational.rbs +122 -6
  53. data/core/rb_config.rbs +14 -4
  54. data/core/refinement.rbs +44 -0
  55. data/core/regexp.rbs +156 -14
  56. data/core/ruby_vm.rbs +42 -3
  57. data/core/signal.rbs +78 -39
  58. data/core/string.rbs +2123 -567
  59. data/core/string_io.rbs +204 -0
  60. data/core/struct.rbs +283 -28
  61. data/core/symbol.rbs +304 -30
  62. data/core/thread.rbs +1288 -688
  63. data/core/thread_group.rbs +66 -10
  64. data/core/time.rbs +643 -217
  65. data/core/trace_point.rbs +100 -12
  66. data/core/true_class.rbs +24 -0
  67. data/core/unbound_method.rbs +73 -7
  68. data/core/warning.rbs +37 -12
  69. data/docs/CONTRIBUTING.md +40 -34
  70. data/docs/stdlib.md +3 -102
  71. data/docs/syntax.md +54 -11
  72. data/ext/rbs_extension/extconf.rb +1 -0
  73. data/ext/rbs_extension/lexer.h +5 -0
  74. data/ext/rbs_extension/lexstate.c +6 -0
  75. data/ext/rbs_extension/parser.c +85 -10
  76. data/ext/rbs_extension/ruby_objs.c +4 -2
  77. data/ext/rbs_extension/ruby_objs.h +2 -2
  78. data/goodcheck.yml +0 -11
  79. data/lib/rbs/annotate/annotations.rb +197 -0
  80. data/lib/rbs/annotate/formatter.rb +80 -0
  81. data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
  82. data/lib/rbs/annotate/rdoc_source.rb +120 -0
  83. data/lib/rbs/annotate.rb +6 -0
  84. data/lib/rbs/ast/members.rb +21 -13
  85. data/lib/rbs/buffer.rb +17 -11
  86. data/lib/rbs/cli.rb +48 -1
  87. data/lib/rbs/definition_builder/method_builder.rb +28 -16
  88. data/lib/rbs/definition_builder.rb +6 -2
  89. data/lib/rbs/environment.rb +8 -4
  90. data/lib/rbs/location_aux.rb +12 -0
  91. data/lib/rbs/namespace.rb +1 -1
  92. data/lib/rbs/prototype/rb.rb +12 -0
  93. data/lib/rbs/type_alias_regularity.rb +6 -4
  94. data/lib/rbs/type_name.rb +1 -1
  95. data/lib/rbs/types.rb +1 -1
  96. data/lib/rbs/validator.rb +6 -0
  97. data/lib/rbs/version.rb +1 -1
  98. data/lib/rbs/writer.rb +54 -4
  99. data/lib/rbs.rb +0 -2
  100. data/schema/typeParam.json +3 -3
  101. data/sig/annotate/annotations.rbs +102 -0
  102. data/sig/annotate/formatter.rbs +24 -0
  103. data/sig/annotate/rdoc_annotater.rbs +82 -0
  104. data/sig/annotate/rdoc_source.rbs +30 -0
  105. data/sig/buffer.rbs +6 -2
  106. data/sig/cli.rbs +2 -0
  107. data/sig/collection/{collections.rbs → sources.rbs} +0 -0
  108. data/sig/location.rbs +6 -0
  109. data/sig/members.rbs +24 -18
  110. data/sig/method_builder.rbs +5 -4
  111. data/sig/method_types.rbs +5 -1
  112. data/sig/polyfill.rbs +78 -0
  113. data/sig/validator.rbs +3 -1
  114. data/sig/writer.rbs +79 -2
  115. data/stdlib/abbrev/0/abbrev.rbs +6 -0
  116. data/stdlib/abbrev/0/array.rbs +26 -0
  117. data/stdlib/base64/0/base64.rbs +31 -0
  118. data/stdlib/benchmark/0/benchmark.rbs +74 -3
  119. data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
  120. data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
  121. data/stdlib/cgi/0/core.rbs +649 -21
  122. data/stdlib/coverage/0/coverage.rbs +164 -2
  123. data/stdlib/csv/0/csv.rbs +2862 -398
  124. data/stdlib/date/0/date.rbs +483 -25
  125. data/stdlib/date/0/date_time.rbs +187 -12
  126. data/stdlib/dbm/0/dbm.rbs +152 -17
  127. data/stdlib/digest/0/digest.rbs +146 -0
  128. data/stdlib/erb/0/erb.rbs +65 -245
  129. data/stdlib/fiber/0/fiber.rbs +73 -91
  130. data/stdlib/fileutils/0/fileutils.rbs +301 -1
  131. data/stdlib/find/0/find.rbs +9 -0
  132. data/stdlib/forwardable/0/forwardable.rbs +65 -1
  133. data/stdlib/io-console/0/io-console.rbs +227 -15
  134. data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
  135. data/stdlib/json/0/json.rbs +1147 -145
  136. data/stdlib/logger/0/formatter.rbs +24 -0
  137. data/stdlib/logger/0/log_device.rbs +64 -0
  138. data/stdlib/logger/0/logger.rbs +165 -13
  139. data/stdlib/logger/0/period.rbs +10 -0
  140. data/stdlib/logger/0/severity.rbs +26 -0
  141. data/stdlib/monitor/0/monitor.rbs +163 -0
  142. data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
  143. data/stdlib/net-http/0/manifest.yaml +1 -0
  144. data/stdlib/net-http/0/net-http.rbs +1513 -683
  145. data/stdlib/nkf/0/nkf.rbs +372 -0
  146. data/stdlib/objspace/0/objspace.rbs +149 -90
  147. data/stdlib/openssl/0/openssl.rbs +8108 -71
  148. data/stdlib/optparse/0/optparse.rbs +487 -19
  149. data/stdlib/pathname/0/pathname.rbs +425 -124
  150. data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
  151. data/stdlib/prime/0/integer-extension.rbs +20 -2
  152. data/stdlib/prime/0/prime.rbs +88 -21
  153. data/stdlib/pstore/0/pstore.rbs +102 -0
  154. data/stdlib/pty/0/pty.rbs +64 -14
  155. data/stdlib/resolv/0/resolv.rbs +420 -31
  156. data/stdlib/rubygems/0/basic_specification.rbs +4 -1
  157. data/stdlib/rubygems/0/config_file.rbs +33 -1
  158. data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
  159. data/stdlib/rubygems/0/installer.rbs +13 -1
  160. data/stdlib/rubygems/0/path_support.rbs +4 -1
  161. data/stdlib/rubygems/0/platform.rbs +5 -1
  162. data/stdlib/rubygems/0/request_set.rbs +44 -2
  163. data/stdlib/rubygems/0/requirement.rbs +65 -2
  164. data/stdlib/rubygems/0/rubygems.rbs +407 -0
  165. data/stdlib/rubygems/0/source_list.rbs +13 -0
  166. data/stdlib/rubygems/0/specification.rbs +21 -1
  167. data/stdlib/rubygems/0/stream_ui.rbs +3 -1
  168. data/stdlib/rubygems/0/uninstaller.rbs +8 -1
  169. data/stdlib/rubygems/0/version.rbs +60 -157
  170. data/stdlib/securerandom/0/securerandom.rbs +44 -0
  171. data/stdlib/set/0/set.rbs +423 -109
  172. data/stdlib/shellwords/0/shellwords.rbs +55 -77
  173. data/stdlib/singleton/0/singleton.rbs +20 -0
  174. data/stdlib/socket/0/addrinfo.rbs +210 -9
  175. data/stdlib/socket/0/basic_socket.rbs +103 -11
  176. data/stdlib/socket/0/ip_socket.rbs +31 -9
  177. data/stdlib/socket/0/socket.rbs +586 -38
  178. data/stdlib/socket/0/tcp_server.rbs +22 -2
  179. data/stdlib/socket/0/tcp_socket.rbs +12 -1
  180. data/stdlib/socket/0/udp_socket.rbs +25 -2
  181. data/stdlib/socket/0/unix_server.rbs +22 -2
  182. data/stdlib/socket/0/unix_socket.rbs +45 -5
  183. data/stdlib/strscan/0/string_scanner.rbs +210 -9
  184. data/stdlib/tempfile/0/tempfile.rbs +58 -10
  185. data/stdlib/time/0/time.rbs +208 -116
  186. data/stdlib/timeout/0/timeout.rbs +10 -0
  187. data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
  188. data/stdlib/tsort/0/cyclic.rbs +1 -0
  189. data/stdlib/tsort/0/interfaces.rbs +1 -0
  190. data/stdlib/tsort/0/tsort.rbs +42 -0
  191. data/stdlib/uri/0/common.rbs +57 -8
  192. data/stdlib/uri/0/file.rbs +55 -109
  193. data/stdlib/uri/0/ftp.rbs +6 -3
  194. data/stdlib/uri/0/generic.rbs +558 -329
  195. data/stdlib/uri/0/http.rbs +60 -114
  196. data/stdlib/uri/0/https.rbs +8 -102
  197. data/stdlib/uri/0/ldap.rbs +143 -137
  198. data/stdlib/uri/0/ldaps.rbs +8 -102
  199. data/stdlib/uri/0/mailto.rbs +3 -0
  200. data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
  201. data/stdlib/uri/0/ws.rbs +6 -3
  202. data/stdlib/uri/0/wss.rbs +5 -3
  203. data/stdlib/yaml/0/dbm.rbs +151 -87
  204. data/stdlib/yaml/0/store.rbs +6 -0
  205. data/stdlib/zlib/0/zlib.rbs +90 -31
  206. metadata +18 -6
  207. data/lib/rbs/location.rb +0 -221
  208. data/sig/char_scanner.rbs +0 -9
data/core/hash.rbs CHANGED
@@ -1,73 +1,231 @@
1
- # A Hash is a dictionary-like collection of unique keys and their values. Also
2
- # called associative arrays, they are similar to Arrays, but where an Array uses
3
- # integers as its index, a Hash allows you to use any object type.
1
+ # <!-- rdoc-file=hash.c -->
2
+ # A Hash maps each of its unique keys to a specific value.
4
3
  #
5
- # Hashes enumerate their values in the order that the corresponding keys were
6
- # inserted.
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.
7
7
  #
8
- # A Hash can be easily created by using its implicit form:
9
8
  #
10
- # grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
9
+ # ### Hash Data Syntax
11
10
  #
12
- # Hashes allow an alternate syntax for keys that are symbols. Instead of
11
+ # The older syntax for Hash data uses the "hash rocket," `=>`:
13
12
  #
14
- # options = { :font_size => 10, :font_family => "Arial" }
13
+ # h = {:foo => 0, :bar => 1, :baz => 2}
14
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
15
15
  #
16
- # You could write it as:
16
+ # Alternatively, but only for a Hash key that's a Symbol, you can use a newer
17
+ # JSON-style syntax, where each bareword becomes a Symbol:
17
18
  #
18
- # options = { font_size: 10, font_family: "Arial" }
19
+ # h = {foo: 0, bar: 1, baz: 2}
20
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
19
21
  #
20
- # Each named key is a symbol you can access in hash:
22
+ # You can also use a String in place of a bareword:
21
23
  #
22
- # options[:font_size] # => 10
24
+ # h = {'foo': 0, 'bar': 1, 'baz': 2}
25
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
23
26
  #
24
- # A Hash can also be created through its ::new method:
27
+ # And you can mix the styles:
25
28
  #
26
- # grades = Hash.new
27
- # grades["Dorothy Doe"] = 9
29
+ # h = {foo: 0, :bar => 1, 'baz': 2}
30
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
28
31
  #
29
- # Hashes have a *default value* that is returned when accessing keys that do not
30
- # exist in the hash. If no default is set `nil` is used. You can set the default
31
- # value by sending it as an argument to Hash.new:
32
+ # But it's an error to try the JSON-style syntax for a key that's not a bareword
33
+ # or a String:
32
34
  #
33
- # grades = Hash.new(0)
35
+ # # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
36
+ # h = {0: 'zero'}
34
37
  #
35
- # Or by using the #default= method:
38
+ # Hash value can be omitted, meaning that value will be fetched from the context
39
+ # by the name of the key:
36
40
  #
37
- # grades = {"Timmy Doe" => 8}
38
- # grades.default = 0
41
+ # x = 0
42
+ # y = 100
43
+ # h = {x:, y:}
44
+ # h # => {:x=>0, :y=>100}
39
45
  #
40
- # Accessing a value in a Hash requires using its key:
46
+ # ### Common Uses
41
47
  #
42
- # puts grades["Jane Doe"] # => 0
48
+ # You can use a Hash to give names to objects:
43
49
  #
44
- # ### Common Uses
50
+ # person = {name: 'Matz', language: 'Ruby'}
51
+ # person # => {:name=>"Matz", :language=>"Ruby"}
45
52
  #
46
- # Hashes are an easy way to represent data structures, such as
53
+ # You can use a Hash to give names to method arguments:
47
54
  #
48
- # books = {}
49
- # books[:matz] = "The Ruby Programming Language"
50
- # books[:black] = "The Well-Grounded Rubyist"
55
+ # def some_method(hash)
56
+ # p hash
57
+ # end
58
+ # some_method({foo: 0, bar: 1, baz: 2}) # => {:foo=>0, :bar=>1, :baz=>2}
51
59
  #
52
- # Hashes are also commonly used as a way to have named parameters in functions.
53
- # Note that no brackets are used below. If a hash is the last argument on a
54
- # method call, no braces are needed, thus creating a really clean interface:
60
+ # Note: when the last argument in a method call is a Hash, the curly braces may
61
+ # be omitted:
55
62
  #
56
- # Person.create(name: "John Doe", age: 27)
63
+ # some_method(foo: 0, bar: 1, baz: 2) # => {:foo=>0, :bar=>1, :baz=>2}
57
64
  #
58
- # def self.create(params)
59
- # @name = params[:name]
60
- # @age = params[:age]
65
+ # You can use a Hash to initialize an object:
66
+ #
67
+ # class Dev
68
+ # attr_accessor :name, :language
69
+ # def initialize(hash)
70
+ # self.name = hash[:name]
71
+ # self.language = hash[:language]
72
+ # end
61
73
  # end
74
+ # matz = Dev.new(name: 'Matz', language: 'Ruby')
75
+ # matz # => #<Dev: @name="Matz", @language="Ruby">
76
+ #
77
+ # ### Creating a Hash
78
+ #
79
+ # You can create a Hash object explicitly with:
80
+ #
81
+ # * A [hash literal](doc/syntax/literals_rdoc.html#label-Hash+Literals).
82
+ #
83
+ #
84
+ # You can convert certain objects to Hashes with:
85
+ #
86
+ # * Method [Hash](Kernel.html#method-i-Hash).
87
+ #
88
+ #
89
+ # You can create a Hash by calling method Hash.new.
90
+ #
91
+ # Create an empty Hash:
92
+ #
93
+ # h = Hash.new
94
+ # h # => {}
95
+ # h.class # => Hash
96
+ #
97
+ # You can create a Hash by calling method Hash.[].
98
+ #
99
+ # Create an empty Hash:
100
+ #
101
+ # h = Hash[]
102
+ # h # => {}
103
+ #
104
+ # Create a Hash with initial entries:
105
+ #
106
+ # h = Hash[foo: 0, bar: 1, baz: 2]
107
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
108
+ #
109
+ # You can create a Hash by using its literal form (curly braces).
110
+ #
111
+ # Create an empty Hash:
112
+ #
113
+ # h = {}
114
+ # h # => {}
115
+ #
116
+ # Create a Hash with initial entries:
117
+ #
118
+ # h = {foo: 0, bar: 1, baz: 2}
119
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
120
+ #
121
+ # ### Hash Value Basics
122
+ #
123
+ # The simplest way to retrieve a Hash value (instance method #[]):
124
+ #
125
+ # h = {foo: 0, bar: 1, baz: 2}
126
+ # h[:foo] # => 0
127
+ #
128
+ # The simplest way to create or update a Hash value (instance method #[]=):
129
+ #
130
+ # h = {foo: 0, bar: 1, baz: 2}
131
+ # h[:bat] = 3 # => 3
132
+ # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
133
+ # h[:foo] = 4 # => 4
134
+ # h # => {:foo=>4, :bar=>1, :baz=>2, :bat=>3}
135
+ #
136
+ # The simplest way to delete a Hash entry (instance method #delete):
137
+ #
138
+ # h = {foo: 0, bar: 1, baz: 2}
139
+ # h.delete(:bar) # => 1
140
+ # h # => {:foo=>0, :baz=>2}
141
+ #
142
+ # ### Entry Order
143
+ #
144
+ # A Hash object presents its entries in the order of their creation. This is
145
+ # seen in:
146
+ #
147
+ # * Iterative methods such as `each`, `each_key`, `each_pair`, `each_value`.
148
+ # * Other order-sensitive methods such as `shift`, `keys`, `values`.
149
+ # * The String returned by method `inspect`.
150
+ #
151
+ #
152
+ # A new Hash has its initial ordering per the given entries:
153
+ #
154
+ # h = Hash[foo: 0, bar: 1]
155
+ # h # => {:foo=>0, :bar=>1}
156
+ #
157
+ # New entries are added at the end:
158
+ #
159
+ # h[:baz] = 2
160
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
161
+ #
162
+ # Updating a value does not affect the order:
163
+ #
164
+ # h[:baz] = 3
165
+ # h # => {:foo=>0, :bar=>1, :baz=>3}
166
+ #
167
+ # But re-creating a deleted entry can affect the order:
168
+ #
169
+ # h.delete(:foo)
170
+ # h[:foo] = 5
171
+ # h # => {:bar=>1, :baz=>3, :foo=>5}
62
172
  #
63
173
  # ### Hash Keys
64
174
  #
65
- # Two objects refer to the same hash key when their `hash` value is identical
66
- # and the two objects are `eql?` to each other.
175
+ # #### Hash Key Equivalence
176
+ #
177
+ # Two objects are treated as the same hash key when their `hash` value is
178
+ # identical and the two objects are `eql?` to each other.
179
+ #
180
+ # #### Modifying an Active Hash Key
181
+ #
182
+ # Modifying a Hash key while it is in use damages the hash's index.
183
+ #
184
+ # This Hash has keys that are Arrays:
185
+ #
186
+ # a0 = [ :foo, :bar ]
187
+ # a1 = [ :baz, :bat ]
188
+ # h = {a0 => 0, a1 => 1}
189
+ # h.include?(a0) # => true
190
+ # h[a0] # => 0
191
+ # a0.hash # => 110002110
67
192
  #
68
- # A user-defined class may be used as a hash key if the `hash` and `eql?`
69
- # methods are overridden to provide meaningful behavior. By default, separate
70
- # instances refer to separate hash keys.
193
+ # Modifying array element `a0[0]` changes its hash value:
194
+ #
195
+ # a0[0] = :bam
196
+ # a0.hash # => 1069447059
197
+ #
198
+ # And damages the Hash index:
199
+ #
200
+ # h.include?(a0) # => false
201
+ # h[a0] # => nil
202
+ #
203
+ # You can repair the hash index using method `rehash`:
204
+ #
205
+ # h.rehash # => {[:bam, :bar]=>0, [:baz, :bat]=>1}
206
+ # h.include?(a0) # => true
207
+ # h[a0] # => 0
208
+ #
209
+ # A String key is always safe. That's because an unfrozen String passed as a key
210
+ # will be replaced by a duplicated and frozen String:
211
+ #
212
+ # s = 'foo'
213
+ # s.frozen? # => false
214
+ # h = {s => 0}
215
+ # first_key = h.keys.first
216
+ # first_key.frozen? # => true
217
+ #
218
+ # #### User-Defined Hash Keys
219
+ #
220
+ # To be useable as a Hash key, objects must implement the methods `hash` and
221
+ # `eql?`. Note: this requirement does not apply if the Hash uses
222
+ # #compare_by_identity since comparison will then rely on the keys' object id
223
+ # instead of `hash` and `eql?`.
224
+ #
225
+ # Object defines basic implementation for `hash` and `eq?` that makes each
226
+ # object a distinct key. Typically, user-defined classes will want to override
227
+ # these methods to provide meaningful behavior, or for example inherit Struct
228
+ # that has useful definitions for these.
71
229
  #
72
230
  # A typical implementation of `hash` is based on the object's data while `eql?`
73
231
  # is usually aliased to the overridden `==` method:
@@ -81,8 +239,8 @@
81
239
  # end
82
240
  #
83
241
  # def ==(other)
84
- # self.class === other and
85
- # other.author == @author and
242
+ # self.class === other &&
243
+ # other.author == @author &&
86
244
  # other.title == @title
87
245
  # end
88
246
  #
@@ -103,933 +261,1708 @@
103
261
  #
104
262
  # reviews.length #=> 1
105
263
  #
106
- # See also Object#hash and Object#eql?
264
+ # ### Default Values
265
+ #
266
+ # The methods #[], #values_at and #dig need to return the value associated to a
267
+ # certain key. When that key is not found, that value will be determined by its
268
+ # default proc (if any) or else its default (initially `nil`).
269
+ #
270
+ # You can retrieve the default value with method #default:
271
+ #
272
+ # h = Hash.new
273
+ # h.default # => nil
274
+ #
275
+ # You can set the default value by passing an argument to method Hash.new or
276
+ # with method #default=
277
+ #
278
+ # h = Hash.new(-1)
279
+ # h.default # => -1
280
+ # h.default = 0
281
+ # h.default # => 0
282
+ #
283
+ # This default value is returned for #[], #values_at and #dig when a key is not
284
+ # found:
285
+ #
286
+ # counts = {foo: 42}
287
+ # counts.default # => nil (default)
288
+ # counts[:foo] = 42
289
+ # counts[:bar] # => nil
290
+ # counts.default = 0
291
+ # counts[:bar] # => 0
292
+ # counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
293
+ # counts.dig(:bar) # => 0
294
+ #
295
+ # Note that the default value is used without being duplicated. It is not
296
+ # advised to set the default value to a mutable object:
297
+ #
298
+ # synonyms = Hash.new([])
299
+ # synonyms[:hello] # => []
300
+ # synonyms[:hello] << :hi # => [:hi], but this mutates the default!
301
+ # synonyms.default # => [:hi]
302
+ # synonyms[:world] << :universe
303
+ # synonyms[:world] # => [:hi, :universe], oops
304
+ # synonyms.keys # => [], oops
305
+ #
306
+ # To use a mutable object as default, it is recommended to use a default proc
307
+ #
308
+ # #### Default Proc
309
+ #
310
+ # When the default proc for a Hash is set (i.e., not `nil`), the default value
311
+ # returned by method #[] is determined by the default proc alone.
312
+ #
313
+ # You can retrieve the default proc with method #default_proc:
314
+ #
315
+ # h = Hash.new
316
+ # h.default_proc # => nil
317
+ #
318
+ # You can set the default proc by calling Hash.new with a block or calling the
319
+ # method #default_proc=
320
+ #
321
+ # h = Hash.new { |hash, key| "Default value for #{key}" }
322
+ # h.default_proc.class # => Proc
323
+ # h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
324
+ # h.default_proc.class # => Proc
325
+ #
326
+ # When the default proc is set (i.e., not `nil`) and method #[] is called with
327
+ # with a non-existent key, #[] calls the default proc with both the Hash object
328
+ # itself and the missing key, then returns the proc's return value:
329
+ #
330
+ # h = Hash.new { |hash, key| "Default value for #{key}" }
331
+ # h[:nosuch] # => "Default value for nosuch"
332
+ #
333
+ # Note that in the example above no entry for key `:nosuch` is created:
334
+ #
335
+ # h.include?(:nosuch) # => false
336
+ #
337
+ # However, the proc itself can add a new entry:
338
+ #
339
+ # synonyms = Hash.new { |hash, key| hash[key] = [] }
340
+ # synonyms.include?(:hello) # => false
341
+ # synonyms[:hello] << :hi # => [:hi]
342
+ # synonyms[:world] << :universe # => [:universe]
343
+ # synonyms.keys # => [:hello, :world]
344
+ #
345
+ # Note that setting the default proc will clear the default value and vice
346
+ # versa.
347
+ #
348
+ # ### What's Here
349
+ #
350
+ # First, what's elsewhere. Class Hash:
351
+ #
352
+ # * Inherits from [class
353
+ # Object](Object.html#class-Object-label-What-27s+Here).
354
+ # * Includes [module
355
+ # Enumerable](Enumerable.html#module-Enumerable-label-What-27s+Here), which
356
+ # provides dozens of additional methods.
357
+ #
358
+ #
359
+ # Here, class Hash provides methods that are useful for:
360
+ #
361
+ # * [Creating a Hash](#class-Hash-label-Methods+for+Creating+a+Hash)
362
+ # * [Setting Hash State](#class-Hash-label-Methods+for+Setting+Hash+State)
363
+ # * [Querying](#class-Hash-label-Methods+for+Querying)
364
+ # * [Comparing](#class-Hash-label-Methods+for+Comparing)
365
+ # * [Fetching](#class-Hash-label-Methods+for+Fetching)
366
+ # * [Assigning](#class-Hash-label-Methods+for+Assigning)
367
+ # * [Deleting](#class-Hash-label-Methods+for+Deleting)
368
+ # * [Iterating](#class-Hash-label-Methods+for+Iterating)
369
+ # * [Converting](#class-Hash-label-Methods+for+Converting)
370
+ # * [Transforming Keys and
371
+ # Values](#class-Hash-label-Methods+for+Transforming+Keys+and+Values)
372
+ # * [And more....](#class-Hash-label-Other+Methods)
373
+ #
374
+ #
375
+ # Class Hash also includes methods from module Enumerable.
376
+ #
377
+ # #### Methods for Creating a Hash
378
+ #
379
+ # ::[]
380
+ # : Returns a new hash populated with given objects.
381
+ # ::new
382
+ # : Returns a new empty hash.
383
+ # ::try_convert
384
+ # : Returns a new hash created from a given object.
385
+ #
386
+ #
387
+ # #### Methods for Setting Hash State
388
+ #
389
+ # #compare_by_identity
390
+ # : Sets `self` to consider only identity in comparing keys.
391
+ # #default=
392
+ # : Sets the default to a given value.
393
+ # #default_proc=
394
+ # : Sets the default proc to a given proc.
395
+ # #rehash
396
+ # : Rebuilds the hash table by recomputing the hash index for each key.
397
+ #
398
+ #
399
+ # #### Methods for Querying
400
+ #
401
+ # #any?
402
+ # : Returns whether any element satisfies a given criterion.
403
+ # #compare_by_identity?
404
+ # : Returns whether the hash considers only identity when comparing keys.
405
+ # #default
406
+ # : Returns the default value, or the default value for a given key.
407
+ # #default_proc
408
+ # : Returns the default proc.
409
+ # #empty?
410
+ # : Returns whether there are no entries.
411
+ # #eql?
412
+ # : Returns whether a given object is equal to `self`.
413
+ # #hash
414
+ # : Returns the integer hash code.
415
+ # #has_value?
416
+ # : Returns whether a given object is a value in `self`.
417
+ # #include?, #has_key?, #member?, #key?
418
+ # : Returns whether a given object is a key in `self`.
419
+ # #length, #size
420
+ # : Returns the count of entries.
421
+ # #value?
422
+ # : Returns whether a given object is a value in `self`.
423
+ #
424
+ #
425
+ # #### Methods for Comparing
426
+ #
427
+ # [#<](#method-i-3C)
428
+ # : Returns whether `self` is a proper subset of a given object.
429
+ # [#<=](#method-i-3C-3D)
430
+ # : Returns whether `self` is a subset of a given object.
431
+ # [#==](#method-i-3D-3D)
432
+ # : Returns whether a given object is equal to `self`.
433
+ # [#>](#method-i-3E)
434
+ # : Returns whether `self` is a proper superset of a given object
435
+ # [#>=](#method-i-3E-3D)
436
+ # : Returns whether `self` is a proper superset of a given object.
437
+ #
438
+ #
439
+ # #### Methods for Fetching
440
+ #
441
+ # #[]
442
+ # : Returns the value associated with a given key.
443
+ # #assoc
444
+ # : Returns a 2-element array containing a given key and its value.
445
+ # #dig
446
+ # : Returns the object in nested objects that is specified by a given key and
447
+ # additional arguments.
448
+ # #fetch
449
+ # : Returns the value for a given key.
450
+ # #fetch_values
451
+ # : Returns array containing the values associated with given keys.
452
+ # #key
453
+ # : Returns the key for the first-found entry with a given value.
454
+ # #keys
455
+ # : Returns an array containing all keys in `self`.
456
+ # #rassoc
457
+ # : Returns a 2-element array consisting of the key and value of the
458
+ # first-found entry having a given value.
459
+ # #values
460
+ # : Returns an array containing all values in `self`/
461
+ # #values_at
462
+ # : Returns an array containing values for given keys.
463
+ #
464
+ #
465
+ # #### Methods for Assigning
466
+ #
467
+ # #[]=, #store
468
+ # : Associates a given key with a given value.
469
+ # #merge
470
+ # : Returns the hash formed by merging each given hash into a copy of `self`.
471
+ # #merge!, #update
472
+ # : Merges each given hash into `self`.
473
+ # #replace
474
+ # : Replaces the entire contents of `self` with the contents of a givan hash.
475
+ #
476
+ #
477
+ # #### Methods for Deleting
478
+ #
479
+ # These methods remove entries from `self`:
480
+ #
481
+ # #clear
482
+ # : Removes all entries from `self`.
483
+ # #compact!
484
+ # : Removes all `nil`-valued entries from `self`.
485
+ # #delete
486
+ # : Removes the entry for a given key.
487
+ # #delete_if
488
+ # : Removes entries selected by a given block.
489
+ # #filter!, #select!
490
+ # : Keep only those entries selected by a given block.
491
+ # #keep_if
492
+ # : Keep only those entries selected by a given block.
493
+ # #reject!
494
+ # : Removes entries selected by a given block.
495
+ # #shift
496
+ # : Removes and returns the first entry.
497
+ #
498
+ #
499
+ # These methods return a copy of `self` with some entries removed:
500
+ #
501
+ # #compact
502
+ # : Returns a copy of `self` with all `nil`-valued entries removed.
503
+ # #except
504
+ # : Returns a copy of `self` with entries removed for specified keys.
505
+ # #filter, #select
506
+ # : Returns a copy of `self` with only those entries selected by a given
507
+ # block.
508
+ # #reject
509
+ # : Returns a copy of `self` with entries removed as specified by a given
510
+ # block.
511
+ # #slice
512
+ # : Returns a hash containing the entries for given keys.
513
+ #
514
+ #
515
+ # #### Methods for Iterating
516
+ # #each, #each_pair
517
+ # : Calls a given block with each key-value pair.
518
+ # #each_key
519
+ # : Calls a given block with each key.
520
+ # #each_value
521
+ # : Calls a given block with each value.
522
+ #
523
+ #
524
+ # #### Methods for Converting
525
+ #
526
+ # #inspect, #to_s
527
+ # : Returns a new String containing the hash entries.
528
+ # #to_a
529
+ # : Returns a new array of 2-element arrays; each nested array contains a
530
+ # key-value pair from `self`.
531
+ # #to_h
532
+ # : Returns `self` if a Hash; if a subclass of Hash, returns a Hash containing
533
+ # the entries from `self`.
534
+ # #to_hash
535
+ # : Returns `self`.
536
+ # #to_proc
537
+ # : Returns a proc that maps a given key to its value.
538
+ #
539
+ #
540
+ # #### Methods for Transforming Keys and Values
541
+ #
542
+ # #transform_keys
543
+ # : Returns a copy of `self` with modified keys.
544
+ # #transform_keys!
545
+ # : Modifies keys in `self`
546
+ # #transform_values
547
+ # : Returns a copy of `self` with modified values.
548
+ # #transform_values!
549
+ # : Modifies values in `self`.
550
+ #
551
+ #
552
+ # #### Other Methods
553
+ # #flatten
554
+ # : Returns an array that is a 1-dimensional flattening of `self`.
555
+ # #invert
556
+ # : Returns a hash with the each key-value pair inverted.
107
557
  #
108
558
  class Hash[unchecked out K, unchecked out V] < Object
109
- include Enumerable[[K, V]]
559
+ include Enumerable[[ K, V ]]
110
560
 
111
- # Creates a new hash populated with the given objects.
561
+ # <!--
562
+ # rdoc-file=hash.c
563
+ # - Hash[] -> new_empty_hash
564
+ # - Hash[hash] -> new_hash
565
+ # - Hash[ [*2_element_arrays] ] -> new_hash
566
+ # - Hash[*objects] -> new_hash
567
+ # -->
568
+ # Returns a new Hash object populated with the given objects, if any. See
569
+ # Hash::new.
570
+ #
571
+ # With no argument, returns a new empty Hash.
572
+ #
573
+ # When the single given argument is a Hash, returns a new Hash populated with
574
+ # the entries from the given Hash, excluding the default value or proc.
575
+ #
576
+ # h = {foo: 0, bar: 1, baz: 2}
577
+ # Hash[h] # => {:foo=>0, :bar=>1, :baz=>2}
578
+ #
579
+ # When the single given argument is an Array of 2-element Arrays, returns a new
580
+ # Hash object wherein each 2-element array forms a key-value entry:
581
+ #
582
+ # Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {:foo=>0, :bar=>1}
112
583
  #
113
- # Similar to the literal `{ *key* => *value*, ... }`. In the first form, keys
114
- # and values occur in pairs, so there must be an even number of arguments.
584
+ # When the argument count is an even number; returns a new Hash object wherein
585
+ # each successive pair of arguments has become a key-value entry:
115
586
  #
116
- # The second and third form take a single argument which is either an array of
117
- # key-value pairs or an object convertible to a hash.
587
+ # Hash[:foo, 0, :bar, 1] # => {:foo=>0, :bar=>1}
118
588
  #
119
- # Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}
120
- # Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200}
121
- # Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200}
589
+ # Raises an exception if the argument list does not conform to any of the above.
122
590
  #
123
591
  def self.[]: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
124
592
  | [U, V] (Array[[ U, V ]]) -> ::Hash[U, V]
125
593
  | (*untyped) -> ::Hash[untyped, untyped]
126
594
 
127
- # Try to convert *obj* into a hash, using to_hash method. Returns converted hash
128
- # or nil if *obj* cannot be converted for any reason.
595
+ # <!--
596
+ # rdoc-file=hash.c
597
+ # - Hash.try_convert(obj) -> obj, new_hash, or nil
598
+ # -->
599
+ # If `obj` is a Hash object, returns `obj`.
129
600
  #
130
- # Hash.try_convert({1=>2}) # => {1=>2}
131
- # Hash.try_convert("1=>2") # => nil
601
+ # Otherwise if `obj` responds to `:to_hash`, calls `obj.to_hash` and returns the
602
+ # result.
603
+ #
604
+ # Returns `nil` if `obj` does not respond to `:to_hash`
605
+ #
606
+ # Raises an exception unless `obj.to_hash` returns a Hash object.
132
607
  #
133
608
  def self.try_convert: [U, V] (_ToHash[U, V]) -> ::Hash[U, V]
134
609
  | (untyped) -> (::Hash[untyped, untyped] | nil)
135
610
 
136
611
  public
137
612
 
138
- # Returns `true` if *hash* is subset of *other*.
139
- #
140
- # h1 = {a:1, b:2}
141
- # h2 = {a:1, b:2, c:3}
142
- # h1 < h2 #=> true
143
- # h2 < h1 #=> false
144
- # h1 < h1 #=> false
613
+ # <!--
614
+ # rdoc-file=hash.c
615
+ # - hash < other_hash -> true or false
616
+ # -->
617
+ # Returns `true` if `hash` is a proper subset of `other_hash`, `false`
618
+ # otherwise:
619
+ # h1 = {foo: 0, bar: 1}
620
+ # h2 = {foo: 0, bar: 1, baz: 2}
621
+ # h1 < h2 # => true
622
+ # h2 < h1 # => false
623
+ # h1 < h1 # => false
145
624
  #
146
625
  def <: [A, B] (::Hash[A, B]) -> bool
147
626
 
148
- # Returns `true` if *hash* is subset of *other* or equals to *other*.
149
- #
150
- # h1 = {a:1, b:2}
151
- # h2 = {a:1, b:2, c:3}
152
- # h1 <= h2 #=> true
153
- # h2 <= h1 #=> false
154
- # h1 <= h1 #=> true
627
+ # <!--
628
+ # rdoc-file=hash.c
629
+ # - hash <= other_hash -> true or false
630
+ # -->
631
+ # Returns `true` if `hash` is a subset of `other_hash`, `false` otherwise:
632
+ # h1 = {foo: 0, bar: 1}
633
+ # h2 = {foo: 0, bar: 1, baz: 2}
634
+ # h1 <= h2 # => true
635
+ # h2 <= h1 # => false
636
+ # h1 <= h1 # => true
155
637
  #
156
638
  def <=: [A, B] (::Hash[A, B]) -> bool
157
639
 
158
- # Equality---Two hashes are equal if they each contain the same number of keys
159
- # and if each key-value pair is equal to (according to Object#==) the
160
- # corresponding elements in the other hash.
640
+ # <!--
641
+ # rdoc-file=hash.c
642
+ # - hash == object -> true or false
643
+ # -->
644
+ # Returns `true` if all of the following are true:
645
+ # * `object` is a Hash object.
646
+ # * `hash` and `object` have the same keys (regardless of order).
647
+ # * For each key `key`, `hash[key] == object[key]`.
161
648
  #
162
- # h1 = { "a" => 1, "c" => 2 }
163
- # h2 = { 7 => 35, "c" => 2, "a" => 1 }
164
- # h3 = { "a" => 1, "c" => 2, 7 => 35 }
165
- # h4 = { "a" => 1, "d" => 2, "f" => 35 }
166
- # h1 == h2 #=> false
167
- # h2 == h3 #=> true
168
- # h3 == h4 #=> false
169
649
  #
170
- # The orders of each hashes are not compared.
650
+ # Otherwise, returns `false`.
171
651
  #
172
- # h1 = { "a" => 1, "c" => 2 }
173
- # h2 = { "c" => 2, "a" => 1 }
174
- # h1 == h2 #=> true
652
+ # Equal:
653
+ # h1 = {foo: 0, bar: 1, baz: 2}
654
+ # h2 = {foo: 0, bar: 1, baz: 2}
655
+ # h1 == h2 # => true
656
+ # h3 = {baz: 2, bar: 1, foo: 0}
657
+ # h1 == h3 # => true
175
658
  #
176
659
  def ==: (untyped other) -> bool
177
660
 
178
- # Returns `true` if *other* is subset of *hash*.
179
- #
180
- # h1 = {a:1, b:2}
181
- # h2 = {a:1, b:2, c:3}
182
- # h1 > h2 #=> false
183
- # h2 > h1 #=> true
184
- # h1 > h1 #=> false
661
+ # <!--
662
+ # rdoc-file=hash.c
663
+ # - hash > other_hash -> true or false
664
+ # -->
665
+ # Returns `true` if `hash` is a proper superset of `other_hash`, `false`
666
+ # otherwise:
667
+ # h1 = {foo: 0, bar: 1, baz: 2}
668
+ # h2 = {foo: 0, bar: 1}
669
+ # h1 > h2 # => true
670
+ # h2 > h1 # => false
671
+ # h1 > h1 # => false
185
672
  #
186
673
  def >: [A, B] (::Hash[A, B]) -> bool
187
674
 
188
- # Returns `true` if *other* is subset of *hash* or equals to *hash*.
189
- #
190
- # h1 = {a:1, b:2}
191
- # h2 = {a:1, b:2, c:3}
192
- # h1 >= h2 #=> false
193
- # h2 >= h1 #=> true
194
- # h1 >= h1 #=> true
675
+ # <!--
676
+ # rdoc-file=hash.c
677
+ # - hash >= other_hash -> true or false
678
+ # -->
679
+ # Returns `true` if `hash` is a superset of `other_hash`, `false` otherwise:
680
+ # h1 = {foo: 0, bar: 1, baz: 2}
681
+ # h2 = {foo: 0, bar: 1}
682
+ # h1 >= h2 # => true
683
+ # h2 >= h1 # => false
684
+ # h1 >= h1 # => true
195
685
  #
196
686
  def >=: [A, B] (::Hash[A, B]) -> bool
197
687
 
198
- # Element Reference---Retrieves the *value* object corresponding to the *key*
199
- # object. If not found, returns the default value (see Hash::new for details).
688
+ # <!--
689
+ # rdoc-file=hash.c
690
+ # - hash[key] -> value
691
+ # -->
692
+ # Returns the value associated with the given `key`, if found:
693
+ # h = {foo: 0, bar: 1, baz: 2}
694
+ # h[:foo] # => 0
200
695
  #
201
- # h = { "a" => 100, "b" => 200 }
202
- # h["a"] #=> 100
203
- # h["c"] #=> nil
696
+ # If `key` is not found, returns a default value (see [Default
697
+ # Values](#class-Hash-label-Default+Values)):
698
+ # h = {foo: 0, bar: 1, baz: 2}
699
+ # h[:nosuch] # => nil
204
700
  #
205
701
  def []: (K arg0) -> V
206
702
 
207
- # ## Element Assignment
208
- #
209
- # Associates the value given by `value` with the key given by `key`.
210
- #
211
- # h = { "a" => 100, "b" => 200 }
212
- # h["a"] = 9
213
- # h["c"] = 4
214
- # h #=> {"a"=>9, "b"=>200, "c"=>4}
215
- # h.store("d", 42) #=> 42
216
- # h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
217
- #
218
- # `key` should not have its value changed while it is in use as a key (an
219
- # `unfrozen String` passed as a key will be duplicated and frozen).
220
- #
221
- # a = "a"
222
- # b = "b".freeze
223
- # h = { a => 100, b => 200 }
224
- # h.key(100).equal? a #=> false
225
- # h.key(200).equal? b #=> true
703
+ # <!--
704
+ # rdoc-file=hash.c
705
+ # - hash[key] = value -> value
706
+ # - hash.store(key, value)
707
+ # -->
708
+ # Hash#store is an alias for Hash#[]=.
709
+ #
710
+ # Associates the given `value` with the given `key`; returns `value`.
711
+ #
712
+ # If the given `key` exists, replaces its value with the given `value`; the
713
+ # ordering is not affected (see [Entry Order](#class-Hash-label-Entry+Order)):
714
+ # h = {foo: 0, bar: 1}
715
+ # h[:foo] = 2 # => 2
716
+ # h.store(:bar, 3) # => 3
717
+ # h # => {:foo=>2, :bar=>3}
718
+ #
719
+ # If `key` does not exist, adds the `key` and `value`; the new entry is last in
720
+ # the order (see [Entry Order](#class-Hash-label-Entry+Order)):
721
+ # h = {foo: 0, bar: 1}
722
+ # h[:baz] = 2 # => 2
723
+ # h.store(:bat, 3) # => 3
724
+ # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
226
725
  #
227
726
  def []=: (K arg0, V arg1) -> V
228
727
 
229
- # See also Enumerable#any?
728
+ # <!--
729
+ # rdoc-file=hash.c
730
+ # - hash.any? -> true or false
731
+ # - hash.any?(object) -> true or false
732
+ # - hash.any? {|key, value| ... } -> true or false
733
+ # -->
734
+ # Returns `true` if any element satisfies a given criterion; `false` otherwise.
735
+ #
736
+ # With no argument and no block, returns `true` if `self` is non-empty; `false`
737
+ # if empty.
738
+ #
739
+ # With argument `object` and no block, returns `true` if for any key `key`
740
+ # `h.assoc(key) == object`:
741
+ # h = {foo: 0, bar: 1, baz: 2}
742
+ # h.any?([:bar, 1]) # => true
743
+ # h.any?([:bar, 0]) # => false
744
+ # h.any?([:baz, 1]) # => false
745
+ #
746
+ # With no argument and a block, calls the block with each key-value pair;
747
+ # returns `true` if the block returns any truthy value, `false` otherwise:
748
+ # h = {foo: 0, bar: 1, baz: 2}
749
+ # h.any? {|key, value| value < 3 } # => true
750
+ # h.any? {|key, value| value > 3 } # => false
230
751
  #
231
752
  def any?: () -> bool
232
753
  | (untyped pattern) -> bool
233
754
  | () { (K, V) -> boolish } -> bool
234
755
 
235
- # Searches through the hash comparing *obj* with the key using `==`. Returns the
236
- # key-value pair (two elements array) or `nil` if no match is found. See
237
- # Array#assoc.
756
+ # <!--
757
+ # rdoc-file=hash.c
758
+ # - hash.assoc(key) -> new_array or nil
759
+ # -->
760
+ # If the given `key` is found, returns a 2-element Array containing that key and
761
+ # its value:
762
+ # h = {foo: 0, bar: 1, baz: 2}
763
+ # h.assoc(:bar) # => [:bar, 1]
238
764
  #
239
- # h = {"colors" => ["red", "blue", "green"],
240
- # "letters" => ["a", "b", "c" ]}
241
- # h.assoc("letters") #=> ["letters", ["a", "b", "c"]]
242
- # h.assoc("foo") #=> nil
765
+ # Returns `nil` if key `key` is not found.
243
766
  #
244
- def assoc: (K arg0) -> [K, V]?
767
+ def assoc: (K arg0) -> [ K, V ]?
245
768
 
246
- # Removes all key-value pairs from *hsh*.
247
- #
248
- # h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
249
- # h.clear #=> {}
769
+ # <!--
770
+ # rdoc-file=hash.c
771
+ # - hash.clear -> self
772
+ # -->
773
+ # Removes all hash entries; returns `self`.
250
774
  #
251
775
  def clear: () -> self
252
776
 
253
- # Returns a new hash with the nil values/key pairs removed
254
- #
255
- # h = { a: 1, b: false, c: nil }
256
- # h.compact #=> { a: 1, b: false }
257
- # h #=> { a: 1, b: false, c: nil }
777
+ # <!--
778
+ # rdoc-file=hash.c
779
+ # - hash.compact -> new_hash
780
+ # -->
781
+ # Returns a copy of `self` with all `nil`-valued entries removed:
782
+ # h = {foo: 0, bar: nil, baz: 2, bat: nil}
783
+ # h1 = h.compact
784
+ # h1 # => {:foo=>0, :baz=>2}
258
785
  #
259
786
  def compact: () -> ::Hash[K, V]
260
787
 
261
- # Removes all nil values from the hash. Returns nil if no changes were made,
262
- # otherwise returns the hash.
788
+ # <!--
789
+ # rdoc-file=hash.c
790
+ # - hash.compact! -> self or nil
791
+ # -->
792
+ # Returns `self` with all its `nil`-valued entries removed (in place):
793
+ # h = {foo: 0, bar: nil, baz: 2, bat: nil}
794
+ # h.compact! # => {:foo=>0, :baz=>2}
263
795
  #
264
- # h = { a: 1, b: false, c: nil }
265
- # h.compact! #=> { a: 1, b: false }
796
+ # Returns `nil` if no entries were removed.
266
797
  #
267
798
  def compact!: () -> self?
268
799
 
269
- # Makes *hsh* compare its keys by their identity, i.e. it will consider exact
270
- # same objects as same keys.
271
- #
272
- # h1 = { "a" => 100, "b" => 200, :c => "c" }
273
- # h1["a"] #=> 100
274
- # h1.compare_by_identity
275
- # h1.compare_by_identity? #=> true
276
- # h1["a".dup] #=> nil # different objects.
277
- # h1[:c] #=> "c" # same symbols are all same.
800
+ # <!--
801
+ # rdoc-file=hash.c
802
+ # - hash.compare_by_identity -> self
803
+ # -->
804
+ # Sets `self` to consider only identity in comparing keys; two keys are
805
+ # considered the same only if they are the same object; returns `self`.
806
+ #
807
+ # By default, these two object are considered to be the same key, so `s1` will
808
+ # overwrite `s0`:
809
+ # s0 = 'x'
810
+ # s1 = 'x'
811
+ # h = {}
812
+ # h.compare_by_identity? # => false
813
+ # h[s0] = 0
814
+ # h[s1] = 1
815
+ # h # => {"x"=>1}
816
+ #
817
+ # After calling #compare_by_identity, the keys are considered to be different,
818
+ # and therefore do not overwrite each other:
819
+ # h = {}
820
+ # h.compare_by_identity # => {}
821
+ # h.compare_by_identity? # => true
822
+ # h[s0] = 0
823
+ # h[s1] = 1
824
+ # h # => {"x"=>0, "x"=>1}
278
825
  #
279
826
  def compare_by_identity: () -> self
280
827
 
281
- # Returns `true` if *hsh* will compare its keys by their identity. Also see
282
- # Hash#compare_by_identity.
828
+ # <!--
829
+ # rdoc-file=hash.c
830
+ # - hash.compare_by_identity? -> true or false
831
+ # -->
832
+ # Returns `true` if #compare_by_identity has been called, `false` otherwise.
283
833
  #
284
834
  def compare_by_identity?: () -> bool
285
835
 
836
+ # <!--
837
+ # rdoc-file=hash.c
838
+ # - deconstruct_keys(p1)
839
+ # -->
840
+ #
286
841
  def deconstruct_keys: (Array[K] | nil) -> self
287
842
 
288
- # Returns the default value, the value that would be returned by *[hsh](key)* if
289
- # *key* did not exist in *hsh*. See also Hash::new and Hash#default=.
290
- #
291
- # h = Hash.new #=> {}
292
- # h.default #=> nil
293
- # h.default(2) #=> nil
294
- #
295
- # h = Hash.new("cat") #=> {}
296
- # h.default #=> "cat"
297
- # h.default(2) #=> "cat"
298
- #
299
- # h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
300
- # h.default #=> nil
301
- # h.default(2) #=> 20
843
+ # <!--
844
+ # rdoc-file=hash.c
845
+ # - hash.default -> object
846
+ # - hash.default(key) -> object
847
+ # -->
848
+ # Returns the default value for the given `key`. The returned value will be
849
+ # determined either by the default proc or by the default value. See [Default
850
+ # Values](#class-Hash-label-Default+Values).
851
+ #
852
+ # With no argument, returns the current default value:
853
+ # h = {}
854
+ # h.default # => nil
855
+ #
856
+ # If `key` is given, returns the default value for `key`, regardless of whether
857
+ # that key exists:
858
+ # h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
859
+ # h[:foo] = "Hello"
860
+ # h.default(:foo) # => "No key foo"
302
861
  #
303
862
  def default: (?K arg0) -> V?
304
863
 
305
- # Sets the default value, the value returned for a key that does not exist in
306
- # the hash. It is not possible to set the default to a Proc that will be
307
- # executed on each key lookup.
308
- #
309
- # h = { "a" => 100, "b" => 200 }
310
- # h.default = "Go fish"
311
- # h["a"] #=> 100
312
- # h["z"] #=> "Go fish"
313
- # # This doesn't do what you might hope...
314
- # h.default = proc do |hash, key|
315
- # hash[key] = key + key
316
- # end
317
- # h[2] #=> #<Proc:0x401b3948@-:6>
318
- # h["cat"] #=> #<Proc:0x401b3948@-:6>
864
+ # <!--
865
+ # rdoc-file=hash.c
866
+ # - hash.default = value -> object
867
+ # -->
868
+ # Sets the default value to `value`; returns `value`:
869
+ # h = {}
870
+ # h.default # => nil
871
+ # h.default = false # => false
872
+ # h.default # => false
873
+ #
874
+ # See [Default Values](#class-Hash-label-Default+Values).
319
875
  #
320
876
  def default=: (V arg0) -> V
321
877
 
322
- # If Hash::new was invoked with a block, return that block, otherwise return
323
- # `nil`.
324
- #
325
- # h = Hash.new {|h,k| h[k] = k*k } #=> {}
326
- # p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
327
- # a = [] #=> []
328
- # p.call(a, 2)
329
- # a #=> [nil, nil, 4]
878
+ # <!--
879
+ # rdoc-file=hash.c
880
+ # - hash.default_proc -> proc or nil
881
+ # -->
882
+ # Returns the default proc for `self` (see [Default
883
+ # Values](#class-Hash-label-Default+Values)):
884
+ # h = {}
885
+ # h.default_proc # => nil
886
+ # h.default_proc = proc {|hash, key| "Default value for #{key}" }
887
+ # h.default_proc.class # => Proc
330
888
  #
331
889
  def default_proc: () -> (Proc | nil)
332
890
 
333
- # Sets the default proc to be executed on each failed key lookup.
334
- #
335
- # h.default_proc = proc do |hash, key|
336
- # hash[key] = key + key
337
- # end
338
- # h[2] #=> 4
339
- # h["cat"] #=> "catcat"
891
+ # <!--
892
+ # rdoc-file=hash.c
893
+ # - hash.default_proc = proc -> proc
894
+ # -->
895
+ # Sets the default proc for `self` to `proc`: (see [Default
896
+ # Values](#class-Hash-label-Default+Values)):
897
+ # h = {}
898
+ # h.default_proc # => nil
899
+ # h.default_proc = proc { |hash, key| "Default value for #{key}" }
900
+ # h.default_proc.class # => Proc
901
+ # h.default_proc = nil
902
+ # h.default_proc # => nil
340
903
  #
341
904
  def default_proc=: (Proc | _ToProc | nil) -> (Proc | _ToProc | nil)
342
905
 
343
- # Deletes the key-value pair and returns the value from *hsh* whose key is equal
344
- # to *key*. If the key is not found, it returns *nil*. If the optional code
345
- # block is given and the key is not found, pass in the key and return the result
346
- # of *block*.
347
- #
348
- # h = { "a" => 100, "b" => 200 }
349
- # h.delete("a") #=> 100
350
- # h.delete("z") #=> nil
351
- # h.delete("z") { |el| "#{el} not found" } #=> "z not found"
906
+ # <!--
907
+ # rdoc-file=hash.c
908
+ # - hash.delete(key) -> value or nil
909
+ # - hash.delete(key) {|key| ... } -> object
910
+ # -->
911
+ # Deletes the entry for the given `key` and returns its associated value.
912
+ #
913
+ # If no block is given and `key` is found, deletes the entry and returns the
914
+ # associated value:
915
+ # h = {foo: 0, bar: 1, baz: 2}
916
+ # h.delete(:bar) # => 1
917
+ # h # => {:foo=>0, :baz=>2}
918
+ #
919
+ # If no block given and `key` is not found, returns `nil`.
920
+ #
921
+ # If a block is given and `key` is found, ignores the block, deletes the entry,
922
+ # and returns the associated value:
923
+ # h = {foo: 0, bar: 1, baz: 2}
924
+ # h.delete(:baz) { |key| raise 'Will never happen'} # => 2
925
+ # h # => {:foo=>0, :bar=>1}
926
+ #
927
+ # If a block is given and `key` is not found, calls the block and returns the
928
+ # block's return value:
929
+ # h = {foo: 0, bar: 1, baz: 2}
930
+ # h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
931
+ # h # => {:foo=>0, :bar=>1, :baz=>2}
352
932
  #
353
933
  def delete: (K arg0) -> V?
354
934
  | [U] (K arg0) { (K arg0) -> U } -> (U | V)
355
935
 
356
- # Deletes every key-value pair from *hsh* for which *block* evaluates to `true`.
357
- #
358
- # If no block is given, an enumerator is returned instead.
359
- #
360
- # h = { "a" => 100, "b" => 200, "c" => 300 }
361
- # h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
936
+ # <!--
937
+ # rdoc-file=hash.c
938
+ # - hash.delete_if {|key, value| ... } -> self
939
+ # - hash.delete_if -> new_enumerator
940
+ # -->
941
+ # If a block given, calls the block with each key-value pair; deletes each entry
942
+ # for which the block returns a truthy value; returns `self`:
943
+ # h = {foo: 0, bar: 1, baz: 2}
944
+ # h.delete_if {|key, value| value > 0 } # => {:foo=>0}
945
+ #
946
+ # If no block given, returns a new Enumerator:
947
+ # h = {foo: 0, bar: 1, baz: 2}
948
+ # e = h.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
949
+ # e.each { |key, value| value > 0 } # => {:foo=>0}
362
950
  #
363
951
  def delete_if: () { (K, V) -> boolish } -> self
364
952
  | () -> ::Enumerator[[ K, V ], self]
365
953
 
366
- # Extracts the nested value specified by the sequence of *key* objects by
367
- # calling `dig` at each step, returning `nil` if any intermediate step is `nil`.
368
- #
369
- # h = { foo: {bar: {baz: 1}}}
370
- #
371
- # h.dig(:foo, :bar, :baz) #=> 1
372
- # h.dig(:foo, :zot, :xyz) #=> nil
373
- #
374
- # g = { foo: [10, 11, 12] }
375
- # g.dig(:foo, 1) #=> 11
376
- # g.dig(:foo, 1, 0) #=> TypeError: Integer does not have #dig method
377
- # g.dig(:foo, :bar) #=> TypeError: no implicit conversion of Symbol into Integer
954
+ # <!--
955
+ # rdoc-file=hash.c
956
+ # - hash.dig(key, *identifiers) -> object
957
+ # -->
958
+ # Finds and returns the object in nested objects that is specified by `key` and
959
+ # `identifiers`. The nested objects may be instances of various classes. See
960
+ # [Dig Methods](rdoc-ref:dig_methods.rdoc).
961
+ #
962
+ # Nested Hashes:
963
+ # h = {foo: {bar: {baz: 2}}}
964
+ # h.dig(:foo) # => {:bar=>{:baz=>2}}
965
+ # h.dig(:foo, :bar) # => {:baz=>2}
966
+ # h.dig(:foo, :bar, :baz) # => 2
967
+ # h.dig(:foo, :bar, :BAZ) # => nil
968
+ #
969
+ # Nested Hashes and Arrays:
970
+ # h = {foo: {bar: [:a, :b, :c]}}
971
+ # h.dig(:foo, :bar, 2) # => :c
972
+ #
973
+ # This method will use the [default values](#class-Hash-label-Default+Values)
974
+ # for keys that are not present:
975
+ # h = {foo: {bar: [:a, :b, :c]}}
976
+ # h.dig(:hello) # => nil
977
+ # h.default_proc = -> (hash, _key) { hash }
978
+ # h.dig(:hello, :world) # => h
979
+ # h.dig(:hello, :world, :foo, :bar, 2) # => :c
378
980
  #
379
981
  def dig: (*untyped) -> untyped
380
982
 
381
- # Calls *block* once for each key in *hsh*, passing the key-value pair as
382
- # parameters.
983
+ # <!-- rdoc-file=hash.c -->
984
+ # Hash#each is an alias for Hash#each_pair.
383
985
  #
384
- # If no block is given, an enumerator is returned instead.
986
+ # Calls the given block with each key-value pair; returns `self`:
987
+ # h = {foo: 0, bar: 1, baz: 2}
988
+ # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
385
989
  #
386
- # h = { "a" => 100, "b" => 200 }
387
- # h.each {|key, value| puts "#{key} is #{value}" }
990
+ # Output:
991
+ # foo: 0
992
+ # bar: 1
993
+ # baz: 2
388
994
  #
389
- # *produces:*
995
+ # Returns a new Enumerator if no block given:
996
+ # h = {foo: 0, bar: 1, baz: 2}
997
+ # e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
998
+ # h1 = e.each {|key, value| puts "#{key}: #{value}"}
999
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
390
1000
  #
391
- # a is 100
392
- # b is 200
1001
+ # Output:
1002
+ # foo: 0
1003
+ # bar: 1
1004
+ # baz: 2
393
1005
  #
394
1006
  def each: () { ([ K, V ] arg0) -> untyped } -> self
395
1007
  | () -> ::Enumerator[[ K, V ], self]
396
1008
 
397
- # Calls *block* once for each key in *hsh*, passing the key as a parameter.
398
- #
399
- # If no block is given, an enumerator is returned instead.
400
- #
401
- # h = { "a" => 100, "b" => 200 }
402
- # h.each_key {|key| puts key }
403
- #
404
- # *produces:*
405
- #
406
- # a
407
- # b
1009
+ # <!--
1010
+ # rdoc-file=hash.c
1011
+ # - hash.each_key {|key| ... } -> self
1012
+ # - hash.each_key -> new_enumerator
1013
+ # -->
1014
+ # Calls the given block with each key; returns `self`:
1015
+ # h = {foo: 0, bar: 1, baz: 2}
1016
+ # h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
1017
+ #
1018
+ # Output:
1019
+ # foo
1020
+ # bar
1021
+ # baz
1022
+ #
1023
+ # Returns a new Enumerator if no block given:
1024
+ # h = {foo: 0, bar: 1, baz: 2}
1025
+ # e = h.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>
1026
+ # h1 = e.each {|key| puts key }
1027
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1028
+ #
1029
+ # Output:
1030
+ # foo
1031
+ # bar
1032
+ # baz
408
1033
  #
409
1034
  def each_key: () { (K arg0) -> untyped } -> ::Hash[K, V]
410
1035
  | () -> ::Enumerator[K, self]
411
1036
 
412
- # Calls *block* once for each key in *hsh*, passing the key-value pair as
413
- # parameters.
414
- #
415
- # If no block is given, an enumerator is returned instead.
416
- #
417
- # h = { "a" => 100, "b" => 200 }
418
- # h.each {|key, value| puts "#{key} is #{value}" }
419
- #
420
- # *produces:*
421
- #
422
- # a is 100
423
- # b is 200
1037
+ # <!--
1038
+ # rdoc-file=hash.c
1039
+ # - hash.each {|key, value| ... } -> self
1040
+ # - hash.each_pair {|key, value| ... } -> self
1041
+ # - hash.each -> new_enumerator
1042
+ # - hash.each_pair -> new_enumerator
1043
+ # -->
1044
+ # Hash#each is an alias for Hash#each_pair.
1045
+ #
1046
+ # Calls the given block with each key-value pair; returns `self`:
1047
+ # h = {foo: 0, bar: 1, baz: 2}
1048
+ # h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
1049
+ #
1050
+ # Output:
1051
+ # foo: 0
1052
+ # bar: 1
1053
+ # baz: 2
1054
+ #
1055
+ # Returns a new Enumerator if no block given:
1056
+ # h = {foo: 0, bar: 1, baz: 2}
1057
+ # e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
1058
+ # h1 = e.each {|key, value| puts "#{key}: #{value}"}
1059
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1060
+ #
1061
+ # Output:
1062
+ # foo: 0
1063
+ # bar: 1
1064
+ # baz: 2
424
1065
  #
425
1066
  alias each_pair each
426
1067
 
427
- # Calls *block* once for each key in *hsh*, passing the value as a parameter.
428
- #
429
- # If no block is given, an enumerator is returned instead.
430
- #
431
- # h = { "a" => 100, "b" => 200 }
432
- # h.each_value {|value| puts value }
433
- #
434
- # *produces:*
435
- #
436
- # 100
437
- # 200
1068
+ # <!--
1069
+ # rdoc-file=hash.c
1070
+ # - hash.each_value {|value| ... } -> self
1071
+ # - hash.each_value -> new_enumerator
1072
+ # -->
1073
+ # Calls the given block with each value; returns `self`:
1074
+ # h = {foo: 0, bar: 1, baz: 2}
1075
+ # h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
1076
+ #
1077
+ # Output:
1078
+ # 0
1079
+ # 1
1080
+ # 2
1081
+ #
1082
+ # Returns a new Enumerator if no block given:
1083
+ # h = {foo: 0, bar: 1, baz: 2}
1084
+ # e = h.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>
1085
+ # h1 = e.each {|value| puts value }
1086
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
1087
+ #
1088
+ # Output:
1089
+ # 0
1090
+ # 1
1091
+ # 2
438
1092
  #
439
1093
  def each_value: () { (V arg0) -> untyped } -> self
440
1094
  | () -> ::Enumerator[V, self]
441
1095
 
442
- # Returns `true` if *hsh* contains no key-value pairs.
443
- #
444
- # {}.empty? #=> true
1096
+ # <!--
1097
+ # rdoc-file=hash.c
1098
+ # - hash.empty? -> true or false
1099
+ # -->
1100
+ # Returns `true` if there are no hash entries, `false` otherwise:
1101
+ # {}.empty? # => true
1102
+ # {foo: 0, bar: 1, baz: 2}.empty? # => false
445
1103
  #
446
1104
  def empty?: () -> bool
447
1105
 
448
- # Returns `true` if *hash* and *other* are both hashes with the same content.
449
- # The orders of each hashes are not compared.
1106
+ # <!--
1107
+ # rdoc-file=hash.c
1108
+ # - hash.eql? object -> true or false
1109
+ # -->
1110
+ # Returns `true` if all of the following are true:
1111
+ # * `object` is a Hash object.
1112
+ # * `hash` and `object` have the same keys (regardless of order).
1113
+ # * For each key `key`, `h[key] eql? object[key]`.
1114
+ #
1115
+ #
1116
+ # Otherwise, returns `false`.
1117
+ #
1118
+ # Equal:
1119
+ # h1 = {foo: 0, bar: 1, baz: 2}
1120
+ # h2 = {foo: 0, bar: 1, baz: 2}
1121
+ # h1.eql? h2 # => true
1122
+ # h3 = {baz: 2, bar: 1, foo: 0}
1123
+ # h1.eql? h3 # => true
450
1124
  #
451
1125
  def eql?: (untyped) -> bool
452
1126
 
453
- # Returns a hash excluded given keys and the values.
454
- #
1127
+ # <!--
1128
+ # rdoc-file=hash.c
1129
+ # - hsh.except(*keys) -> a_hash
1130
+ # -->
1131
+ # Returns a new Hash excluding entries for the given `keys`:
455
1132
  # h = { a: 100, b: 200, c: 300 }
456
- # h.except(:a) #=> {:b=>200, :c=>300}
457
- # h.except(:b, :c, :d) #=> {:a=>100}
1133
+ # h.except(:a) #=> {:b=>200, :c=>300}
1134
+ #
1135
+ # Any given `keys` that are not found are ignored.
458
1136
  #
459
1137
  def except: (*K) -> ::Hash[K, V]
460
1138
 
461
- # Returns a value from the hash for the given key. If the key can't be found,
462
- # there are several options: With no other arguments, it will raise a KeyError
463
- # exception; if *default* is given, then that will be returned; if the optional
464
- # code block is specified, then that will be run and its result returned.
1139
+ # <!--
1140
+ # rdoc-file=hash.c
1141
+ # - hash.fetch(key) -> object
1142
+ # - hash.fetch(key, default_value) -> object
1143
+ # - hash.fetch(key) {|key| ... } -> object
1144
+ # -->
1145
+ # Returns the value for the given `key`, if found.
1146
+ # h = {foo: 0, bar: 1, baz: 2}
1147
+ # h.fetch(:bar) # => 1
465
1148
  #
466
- # h = { "a" => 100, "b" => 200 }
467
- # h.fetch("a") #=> 100
468
- # h.fetch("z", "go fish") #=> "go fish"
469
- # h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
1149
+ # If `key` is not found and no block was given, returns `default_value`:
1150
+ # {}.fetch(:nosuch, :default) # => :default
470
1151
  #
471
- # The following example shows that an exception is raised if the key is not
472
- # found and a default value is not supplied.
1152
+ # If `key` is not found and a block was given, yields `key` to the block and
1153
+ # returns the block's return value:
1154
+ # {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
473
1155
  #
474
- # h = { "a" => 100, "b" => 200 }
475
- # h.fetch("z")
1156
+ # Raises KeyError if neither `default_value` nor a block was given.
476
1157
  #
477
- # *produces:*
478
- #
479
- # prog.rb:2:in `fetch': key not found (KeyError)
480
- # from prog.rb:2
1158
+ # Note that this method does not use the values of either #default or
1159
+ # #default_proc.
481
1160
  #
482
1161
  def fetch: (K arg0) -> V
483
1162
  | [X] (K arg0, X arg1) -> (V | X)
484
1163
  | [X] (K arg0) { (K arg0) -> X } -> (V | X)
485
1164
 
486
- # Returns an array containing the values associated with the given keys but also
487
- # raises KeyError when one of keys can't be found. Also see Hash#values_at and
488
- # Hash#fetch.
1165
+ # <!--
1166
+ # rdoc-file=hash.c
1167
+ # - hash.fetch_values(*keys) -> new_array
1168
+ # - hash.fetch_values(*keys) {|key| ... } -> new_array
1169
+ # -->
1170
+ # Returns a new Array containing the values associated with the given keys
1171
+ # *keys:
1172
+ # h = {foo: 0, bar: 1, baz: 2}
1173
+ # h.fetch_values(:baz, :foo) # => [2, 0]
1174
+ #
1175
+ # Returns a new empty Array if no arguments given.
489
1176
  #
490
- # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
1177
+ # When a block is given, calls the block with each missing key, treating the
1178
+ # block's return value as the value for that key:
1179
+ # h = {foo: 0, bar: 1, baz: 2}
1180
+ # values = h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
1181
+ # values # => [1, 0, "bad", "bam"]
491
1182
  #
492
- # h.fetch_values("cow", "cat") #=> ["bovine", "feline"]
493
- # h.fetch_values("cow", "bird") # raises KeyError
494
- # h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
1183
+ # When no block is given, raises an exception if any given key is not found.
495
1184
  #
496
1185
  def fetch_values: (*K) -> ::Array[V]
497
1186
  | [X] (*K) { (K) -> X } -> ::Array[V | X]
498
1187
 
499
- # Returns a new hash consisting of entries for which the block returns true.
500
- #
501
- # If no block is given, an enumerator is returned instead.
1188
+ # <!-- rdoc-file=hash.c -->
1189
+ # Hash#filter is an alias for Hash#select.
502
1190
  #
503
- # h = { "a" => 100, "b" => 200, "c" => 300 }
504
- # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
505
- # h.select {|k,v| v < 200} #=> {"a" => 100}
1191
+ # Returns a new Hash object whose entries are those for which the block returns
1192
+ # a truthy value:
1193
+ # h = {foo: 0, bar: 1, baz: 2}
1194
+ # h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
506
1195
  #
507
- # Hash#filter is an alias for Hash#select.
1196
+ # Returns a new Enumerator if no block given:
1197
+ # h = {foo: 0, bar: 1, baz: 2}
1198
+ # e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
1199
+ # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
508
1200
  #
509
1201
  def filter: () { (K, V) -> boolish } -> ::Hash[K, V]
510
1202
  | () -> ::Enumerator[[ K, V ], ::Hash[K, V]]
511
1203
 
512
- # Equivalent to Hash#keep_if, but returns `nil` if no changes were made.
513
- #
1204
+ # <!-- rdoc-file=hash.c -->
514
1205
  # Hash#filter! is an alias for Hash#select!.
515
1206
  #
1207
+ # Returns `self`, whose entries are those for which the block returns a truthy
1208
+ # value:
1209
+ # h = {foo: 0, bar: 1, baz: 2}
1210
+ # h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
1211
+ #
1212
+ # Returns `nil` if no entries were removed.
1213
+ #
1214
+ # Returns a new Enumerator if no block given:
1215
+ # h = {foo: 0, bar: 1, baz: 2}
1216
+ # e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
1217
+ # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
1218
+ #
516
1219
  def filter!: () { (K, V) -> boolish } -> self?
517
1220
  | () -> ::Enumerator[[ K, V ], self?]
518
1221
 
519
- # Returns a new array that is a one-dimensional flattening of this hash. That
520
- # is, for every key or value that is an array, extract its elements into the new
521
- # array. Unlike Array#flatten, this method does not flatten recursively by
522
- # default. The optional *level* argument determines the level of recursion to
523
- # flatten.
1222
+ # <!--
1223
+ # rdoc-file=hash.c
1224
+ # - hash.flatten -> new_array
1225
+ # - hash.flatten(level) -> new_array
1226
+ # -->
1227
+ # Returns a new Array object that is a 1-dimensional flattening of `self`.
1228
+ #
1229
+ # ---
1230
+ #
1231
+ # By default, nested Arrays are not flattened:
1232
+ # h = {foo: 0, bar: [:bat, 3], baz: 2}
1233
+ # h.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]
1234
+ #
1235
+ # Takes the depth of recursive flattening from Integer argument `level`:
1236
+ # h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
1237
+ # h.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
1238
+ # h.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
1239
+ # h.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
1240
+ # h.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]
524
1241
  #
525
- # a = {1=> "one", 2 => [2,"two"], 3 => "three"}
526
- # a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]
527
- # a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]
1242
+ # When `level` is negative, flattens all nested Arrays:
1243
+ # h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
1244
+ # h.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
1245
+ # h.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]
1246
+ #
1247
+ # When `level` is zero, returns the equivalent of #to_a :
1248
+ # h = {foo: 0, bar: [:bat, 3], baz: 2}
1249
+ # h.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
1250
+ # h.flatten(0) == h.to_a # => true
528
1251
  #
529
1252
  def flatten: () -> ::Array[K | V]
530
1253
  | (1 level) -> ::Array[K | V]
531
1254
  | (Integer level) -> Array[untyped]
532
1255
 
533
- # Returns `true` if the given key is present in *hsh*.
534
- #
535
- # h = { "a" => 100, "b" => 200 }
536
- # h.has_key?("a") #=> true
537
- # h.has_key?("z") #=> false
1256
+ # <!-- rdoc-file=hash.c -->
1257
+ # Methods #has_key?, #key?, and #member? are aliases for #include?.
538
1258
  #
539
- # Note that #include? and #member? do not test member equality using `==` as do
540
- # other Enumerables.
541
- #
542
- # See also Enumerable#include?
1259
+ # Returns `true` if `key` is a key in `self`, otherwise `false`.
543
1260
  #
544
1261
  def has_key?: (K arg0) -> bool
545
1262
 
546
- # Returns `true` if the given value is present for some key in *hsh*.
1263
+ # <!--
1264
+ # rdoc-file=hash.c
1265
+ # - hash.has_value?(value) -> true or false
1266
+ # - hash.value?(value) -> true or false
1267
+ # -->
1268
+ # Method #value? is an alias for #has_value?.
547
1269
  #
548
- # h = { "a" => 100, "b" => 200 }
549
- # h.value?(100) #=> true
550
- # h.value?(999) #=> false
1270
+ # Returns `true` if `value` is a value in `self`, otherwise `false`.
551
1271
  #
552
1272
  def has_value?: (V arg0) -> bool
553
1273
 
554
- # Compute a hash-code for this hash. Two hashes with the same content will have
555
- # the same hash code (and will compare using `eql?`).
1274
+ # <!--
1275
+ # rdoc-file=hash.c
1276
+ # - hash.hash -> an_integer
1277
+ # -->
1278
+ # Returns the Integer hash-code for the hash.
556
1279
  #
557
- # See also Object#hash.
1280
+ # Two Hash objects have the same hash-code if their content is the same
1281
+ # (regardless or order):
1282
+ # h1 = {foo: 0, bar: 1, baz: 2}
1283
+ # h2 = {baz: 2, bar: 1, foo: 0}
1284
+ # h2.hash == h1.hash # => true
1285
+ # h2.eql? h1 # => true
558
1286
  #
559
1287
  def hash: () -> Integer
560
1288
 
561
- # Returns `true` if the given key is present in *hsh*.
562
- #
563
- # h = { "a" => 100, "b" => 200 }
564
- # h.has_key?("a") #=> true
565
- # h.has_key?("z") #=> false
1289
+ # <!--
1290
+ # rdoc-file=hash.c
1291
+ # - hash.include?(key) -> true or false
1292
+ # - hash.has_key?(key) -> true or false
1293
+ # - hash.key?(key) -> true or false
1294
+ # - hash.member?(key) -> true or false
1295
+ # -->
1296
+ # Methods #has_key?, #key?, and #member? are aliases for #include?.
566
1297
  #
567
- # Note that #include? and #member? do not test member equality using `==` as do
568
- # other Enumerables.
569
- #
570
- # See also Enumerable#include?
1298
+ # Returns `true` if `key` is a key in `self`, otherwise `false`.
571
1299
  #
572
1300
  alias include? has_key?
573
1301
 
574
- # Return the contents of this hash as a string.
1302
+ # <!--
1303
+ # rdoc-file=hash.c
1304
+ # - hash.inspect -> new_string
1305
+ # -->
1306
+ # Returns a new String containing the hash entries:
1307
+ # h = {foo: 0, bar: 1, baz: 2}
1308
+ # h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
575
1309
  #
576
- # h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
577
- # h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"
1310
+ # Hash#to_s is an alias for Hash#inspect.
578
1311
  #
579
1312
  def inspect: () -> String
580
1313
 
581
- # Returns a new hash created by using *hsh*'s values as keys, and the keys as
582
- # values. If a key with the same value already exists in the *hsh*, then the
583
- # last one defined will be used, the earlier value(s) will be discarded.
584
- #
585
- # h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
586
- # h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
587
- #
588
- # If there is no key with the same value, Hash#invert is involutive.
589
- #
590
- # h = { a: 1, b: 3, c: 4 }
591
- # h.invert.invert == h #=> true
1314
+ # <!--
1315
+ # rdoc-file=hash.c
1316
+ # - hash.invert -> new_hash
1317
+ # -->
1318
+ # Returns a new Hash object with the each key-value pair inverted:
1319
+ # h = {foo: 0, bar: 1, baz: 2}
1320
+ # h1 = h.invert
1321
+ # h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
592
1322
  #
593
- # The condition, no key with the same value, can be tested by comparing the size
594
- # of inverted hash.
595
- #
596
- # # no key with the same value
597
- # h = { a: 1, b: 3, c: 4 }
598
- # h.size == h.invert.size #=> true
599
- #
600
- # # two (or more) keys has the same value
601
- # h = { a: 1, b: 3, c: 1 }
602
- # h.size == h.invert.size #=> false
1323
+ # Overwrites any repeated new keys: (see [Entry
1324
+ # Order](#class-Hash-label-Entry+Order)):
1325
+ # h = {foo: 0, bar: 0, baz: 0}
1326
+ # h.invert # => {0=>:baz}
603
1327
  #
604
1328
  def invert: () -> ::Hash[V, K]
605
1329
 
606
- # Deletes every key-value pair from *hsh* for which *block* evaluates to
607
- # `false`.
608
- #
609
- # If no block is given, an enumerator is returned instead.
610
- #
611
- # See also Hash#select!.
1330
+ # <!--
1331
+ # rdoc-file=hash.c
1332
+ # - hash.keep_if {|key, value| ... } -> self
1333
+ # - hash.keep_if -> new_enumerator
1334
+ # -->
1335
+ # Calls the block for each key-value pair; retains the entry if the block
1336
+ # returns a truthy value; otherwise deletes the entry; returns `self`.
1337
+ # h = {foo: 0, bar: 1, baz: 2}
1338
+ # h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
1339
+ #
1340
+ # Returns a new Enumerator if no block given:
1341
+ # h = {foo: 0, bar: 1, baz: 2}
1342
+ # e = h.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
1343
+ # e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
612
1344
  #
613
1345
  def keep_if: () { (K, V) -> boolish } -> self
614
1346
  | () -> ::Enumerator[[ K, V ], self]
615
1347
 
616
- # Returns the key of an occurrence of a given value. If the value is not found,
617
- # returns `nil`.
1348
+ # <!--
1349
+ # rdoc-file=hash.c
1350
+ # - hash.key(value) -> key or nil
1351
+ # -->
1352
+ # Returns the key for the first-found entry with the given `value` (see [Entry
1353
+ # Order](#class-Hash-label-Entry+Order)):
1354
+ # h = {foo: 0, bar: 2, baz: 2}
1355
+ # h.key(0) # => :foo
1356
+ # h.key(2) # => :bar
618
1357
  #
619
- # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }
620
- # h.key(200) #=> "b"
621
- # h.key(300) #=> "c"
622
- # h.key(999) #=> nil
1358
+ # Returns `nil` if so such value is found.
623
1359
  #
624
1360
  def key: (V) -> K?
625
1361
 
626
- # Returns `true` if the given key is present in *hsh*.
1362
+ # <!-- rdoc-file=hash.c -->
1363
+ # Methods #has_key?, #key?, and #member? are aliases for #include?.
627
1364
  #
628
- # h = { "a" => 100, "b" => 200 }
629
- # h.has_key?("a") #=> true
630
- # h.has_key?("z") #=> false
631
- #
632
- # Note that #include? and #member? do not test member equality using `==` as do
633
- # other Enumerables.
634
- #
635
- # See also Enumerable#include?
1365
+ # Returns `true` if `key` is a key in `self`, otherwise `false`.
636
1366
  #
637
1367
  alias key? has_key?
638
1368
 
639
- # Returns a new array populated with the keys from this hash. See also
640
- # Hash#values.
641
- #
642
- # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
643
- # h.keys #=> ["a", "b", "c", "d"]
1369
+ # <!--
1370
+ # rdoc-file=hash.c
1371
+ # - hash.keys -> new_array
1372
+ # -->
1373
+ # Returns a new Array containing all keys in `self`:
1374
+ # h = {foo: 0, bar: 1, baz: 2}
1375
+ # h.keys # => [:foo, :bar, :baz]
644
1376
  #
645
1377
  def keys: () -> ::Array[K]
646
1378
 
647
- # Returns the number of key-value pairs in the hash.
648
- #
649
- # h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
650
- # h.size #=> 4
651
- # h.delete("a") #=> 200
652
- # h.size #=> 3
653
- # h.length #=> 3
1379
+ # <!-- rdoc-file=hash.c -->
1380
+ # Returns the count of entries in `self`:
1381
+ # {foo: 0, bar: 1, baz: 2}.length # => 3
654
1382
  #
655
1383
  # Hash#length is an alias for Hash#size.
656
1384
  #
657
1385
  def length: () -> Integer
658
1386
 
659
- # Returns `true` if the given key is present in *hsh*.
1387
+ # <!-- rdoc-file=hash.c -->
1388
+ # Methods #has_key?, #key?, and #member? are aliases for #include?.
660
1389
  #
661
- # h = { "a" => 100, "b" => 200 }
662
- # h.has_key?("a") #=> true
663
- # h.has_key?("z") #=> false
664
- #
665
- # Note that #include? and #member? do not test member equality using `==` as do
666
- # other Enumerables.
667
- #
668
- # See also Enumerable#include?
1390
+ # Returns `true` if `key` is a key in `self`, otherwise `false`.
669
1391
  #
670
1392
  alias member? has_key?
671
1393
 
672
- # Returns a new hash that combines the contents of the receiver and the contents
673
- # of the given hashes.
1394
+ # <!--
1395
+ # rdoc-file=hash.c
1396
+ # - hash.merge -> copy_of_self
1397
+ # - hash.merge(*other_hashes) -> new_hash
1398
+ # - hash.merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
1399
+ # -->
1400
+ # Returns the new Hash formed by merging each of `other_hashes` into a copy of
1401
+ # `self`.
1402
+ #
1403
+ # Each argument in `other_hashes` must be a Hash.
1404
+ #
1405
+ # ---
1406
+ #
1407
+ # With arguments and no block:
1408
+ # * Returns the new Hash object formed by merging each successive Hash in
1409
+ # `other_hashes` into `self`.
1410
+ # * Each new-key entry is added at the end.
1411
+ # * Each duplicate-key entry's value overwrites the previous value.
1412
+ #
1413
+ #
1414
+ # Example:
1415
+ # h = {foo: 0, bar: 1, baz: 2}
1416
+ # h1 = {bat: 3, bar: 4}
1417
+ # h2 = {bam: 5, bat:6}
1418
+ # h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
1419
+ #
1420
+ # With arguments and a block:
1421
+ # * Returns a new Hash object that is the merge of `self` and each given hash.
1422
+ # * The given hashes are merged left to right.
1423
+ # * Each new-key entry is added at the end.
1424
+ # * For each duplicate key:
1425
+ # * Calls the block with the key and the old and new values.
1426
+ # * The block's return value becomes the new value for the entry.
674
1427
  #
675
- # If no block is given, entries with duplicate keys are overwritten with the
676
- # values from each `other_hash` successively, otherwise the value for each
677
- # duplicate key is determined by calling the block with the key, its value in
678
- # the receiver and its value in each `other_hash`.
679
1428
  #
680
- # When called without any argument, returns a copy of the receiver.
681
1429
  #
682
- # h1 = { "a" => 100, "b" => 200 }
683
- # h2 = { "b" => 246, "c" => 300 }
684
- # h3 = { "b" => 357, "d" => 400 }
685
- # h1.merge #=> {"a"=>100, "b"=>200}
686
- # h1.merge(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
687
- # h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
688
- # h1.merge(h2) {|key, oldval, newval| newval - oldval}
689
- # #=> {"a"=>100, "b"=>46, "c"=>300}
690
- # h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
691
- # #=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
692
- # h1 #=> {"a"=>100, "b"=>200}
1430
+ # Example:
1431
+ # h = {foo: 0, bar: 1, baz: 2}
1432
+ # h1 = {bat: 3, bar: 4}
1433
+ # h2 = {bam: 5, bat:6}
1434
+ # h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
1435
+ # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
1436
+ #
1437
+ # With no arguments:
1438
+ # * Returns a copy of `self`.
1439
+ # * The block, if given, is ignored.
1440
+ #
1441
+ #
1442
+ # Example:
1443
+ # h = {foo: 0, bar: 1, baz: 2}
1444
+ # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1445
+ # h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
1446
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
693
1447
  #
694
1448
  def merge: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V]
695
- | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> (C) } -> ::Hash[A | K, B | V | C]
1449
+ | [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> C } -> ::Hash[A | K, B | V | C]
696
1450
 
697
- # Adds the contents of the given hashes to the receiver.
1451
+ # <!-- rdoc-file=hash.c -->
1452
+ # Merges each of `other_hashes` into `self`; returns `self`.
1453
+ #
1454
+ # Each argument in `other_hashes` must be a Hash.
1455
+ #
1456
+ # Method #update is an alias for #merge!.
1457
+ #
1458
+ # With arguments and no block:
1459
+ # * Returns `self`, after the given hashes are merged into it.
1460
+ # * The given hashes are merged left to right.
1461
+ # * Each new entry is added at the end.
1462
+ # * Each duplicate-key entry's value overwrites the previous value.
1463
+ #
1464
+ #
1465
+ # Example:
1466
+ # h = {foo: 0, bar: 1, baz: 2}
1467
+ # h1 = {bat: 3, bar: 4}
1468
+ # h2 = {bam: 5, bat:6}
1469
+ # h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
698
1470
  #
699
- # If no block is given, entries with duplicate keys are overwritten with the
700
- # values from each `other_hash` successively, otherwise the value for each
701
- # duplicate key is determined by calling the block with the key, its value in
702
- # the receiver and its value in each `other_hash`.
1471
+ # With arguments and a block:
1472
+ # * Returns `self`, after the given hashes are merged.
1473
+ # * The given hashes are merged left to right.
1474
+ # * Each new-key entry is added at the end.
1475
+ # * For each duplicate key:
1476
+ # * Calls the block with the key and the old and new values.
1477
+ # * The block's return value becomes the new value for the entry.
703
1478
  #
704
- # h1 = { "a" => 100, "b" => 200 }
705
- # h1.merge! #=> {"a"=>100, "b"=>200}
706
- # h1 #=> {"a"=>100, "b"=>200}
707
1479
  #
708
- # h1 = { "a" => 100, "b" => 200 }
709
- # h2 = { "b" => 246, "c" => 300 }
710
- # h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
711
- # h1 #=> {"a"=>100, "b"=>246, "c"=>300}
712
1480
  #
713
- # h1 = { "a" => 100, "b" => 200 }
714
- # h2 = { "b" => 246, "c" => 300 }
715
- # h3 = { "b" => 357, "d" => 400 }
716
- # h1.merge!(h2, h3)
717
- # #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
718
- # h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
1481
+ # Example:
1482
+ # h = {foo: 0, bar: 1, baz: 2}
1483
+ # h1 = {bat: 3, bar: 4}
1484
+ # h2 = {bam: 5, bat:6}
1485
+ # h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
1486
+ # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
719
1487
  #
720
- # h1 = { "a" => 100, "b" => 200 }
721
- # h2 = { "b" => 246, "c" => 300 }
722
- # h3 = { "b" => 357, "d" => 400 }
723
- # h1.merge!(h2, h3) {|key, v1, v2| v1 }
724
- # #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
725
- # h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
1488
+ # With no arguments:
1489
+ # * Returns `self`, unmodified.
1490
+ # * The block, if given, is ignored.
726
1491
  #
727
- # Hash#update is an alias for Hash#merge!.
1492
+ #
1493
+ # Example:
1494
+ # h = {foo: 0, bar: 1, baz: 2}
1495
+ # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1496
+ # h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
1497
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
728
1498
  #
729
1499
  def merge!: (*::Hash[K, V] other_hashes) -> self
730
- | (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> (V) } -> self
1500
+ | (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self
731
1501
 
732
- # Searches through the hash comparing *obj* with the value using `==`. Returns
733
- # the first key-value pair (two-element array) that matches. See also
734
- # Array#rassoc.
1502
+ # <!--
1503
+ # rdoc-file=hash.c
1504
+ # - hash.rassoc(value) -> new_array or nil
1505
+ # -->
1506
+ # Returns a new 2-element Array consisting of the key and value of the
1507
+ # first-found entry whose value is `==` to value (see [Entry
1508
+ # Order](#class-Hash-label-Entry+Order)):
1509
+ # h = {foo: 0, bar: 1, baz: 1}
1510
+ # h.rassoc(1) # => [:bar, 1]
735
1511
  #
736
- # a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}
737
- # a.rassoc("two") #=> [2, "two"]
738
- # a.rassoc("four") #=> nil
1512
+ # Returns `nil` if no such value found.
739
1513
  #
740
- def rassoc: (V) -> [K, V]?
1514
+ def rassoc: (V) -> [ K, V ]?
741
1515
 
742
- # Rebuilds the hash based on the current hash values for each key. If values of
743
- # key objects have changed since they were inserted, this method will reindex
744
- # *hsh*. If Hash#rehash is called while an iterator is traversing the hash, a
745
- # RuntimeError will be raised in the iterator.
1516
+ # <!--
1517
+ # rdoc-file=hash.c
1518
+ # - hash.rehash -> self
1519
+ # -->
1520
+ # Rebuilds the hash table by recomputing the hash index for each key; returns
1521
+ # `self`.
746
1522
  #
747
- # a = [ "a", "b" ]
748
- # c = [ "c", "d" ]
749
- # h = { a => 100, c => 300 }
750
- # h[a] #=> 100
751
- # a[0] = "z"
752
- # h[a] #=> nil
753
- # h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}
754
- # h[a] #=> 100
1523
+ # The hash table becomes invalid if the hash value of a key has changed after
1524
+ # the entry was created. See [Modifying an Active Hash
1525
+ # Key](#class-Hash-label-Modifying+an+Active+Hash+Key).
755
1526
  #
756
1527
  def rehash: () -> self
757
1528
 
758
- # Returns a new hash consisting of entries for which the block returns false.
759
- #
760
- # If no block is given, an enumerator is returned instead.
761
- #
762
- # h = { "a" => 100, "b" => 200, "c" => 300 }
763
- # h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}
764
- # h.reject {|k,v| v > 100} #=> {"a" => 100}
1529
+ # <!--
1530
+ # rdoc-file=hash.c
1531
+ # - hash.reject {|key, value| ... } -> new_hash
1532
+ # - hash.reject -> new_enumerator
1533
+ # -->
1534
+ # Returns a new Hash object whose entries are all those from `self` for which
1535
+ # the block returns `false` or `nil`:
1536
+ # h = {foo: 0, bar: 1, baz: 2}
1537
+ # h1 = h.reject {|key, value| key.start_with?('b') }
1538
+ # h1 # => {:foo=>0}
1539
+ #
1540
+ # Returns a new Enumerator if no block given:
1541
+ # h = {foo: 0, bar: 1, baz: 2}
1542
+ # e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
1543
+ # h1 = e.each {|key, value| key.start_with?('b') }
1544
+ # h1 # => {:foo=>0}
765
1545
  #
766
1546
  def reject: () -> ::Enumerator[[ K, V ], self]
767
1547
  | () { (K, V) -> boolish } -> self
768
1548
 
769
- # Equivalent to Hash#delete_if, but returns `nil` if no changes were made.
1549
+ # <!--
1550
+ # rdoc-file=hash.c
1551
+ # - hash.reject! {|key, value| ... } -> self or nil
1552
+ # - hash.reject! -> new_enumerator
1553
+ # -->
1554
+ # Returns `self`, whose remaining entries are those for which the block returns
1555
+ # `false` or `nil`:
1556
+ # h = {foo: 0, bar: 1, baz: 2}
1557
+ # h.reject! {|key, value| value < 2 } # => {:baz=>2}
1558
+ #
1559
+ # Returns `nil` if no entries are removed.
1560
+ #
1561
+ # Returns a new Enumerator if no block given:
1562
+ # h = {foo: 0, bar: 1, baz: 2}
1563
+ # e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
1564
+ # e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
770
1565
  #
771
1566
  def reject!: () -> ::Enumerator[[ K, V ], self?]
772
- | () { (K, V) -> boolish } -> self?
1567
+ | () { (K, V) -> boolish } -> self?
773
1568
 
774
- # Replaces the contents of *hsh* with the contents of *other_hash*.
775
- #
776
- # h = { "a" => 100, "b" => 200 }
777
- # h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1569
+ # <!-- rdoc-file=hash.c -->
1570
+ # Replaces the entire contents of `self` with the contents of `other_hash`;
1571
+ # returns `self`:
1572
+ # h = {foo: 0, bar: 1, baz: 2}
1573
+ # h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
778
1574
  #
779
1575
  def replace: (Hash[K, V]) -> self
780
1576
 
781
- # Returns a new hash consisting of entries for which the block returns true.
782
- #
783
- # If no block is given, an enumerator is returned instead.
1577
+ # <!--
1578
+ # rdoc-file=hash.c
1579
+ # - hash.select {|key, value| ... } -> new_hash
1580
+ # - hash.select -> new_enumerator
1581
+ # -->
1582
+ # Hash#filter is an alias for Hash#select.
784
1583
  #
785
- # h = { "a" => 100, "b" => 200, "c" => 300 }
786
- # h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}
787
- # h.select {|k,v| v < 200} #=> {"a" => 100}
1584
+ # Returns a new Hash object whose entries are those for which the block returns
1585
+ # a truthy value:
1586
+ # h = {foo: 0, bar: 1, baz: 2}
1587
+ # h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
788
1588
  #
789
- # Hash#filter is an alias for Hash#select.
1589
+ # Returns a new Enumerator if no block given:
1590
+ # h = {foo: 0, bar: 1, baz: 2}
1591
+ # e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
1592
+ # e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
790
1593
  #
791
1594
  alias select filter
792
1595
 
793
- # Equivalent to Hash#keep_if, but returns `nil` if no changes were made.
794
- #
1596
+ # <!--
1597
+ # rdoc-file=hash.c
1598
+ # - hash.select! {|key, value| ... } -> self or nil
1599
+ # - hash.select! -> new_enumerator
1600
+ # -->
795
1601
  # Hash#filter! is an alias for Hash#select!.
796
1602
  #
1603
+ # Returns `self`, whose entries are those for which the block returns a truthy
1604
+ # value:
1605
+ # h = {foo: 0, bar: 1, baz: 2}
1606
+ # h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
1607
+ #
1608
+ # Returns `nil` if no entries were removed.
1609
+ #
1610
+ # Returns a new Enumerator if no block given:
1611
+ # h = {foo: 0, bar: 1, baz: 2}
1612
+ # e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
1613
+ # e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
1614
+ #
797
1615
  alias select! filter!
798
1616
 
799
- # Removes a key-value pair from *hsh* and returns it as the two-item array `[`
800
- # *key, value* `]`, or the hash's default value if the hash is empty.
1617
+ # <!--
1618
+ # rdoc-file=hash.c
1619
+ # - hash.shift -> [key, value] or default_value
1620
+ # -->
1621
+ # Removes the first hash entry (see [Entry
1622
+ # Order](#class-Hash-label-Entry+Order)); returns a 2-element Array containing
1623
+ # the removed key and value:
1624
+ # h = {foo: 0, bar: 1, baz: 2}
1625
+ # h.shift # => [:foo, 0]
1626
+ # h # => {:bar=>1, :baz=>2}
801
1627
  #
802
- # h = { 1 => "a", 2 => "b", 3 => "c" }
803
- # h.shift #=> [1, "a"]
804
- # h #=> {2=>"b", 3=>"c"}
1628
+ # Returns the default value if the hash is empty (see [Default
1629
+ # Values](#class-Hash-label-Default+Values)).
805
1630
  #
806
1631
  def shift: () -> [ K, V ]?
807
1632
 
808
- # Returns the number of key-value pairs in the hash.
809
- #
810
- # h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
811
- # h.size #=> 4
812
- # h.delete("a") #=> 200
813
- # h.size #=> 3
814
- # h.length #=> 3
1633
+ # <!--
1634
+ # rdoc-file=hash.c
1635
+ # - hash.length -> integer
1636
+ # - hash.size -> integer
1637
+ # -->
1638
+ # Returns the count of entries in `self`:
1639
+ # {foo: 0, bar: 1, baz: 2}.length # => 3
815
1640
  #
816
1641
  # Hash#length is an alias for Hash#size.
817
1642
  #
818
1643
  alias size length
819
1644
 
820
- # Returns a hash containing only the given keys and their values.
1645
+ # <!--
1646
+ # rdoc-file=hash.c
1647
+ # - hash.slice(*keys) -> new_hash
1648
+ # -->
1649
+ # Returns a new Hash object containing the entries for the given `keys`:
1650
+ # h = {foo: 0, bar: 1, baz: 2}
1651
+ # h.slice(:baz, :foo) # => {:baz=>2, :foo=>0}
821
1652
  #
822
- # h = { a: 100, b: 200, c: 300 }
823
- # h.slice(:a) #=> {:a=>100}
824
- # h.slice(:b, :c, :d) #=> {:b=>200, :c=>300}
1653
+ # Any given `keys` that are not found are ignored.
825
1654
  #
826
1655
  def slice: (*K) -> ::Hash[K, V]
827
1656
 
828
- # ## Element Assignment
829
- #
830
- # Associates the value given by `value` with the key given by `key`.
1657
+ # <!-- rdoc-file=hash.c -->
1658
+ # Hash#store is an alias for Hash#[]=.
831
1659
  #
832
- # h = { "a" => 100, "b" => 200 }
833
- # h["a"] = 9
834
- # h["c"] = 4
835
- # h #=> {"a"=>9, "b"=>200, "c"=>4}
836
- # h.store("d", 42) #=> 42
837
- # h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
1660
+ # Associates the given `value` with the given `key`; returns `value`.
838
1661
  #
839
- # `key` should not have its value changed while it is in use as a key (an
840
- # `unfrozen String` passed as a key will be duplicated and frozen).
1662
+ # If the given `key` exists, replaces its value with the given `value`; the
1663
+ # ordering is not affected (see [Entry Order](#class-Hash-label-Entry+Order)):
1664
+ # h = {foo: 0, bar: 1}
1665
+ # h[:foo] = 2 # => 2
1666
+ # h.store(:bar, 3) # => 3
1667
+ # h # => {:foo=>2, :bar=>3}
841
1668
  #
842
- # a = "a"
843
- # b = "b".freeze
844
- # h = { a => 100, b => 200 }
845
- # h.key(100).equal? a #=> false
846
- # h.key(200).equal? b #=> true
1669
+ # If `key` does not exist, adds the `key` and `value`; the new entry is last in
1670
+ # the order (see [Entry Order](#class-Hash-label-Entry+Order)):
1671
+ # h = {foo: 0, bar: 1}
1672
+ # h[:baz] = 2 # => 2
1673
+ # h.store(:bat, 3) # => 3
1674
+ # h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
847
1675
  #
848
1676
  alias store []=
849
1677
 
850
- # Converts *hsh* to a nested array of `[` *key, value* `]` arrays.
851
- #
852
- # h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
853
- # h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]
1678
+ # <!--
1679
+ # rdoc-file=hash.c
1680
+ # - hash.to_a -> new_array
1681
+ # -->
1682
+ # Returns a new Array of 2-element Array objects; each nested Array contains a
1683
+ # key-value pair from `self`:
1684
+ # h = {foo: 0, bar: 1, baz: 2}
1685
+ # h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
854
1686
  #
855
1687
  def to_a: () -> ::Array[[ K, V ]]
856
1688
 
857
- # Returns `self`. If called on a subclass of Hash, converts the receiver to a
858
- # Hash object.
1689
+ # <!--
1690
+ # rdoc-file=hash.c
1691
+ # - hash.to_h -> self or new_hash
1692
+ # - hash.to_h {|key, value| ... } -> new_hash
1693
+ # -->
1694
+ # For an instance of Hash, returns `self`.
1695
+ #
1696
+ # For a subclass of Hash, returns a new Hash containing the content of `self`.
859
1697
  #
860
- # If a block is given, the results of the block on each pair of the receiver
861
- # will be used as pairs.
1698
+ # When a block is given, returns a new Hash object whose content is based on the
1699
+ # block; the block should return a 2-element Array object specifying the
1700
+ # key-value pair to be included in the returned Array:
1701
+ # h = {foo: 0, bar: 1, baz: 2}
1702
+ # h1 = h.to_h {|key, value| [value, key] }
1703
+ # h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
862
1704
  #
863
1705
  def to_h: () -> Hash[K, V]
864
1706
  | [A, B] () { (K, V) -> [ A, B ] } -> Hash[A, B]
865
1707
 
1708
+ # <!--
1709
+ # rdoc-file=hash.c
1710
+ # - hash.to_hash -> self
1711
+ # -->
866
1712
  # Returns `self`.
867
1713
  #
868
1714
  def to_hash: () -> self
869
1715
 
870
- # Returns a Proc which maps keys to values.
871
- #
872
- # h = {a:1, b:2}
873
- # hp = h.to_proc
874
- # hp.call(:a) #=> 1
875
- # hp.call(:b) #=> 2
876
- # hp.call(:c) #=> nil
877
- # [:a, :b, :c].map(&h) #=> [1, 2, nil]
1716
+ # <!--
1717
+ # rdoc-file=hash.c
1718
+ # - hash.to_proc -> proc
1719
+ # -->
1720
+ # Returns a Proc object that maps a key to its value:
1721
+ # h = {foo: 0, bar: 1, baz: 2}
1722
+ # proc = h.to_proc
1723
+ # proc.class # => Proc
1724
+ # proc.call(:foo) # => 0
1725
+ # proc.call(:bar) # => 1
1726
+ # proc.call(:nosuch) # => nil
878
1727
  #
879
1728
  def to_proc: () -> ^(K) -> V?
880
1729
 
1730
+ # <!-- rdoc-file=hash.c -->
1731
+ # Returns a new String containing the hash entries:
1732
+ # h = {foo: 0, bar: 1, baz: 2}
1733
+ # h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
1734
+ #
1735
+ # Hash#to_s is an alias for Hash#inspect.
1736
+ #
881
1737
  alias to_s inspect
882
1738
 
883
- # Returns a new hash with the results of running the block once for every key.
884
- # This method does not change the values.
1739
+ # <!--
1740
+ # rdoc-file=hash.c
1741
+ # - hash.transform_keys {|key| ... } -> new_hash
1742
+ # - hash.transform_keys(hash2) -> new_hash
1743
+ # - hash.transform_keys(hash2) {|other_key| ...} -> new_hash
1744
+ # - hash.transform_keys -> new_enumerator
1745
+ # -->
1746
+ # Returns a new Hash object; each entry has:
1747
+ # * A key provided by the block.
1748
+ # * The value from `self`.
1749
+ #
1750
+ #
1751
+ # An optional hash argument can be provided to map keys to new keys. Any key not
1752
+ # given will be mapped using the provided block, or remain the same if no block
1753
+ # is given.
1754
+ #
1755
+ # Transform keys:
1756
+ # h = {foo: 0, bar: 1, baz: 2}
1757
+ # h1 = h.transform_keys {|key| key.to_s }
1758
+ # h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
885
1759
  #
886
- # h = { a: 1, b: 2, c: 3 }
887
- # h.transform_keys {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
888
- # h.transform_keys(&:to_s) #=> { "a" => 1, "b" => 2, "c" => 3 }
889
- # h.transform_keys.with_index {|k, i| "#{k}.#{i}" }
890
- # #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
1760
+ # h.transform_keys(foo: :bar, bar: :foo)
1761
+ # #=> {bar: 0, foo: 1, baz: 2}
891
1762
  #
892
- # If no block is given, an enumerator is returned instead.
1763
+ # h.transform_keys(foo: :hello, &:to_s)
1764
+ # #=> {:hello=>0, "bar"=>1, "baz"=>2}
1765
+ #
1766
+ # Overwrites values for duplicate keys:
1767
+ # h = {foo: 0, bar: 1, baz: 2}
1768
+ # h1 = h.transform_keys {|key| :bat }
1769
+ # h1 # => {:bat=>2}
1770
+ #
1771
+ # Returns a new Enumerator if no block given:
1772
+ # h = {foo: 0, bar: 1, baz: 2}
1773
+ # e = h.transform_keys # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_keys>
1774
+ # h1 = e.each { |key| key.to_s }
1775
+ # h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
893
1776
  #
894
1777
  def transform_keys: () -> Enumerator[K, Hash[untyped, V]]
895
1778
  | [A] () { (K) -> A } -> Hash[A, V]
896
1779
 
897
- # Invokes the given block once for each key in *hsh*, replacing it with the new
898
- # key returned by the block, and then returns *hsh*. This method does not change
899
- # the values.
900
- #
901
- # h = { a: 1, b: 2, c: 3 }
902
- # h.transform_keys! {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
903
- # h.transform_keys!(&:to_sym) #=> { a: 1, b: 2, c: 3 }
904
- # h.transform_keys!.with_index {|k, i| "#{k}.#{i}" }
905
- # #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
906
- #
907
- # If no block is given, an enumerator is returned instead.
1780
+ # <!--
1781
+ # rdoc-file=hash.c
1782
+ # - hash.transform_keys! {|key| ... } -> self
1783
+ # - hash.transform_keys!(hash2) -> self
1784
+ # - hash.transform_keys!(hash2) {|other_key| ...} -> self
1785
+ # - hash.transform_keys! -> new_enumerator
1786
+ # -->
1787
+ # Same as Hash#transform_keys but modifies the receiver in place instead of
1788
+ # returning a new hash.
908
1789
  #
909
1790
  def transform_keys!: () -> Enumerator[K, self]
910
1791
  | () { (K) -> K } -> self
911
1792
 
912
- # Returns a new hash with the results of running the block once for every value.
913
- # This method does not change the keys.
1793
+ # <!--
1794
+ # rdoc-file=hash.c
1795
+ # - hash.transform_values {|value| ... } -> new_hash
1796
+ # - hash.transform_values -> new_enumerator
1797
+ # -->
1798
+ # Returns a new Hash object; each entry has:
1799
+ # * A key from `self`.
1800
+ # * A value provided by the block.
914
1801
  #
915
- # h = { a: 1, b: 2, c: 3 }
916
- # h.transform_values {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
917
- # h.transform_values(&:to_s) #=> { a: "1", b: "2", c: "3" }
918
- # h.transform_values.with_index {|v, i| "#{v}.#{i}" }
919
- # #=> { a: "1.0", b: "2.1", c: "3.2" }
920
1802
  #
921
- # If no block is given, an enumerator is returned instead.
1803
+ # Transform values:
1804
+ # h = {foo: 0, bar: 1, baz: 2}
1805
+ # h1 = h.transform_values {|value| value * 100}
1806
+ # h1 # => {:foo=>0, :bar=>100, :baz=>200}
1807
+ #
1808
+ # Returns a new Enumerator if no block given:
1809
+ # h = {foo: 0, bar: 1, baz: 2}
1810
+ # e = h.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
1811
+ # h1 = e.each { |value| value * 100}
1812
+ # h1 # => {:foo=>0, :bar=>100, :baz=>200}
922
1813
  #
923
1814
  def transform_values: () -> Enumerator[V, Hash[K, untyped]]
924
1815
  | [A] () { (V) -> A } -> Hash[K, A]
925
1816
 
926
- # Invokes the given block once for each value in *hsh*, replacing it with the
927
- # new value returned by the block, and then returns *hsh*. This method does not
928
- # change the keys.
929
- #
930
- # h = { a: 1, b: 2, c: 3 }
931
- # h.transform_values! {|v| v * v + 1 } #=> { a: 2, b: 5, c: 10 }
932
- # h.transform_values!(&:to_s) #=> { a: "2", b: "5", c: "10" }
933
- # h.transform_values!.with_index {|v, i| "#{v}.#{i}" }
934
- # #=> { a: "2.0", b: "5.1", c: "10.2" }
935
- #
936
- # If no block is given, an enumerator is returned instead.
1817
+ # <!--
1818
+ # rdoc-file=hash.c
1819
+ # - hash.transform_values! {|value| ... } -> self
1820
+ # - hash.transform_values! -> new_enumerator
1821
+ # -->
1822
+ # Returns `self`, whose keys are unchanged, and whose values are determined by
1823
+ # the given block.
1824
+ # h = {foo: 0, bar: 1, baz: 2}
1825
+ # h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
1826
+ #
1827
+ # Returns a new Enumerator if no block given:
1828
+ # h = {foo: 0, bar: 1, baz: 2}
1829
+ # e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!>
1830
+ # h1 = e.each {|value| value * 100}
1831
+ # h1 # => {:foo=>0, :bar=>100, :baz=>200}
937
1832
  #
938
1833
  def transform_values!: () -> Enumerator[V, self]
939
1834
  | () { (V) -> V } -> self
940
1835
 
941
- # Adds the contents of the given hashes to the receiver.
1836
+ # <!--
1837
+ # rdoc-file=hash.c
1838
+ # - hash.merge! -> self
1839
+ # - hash.merge!(*other_hashes) -> self
1840
+ # - hash.merge!(*other_hashes) { |key, old_value, new_value| ... } -> self
1841
+ # -->
1842
+ # Merges each of `other_hashes` into `self`; returns `self`.
1843
+ #
1844
+ # Each argument in `other_hashes` must be a Hash.
942
1845
  #
943
- # If no block is given, entries with duplicate keys are overwritten with the
944
- # values from each `other_hash` successively, otherwise the value for each
945
- # duplicate key is determined by calling the block with the key, its value in
946
- # the receiver and its value in each `other_hash`.
1846
+ # Method #update is an alias for #merge!.
947
1847
  #
948
- # h1 = { "a" => 100, "b" => 200 }
949
- # h1.merge! #=> {"a"=>100, "b"=>200}
950
- # h1 #=> {"a"=>100, "b"=>200}
1848
+ # With arguments and no block:
1849
+ # * Returns `self`, after the given hashes are merged into it.
1850
+ # * The given hashes are merged left to right.
1851
+ # * Each new entry is added at the end.
1852
+ # * Each duplicate-key entry's value overwrites the previous value.
951
1853
  #
952
- # h1 = { "a" => 100, "b" => 200 }
953
- # h2 = { "b" => 246, "c" => 300 }
954
- # h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300}
955
- # h1 #=> {"a"=>100, "b"=>246, "c"=>300}
956
1854
  #
957
- # h1 = { "a" => 100, "b" => 200 }
958
- # h2 = { "b" => 246, "c" => 300 }
959
- # h3 = { "b" => 357, "d" => 400 }
960
- # h1.merge!(h2, h3)
961
- # #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
962
- # h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
1855
+ # Example:
1856
+ # h = {foo: 0, bar: 1, baz: 2}
1857
+ # h1 = {bat: 3, bar: 4}
1858
+ # h2 = {bam: 5, bat:6}
1859
+ # h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
963
1860
  #
964
- # h1 = { "a" => 100, "b" => 200 }
965
- # h2 = { "b" => 246, "c" => 300 }
966
- # h3 = { "b" => 357, "d" => 400 }
967
- # h1.merge!(h2, h3) {|key, v1, v2| v1 }
968
- # #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
969
- # h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
1861
+ # With arguments and a block:
1862
+ # * Returns `self`, after the given hashes are merged.
1863
+ # * The given hashes are merged left to right.
1864
+ # * Each new-key entry is added at the end.
1865
+ # * For each duplicate key:
1866
+ # * Calls the block with the key and the old and new values.
1867
+ # * The block's return value becomes the new value for the entry.
970
1868
  #
971
- # Hash#update is an alias for Hash#merge!.
1869
+ #
1870
+ #
1871
+ # Example:
1872
+ # h = {foo: 0, bar: 1, baz: 2}
1873
+ # h1 = {bat: 3, bar: 4}
1874
+ # h2 = {bam: 5, bat:6}
1875
+ # h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
1876
+ # h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
1877
+ #
1878
+ # With no arguments:
1879
+ # * Returns `self`, unmodified.
1880
+ # * The block, if given, is ignored.
1881
+ #
1882
+ #
1883
+ # Example:
1884
+ # h = {foo: 0, bar: 1, baz: 2}
1885
+ # h.merge # => {:foo=>0, :bar=>1, :baz=>2}
1886
+ # h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
1887
+ # h1 # => {:foo=>0, :bar=>1, :baz=>2}
972
1888
  #
973
1889
  alias update merge!
974
1890
 
975
- # Returns `true` if the given value is present for some key in *hsh*.
1891
+ # <!-- rdoc-file=hash.c -->
1892
+ # Method #value? is an alias for #has_value?.
976
1893
  #
977
- # h = { "a" => 100, "b" => 200 }
978
- # h.value?(100) #=> true
979
- # h.value?(999) #=> false
1894
+ # Returns `true` if `value` is a value in `self`, otherwise `false`.
980
1895
  #
981
1896
  alias value? has_value?
982
1897
 
983
- # Returns a new array populated with the values from *hsh*. See also Hash#keys.
984
- #
985
- # h = { "a" => 100, "b" => 200, "c" => 300 }
986
- # h.values #=> [100, 200, 300]
1898
+ # <!--
1899
+ # rdoc-file=hash.c
1900
+ # - hash.values -> new_array
1901
+ # -->
1902
+ # Returns a new Array containing all values in `self`:
1903
+ # h = {foo: 0, bar: 1, baz: 2}
1904
+ # h.values # => [0, 1, 2]
987
1905
  #
988
1906
  def values: () -> ::Array[V]
989
1907
 
990
- # Return an array containing the values associated with the given keys. Also see
991
- # Hash.select.
1908
+ # <!--
1909
+ # rdoc-file=hash.c
1910
+ # - hash.values_at(*keys) -> new_array
1911
+ # -->
1912
+ # Returns a new Array containing values for the given `keys`:
1913
+ # h = {foo: 0, bar: 1, baz: 2}
1914
+ # h.values_at(:baz, :foo) # => [2, 0]
992
1915
  #
993
- # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
994
- # h.values_at("cow", "cat") #=> ["bovine", "feline"]
1916
+ # The [default values](#class-Hash-label-Default+Values) are returned for any
1917
+ # keys that are not found:
1918
+ # h.values_at(:hello, :foo) # => [nil, 0]
995
1919
  #
996
1920
  def values_at: (*K arg0) -> ::Array[V?]
997
1921
 
998
1922
  private
999
1923
 
1000
- # Returns a new, empty hash. If this hash is subsequently accessed by a key that
1001
- # doesn't correspond to a hash entry, the value returned depends on the style of
1002
- # `new` used to create the hash. In the first form, the access returns `nil`. If
1003
- # *obj* is specified, this single object will be used for all *default values*.
1004
- # If a block is specified, it will be called with the hash object and the key,
1005
- # and should return the default value. It is the block's responsibility to store
1006
- # the value in the hash if required.
1007
- #
1008
- # h = Hash.new("Go Fish")
1009
- # h["a"] = 100
1010
- # h["b"] = 200
1011
- # h["a"] #=> 100
1012
- # h["c"] #=> "Go Fish"
1013
- # # The following alters the single default object
1014
- # h["c"].upcase! #=> "GO FISH"
1015
- # h["d"] #=> "GO FISH"
1016
- # h.keys #=> ["a", "b"]
1017
- #
1018
- # # While this creates a new default object each time
1019
- # h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
1020
- # h["c"] #=> "Go Fish: c"
1021
- # h["c"].upcase! #=> "GO FISH: C"
1022
- # h["d"] #=> "Go Fish: d"
1023
- # h.keys #=> ["c", "d"]
1924
+ # <!--
1925
+ # rdoc-file=hash.c
1926
+ # - Hash.new(default_value = nil) -> new_hash
1927
+ # - Hash.new {|hash, key| ... } -> new_hash
1928
+ # -->
1929
+ # Returns a new empty Hash object.
1930
+ #
1931
+ # The initial default value and initial default proc for the new hash depend on
1932
+ # which form above was used. See [Default
1933
+ # Values](#class-Hash-label-Default+Values).
1934
+ #
1935
+ # If neither an argument nor a block given, initializes both the default value
1936
+ # and the default proc to `nil`:
1937
+ # h = Hash.new
1938
+ # h.default # => nil
1939
+ # h.default_proc # => nil
1940
+ #
1941
+ # If argument `default_value` given but no block given, initializes the default
1942
+ # value to the given `default_value` and the default proc to `nil`:
1943
+ # h = Hash.new(false)
1944
+ # h.default # => false
1945
+ # h.default_proc # => nil
1946
+ #
1947
+ # If a block given but no argument, stores the block as the default proc and
1948
+ # sets the default value to `nil`:
1949
+ # h = Hash.new {|hash, key| "Default value for #{key}" }
1950
+ # h.default # => nil
1951
+ # h.default_proc.class # => Proc
1952
+ # h[:nosuch] # => "Default value for nosuch"
1024
1953
  #
1025
1954
  def initialize: () -> void
1026
- | (untyped default) -> void
1027
- | [A, B] () { (Hash[A, B] hash, A key) -> B } -> void
1028
-
1029
- # Replaces the contents of *hsh* with the contents of *other_hash*.
1030
- #
1031
- # h = { "a" => 100, "b" => 200 }
1032
- # h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1955
+ | (untyped default) -> void
1956
+ | [A, B] () { (Hash[A, B] hash, A key) -> B } -> void
1957
+
1958
+ # <!--
1959
+ # rdoc-file=hash.c
1960
+ # - hash.replace(other_hash) -> self
1961
+ # -->
1962
+ # Replaces the entire contents of `self` with the contents of `other_hash`;
1963
+ # returns `self`:
1964
+ # h = {foo: 0, bar: 1, baz: 2}
1965
+ # h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
1033
1966
  #
1034
1967
  def initialize_copy: (self object) -> self
1035
1968
  end