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