rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/core/hash.rbs
CHANGED
@@ -1,73 +1,231 @@
|
|
1
|
-
#
|
2
|
-
#
|
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
|
-
#
|
6
|
-
#
|
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
|
-
#
|
9
|
+
# ### Hash Data Syntax
|
11
10
|
#
|
12
|
-
#
|
11
|
+
# The older syntax for Hash data uses the "hash rocket," `=>`:
|
13
12
|
#
|
14
|
-
#
|
13
|
+
# h = {:foo => 0, :bar => 1, :baz => 2}
|
14
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2}
|
15
15
|
#
|
16
|
-
#
|
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
|
-
#
|
19
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
20
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2}
|
19
21
|
#
|
20
|
-
#
|
22
|
+
# You can also use a String in place of a bareword:
|
21
23
|
#
|
22
|
-
#
|
24
|
+
# h = {'foo': 0, 'bar': 1, 'baz': 2}
|
25
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2}
|
23
26
|
#
|
24
|
-
#
|
27
|
+
# And you can mix the styles:
|
25
28
|
#
|
26
|
-
#
|
27
|
-
#
|
29
|
+
# h = {foo: 0, :bar => 1, 'baz': 2}
|
30
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2}
|
28
31
|
#
|
29
|
-
#
|
30
|
-
#
|
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
|
-
#
|
35
|
+
# # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
|
36
|
+
# h = {0: 'zero'}
|
34
37
|
#
|
35
|
-
#
|
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
|
-
#
|
38
|
-
#
|
41
|
+
# x = 0
|
42
|
+
# y = 100
|
43
|
+
# h = {x:, y:}
|
44
|
+
# h # => {:x=>0, :y=>100}
|
39
45
|
#
|
40
|
-
#
|
46
|
+
# ### Common Uses
|
41
47
|
#
|
42
|
-
#
|
48
|
+
# You can use a Hash to give names to objects:
|
43
49
|
#
|
44
|
-
#
|
50
|
+
# person = {name: 'Matz', language: 'Ruby'}
|
51
|
+
# person # => {:name=>"Matz", :language=>"Ruby"}
|
45
52
|
#
|
46
|
-
#
|
53
|
+
# You can use a Hash to give names to method arguments:
|
47
54
|
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
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
|
-
#
|
53
|
-
#
|
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
|
-
#
|
63
|
+
# some_method(foo: 0, bar: 1, baz: 2) # => {:foo=>0, :bar=>1, :baz=>2}
|
57
64
|
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
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
|
-
#
|
66
|
-
#
|
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
|
-
#
|
69
|
-
#
|
70
|
-
#
|
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
|
85
|
-
# other.author == @author
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
114
|
-
#
|
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
|
-
#
|
117
|
-
# key-value pairs or an object convertible to a hash.
|
587
|
+
# Hash[:foo, 0, :bar, 1] # => {:foo=>0, :bar=>1}
|
118
588
|
#
|
119
|
-
#
|
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
|
-
#
|
128
|
-
#
|
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
|
-
#
|
131
|
-
#
|
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
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
# h1
|
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
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
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
|
-
#
|
159
|
-
#
|
160
|
-
#
|
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
|
-
#
|
650
|
+
# Otherwise, returns `false`.
|
171
651
|
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
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
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
# h1
|
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
|
-
#
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
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
|
-
#
|
199
|
-
#
|
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
|
-
#
|
202
|
-
#
|
203
|
-
# h
|
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
|
-
#
|
208
|
-
#
|
209
|
-
#
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
# h
|
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
|
-
#
|
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
|
-
#
|
236
|
-
#
|
237
|
-
#
|
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
|
-
#
|
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
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
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
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
#
|
257
|
-
#
|
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
|
-
#
|
262
|
-
#
|
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
|
-
#
|
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
|
-
#
|
270
|
-
#
|
271
|
-
#
|
272
|
-
#
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
#
|
277
|
-
#
|
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
|
-
#
|
282
|
-
#
|
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
|
-
#
|
289
|
-
#
|
290
|
-
#
|
291
|
-
#
|
292
|
-
#
|
293
|
-
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
#
|
297
|
-
#
|
298
|
-
#
|
299
|
-
# h
|
300
|
-
#
|
301
|
-
#
|
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
|
-
#
|
306
|
-
#
|
307
|
-
#
|
308
|
-
#
|
309
|
-
#
|
310
|
-
# h
|
311
|
-
# h
|
312
|
-
# h
|
313
|
-
# #
|
314
|
-
#
|
315
|
-
#
|
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
|
-
#
|
323
|
-
#
|
324
|
-
#
|
325
|
-
#
|
326
|
-
#
|
327
|
-
#
|
328
|
-
#
|
329
|
-
#
|
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
|
-
#
|
334
|
-
#
|
335
|
-
#
|
336
|
-
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
# h
|
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
|
-
#
|
344
|
-
#
|
345
|
-
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
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
|
-
#
|
357
|
-
#
|
358
|
-
#
|
359
|
-
#
|
360
|
-
#
|
361
|
-
#
|
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
|
-
#
|
367
|
-
#
|
368
|
-
#
|
369
|
-
#
|
370
|
-
#
|
371
|
-
#
|
372
|
-
#
|
373
|
-
#
|
374
|
-
#
|
375
|
-
#
|
376
|
-
#
|
377
|
-
#
|
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
|
-
#
|
382
|
-
#
|
983
|
+
# <!-- rdoc-file=hash.c -->
|
984
|
+
# Hash#each is an alias for Hash#each_pair.
|
383
985
|
#
|
384
|
-
#
|
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
|
-
#
|
387
|
-
#
|
990
|
+
# Output:
|
991
|
+
# foo: 0
|
992
|
+
# bar: 1
|
993
|
+
# baz: 2
|
388
994
|
#
|
389
|
-
#
|
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
|
-
#
|
392
|
-
#
|
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
|
-
#
|
398
|
-
#
|
399
|
-
#
|
400
|
-
#
|
401
|
-
#
|
402
|
-
#
|
403
|
-
#
|
404
|
-
#
|
405
|
-
#
|
406
|
-
#
|
407
|
-
#
|
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
|
-
#
|
413
|
-
#
|
414
|
-
#
|
415
|
-
#
|
416
|
-
#
|
417
|
-
#
|
418
|
-
#
|
419
|
-
#
|
420
|
-
#
|
421
|
-
#
|
422
|
-
#
|
423
|
-
#
|
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
|
-
#
|
428
|
-
#
|
429
|
-
#
|
430
|
-
#
|
431
|
-
#
|
432
|
-
#
|
433
|
-
#
|
434
|
-
#
|
435
|
-
#
|
436
|
-
#
|
437
|
-
#
|
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
|
-
#
|
443
|
-
#
|
444
|
-
#
|
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
|
-
#
|
449
|
-
#
|
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
|
-
#
|
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)
|
457
|
-
#
|
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
|
-
#
|
462
|
-
#
|
463
|
-
#
|
464
|
-
#
|
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
|
-
#
|
467
|
-
#
|
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
|
-
#
|
472
|
-
#
|
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
|
-
#
|
475
|
-
# h.fetch("z")
|
1156
|
+
# Raises KeyError if neither `default_value` nor a block was given.
|
476
1157
|
#
|
477
|
-
#
|
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
|
-
#
|
487
|
-
#
|
488
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
504
|
-
#
|
505
|
-
# h
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
520
|
-
#
|
521
|
-
#
|
522
|
-
#
|
523
|
-
#
|
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
|
-
#
|
526
|
-
#
|
527
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
555
|
-
#
|
1274
|
+
# <!--
|
1275
|
+
# rdoc-file=hash.c
|
1276
|
+
# - hash.hash -> an_integer
|
1277
|
+
# -->
|
1278
|
+
# Returns the Integer hash-code for the hash.
|
556
1279
|
#
|
557
|
-
#
|
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
|
-
#
|
562
|
-
#
|
563
|
-
#
|
564
|
-
#
|
565
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
582
|
-
#
|
583
|
-
#
|
584
|
-
#
|
585
|
-
#
|
586
|
-
# h
|
587
|
-
#
|
588
|
-
#
|
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
|
-
#
|
594
|
-
#
|
595
|
-
#
|
596
|
-
# #
|
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
|
-
#
|
607
|
-
#
|
608
|
-
#
|
609
|
-
#
|
610
|
-
#
|
611
|
-
#
|
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
|
-
#
|
617
|
-
#
|
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
|
-
#
|
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
|
-
#
|
1362
|
+
# <!-- rdoc-file=hash.c -->
|
1363
|
+
# Methods #has_key?, #key?, and #member? are aliases for #include?.
|
627
1364
|
#
|
628
|
-
#
|
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
|
-
#
|
640
|
-
#
|
641
|
-
#
|
642
|
-
#
|
643
|
-
#
|
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
|
-
#
|
648
|
-
#
|
649
|
-
#
|
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
|
-
#
|
1387
|
+
# <!-- rdoc-file=hash.c -->
|
1388
|
+
# Methods #has_key?, #key?, and #member? are aliases for #include?.
|
660
1389
|
#
|
661
|
-
#
|
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
|
-
#
|
673
|
-
#
|
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
|
-
#
|
683
|
-
#
|
684
|
-
#
|
685
|
-
#
|
686
|
-
#
|
687
|
-
#
|
688
|
-
#
|
689
|
-
#
|
690
|
-
#
|
691
|
-
#
|
692
|
-
#
|
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) ->
|
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
|
-
#
|
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
|
-
#
|
700
|
-
#
|
701
|
-
#
|
702
|
-
#
|
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
|
-
#
|
714
|
-
#
|
715
|
-
#
|
716
|
-
#
|
717
|
-
#
|
718
|
-
#
|
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
|
-
#
|
721
|
-
#
|
722
|
-
#
|
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
|
-
#
|
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) ->
|
1500
|
+
| (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self
|
731
1501
|
|
732
|
-
#
|
733
|
-
#
|
734
|
-
#
|
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
|
-
#
|
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
|
-
#
|
743
|
-
#
|
744
|
-
#
|
745
|
-
#
|
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
|
-
#
|
748
|
-
#
|
749
|
-
#
|
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
|
-
#
|
759
|
-
#
|
760
|
-
#
|
761
|
-
#
|
762
|
-
#
|
763
|
-
#
|
764
|
-
#
|
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
|
-
#
|
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
|
-
|
1567
|
+
| () { (K, V) -> boolish } -> self?
|
773
1568
|
|
774
|
-
#
|
775
|
-
#
|
776
|
-
#
|
777
|
-
# h
|
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
|
-
#
|
782
|
-
#
|
783
|
-
#
|
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
|
-
#
|
786
|
-
#
|
787
|
-
# h
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
800
|
-
#
|
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
|
-
#
|
803
|
-
#
|
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
|
-
#
|
809
|
-
#
|
810
|
-
#
|
811
|
-
#
|
812
|
-
#
|
813
|
-
#
|
814
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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`
|
840
|
-
#
|
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
|
-
#
|
843
|
-
#
|
844
|
-
# h = {
|
845
|
-
# h
|
846
|
-
# h.
|
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
|
-
#
|
851
|
-
#
|
852
|
-
#
|
853
|
-
#
|
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
|
-
#
|
858
|
-
#
|
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
|
-
#
|
861
|
-
#
|
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
|
-
#
|
871
|
-
#
|
872
|
-
#
|
873
|
-
#
|
874
|
-
#
|
875
|
-
#
|
876
|
-
#
|
877
|
-
#
|
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
|
-
#
|
884
|
-
#
|
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
|
887
|
-
#
|
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
|
-
#
|
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
|
-
#
|
898
|
-
#
|
899
|
-
#
|
900
|
-
#
|
901
|
-
#
|
902
|
-
#
|
903
|
-
#
|
904
|
-
#
|
905
|
-
#
|
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
|
-
#
|
913
|
-
#
|
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
|
-
#
|
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
|
-
#
|
927
|
-
#
|
928
|
-
#
|
929
|
-
#
|
930
|
-
#
|
931
|
-
#
|
932
|
-
#
|
933
|
-
# h
|
934
|
-
#
|
935
|
-
#
|
936
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
949
|
-
#
|
950
|
-
#
|
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
|
-
#
|
958
|
-
#
|
959
|
-
#
|
960
|
-
#
|
961
|
-
#
|
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
|
-
#
|
965
|
-
#
|
966
|
-
#
|
967
|
-
#
|
968
|
-
#
|
969
|
-
#
|
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
|
-
#
|
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
|
-
#
|
1891
|
+
# <!-- rdoc-file=hash.c -->
|
1892
|
+
# Method #value? is an alias for #has_value?.
|
976
1893
|
#
|
977
|
-
#
|
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
|
-
#
|
984
|
-
#
|
985
|
-
#
|
986
|
-
#
|
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
|
-
#
|
991
|
-
#
|
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
|
-
#
|
994
|
-
#
|
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
|
-
#
|
1001
|
-
#
|
1002
|
-
#
|
1003
|
-
#
|
1004
|
-
#
|
1005
|
-
#
|
1006
|
-
#
|
1007
|
-
#
|
1008
|
-
#
|
1009
|
-
#
|
1010
|
-
#
|
1011
|
-
#
|
1012
|
-
#
|
1013
|
-
#
|
1014
|
-
# h
|
1015
|
-
# h
|
1016
|
-
#
|
1017
|
-
#
|
1018
|
-
#
|
1019
|
-
# h = Hash.new
|
1020
|
-
# h
|
1021
|
-
# h
|
1022
|
-
#
|
1023
|
-
#
|
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
|
-
|
1027
|
-
|
1028
|
-
|
1029
|
-
#
|
1030
|
-
#
|
1031
|
-
#
|
1032
|
-
#
|
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
|