rbs 3.10.0 → 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 +3 -3
- data/.github/workflows/ruby.yml +24 -35
- data/.github/workflows/typecheck.yml +3 -1
- data/.github/workflows/windows.yml +2 -2
- data/.gitignore +0 -4
- data/CHANGELOG.md +0 -88
- data/README.md +1 -38
- data/Rakefile +20 -142
- data/Steepfile +1 -0
- data/config.yml +43 -1
- data/core/array.rbs +46 -100
- data/core/complex.rbs +21 -32
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +9 -6
- data/core/enumerable.rbs +3 -90
- data/core/enumerator.rbs +1 -18
- data/core/errno.rbs +0 -8
- data/core/errors.rbs +1 -28
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +4 -5
- data/core/file.rbs +12 -27
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +22 -209
- data/core/gc.rbs +281 -417
- data/core/hash.rbs +727 -1024
- data/core/integer.rbs +38 -78
- data/core/io/buffer.rbs +7 -18
- data/core/io/wait.rbs +33 -11
- data/core/io.rbs +12 -14
- data/core/kernel.rbs +51 -57
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +3 -42
- data/core/method.rbs +6 -14
- data/core/module.rbs +17 -88
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +16 -16
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +15 -21
- data/core/proc.rbs +8 -15
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +437 -278
- data/core/range.rbs +8 -7
- data/core/rational.rbs +24 -37
- 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/string.rbs +1228 -3153
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +29 -92
- 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 -1
- 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 +3 -3
- 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 +4 -3
- 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 +77 -80
- 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 +396 -2
- 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/{core → stdlib/pathname/0}/pathname.rbs +355 -255
- 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 +40 -12
- data/.clang-format +0 -74
- data/.clangd +0 -2
- data/.github/workflows/c-check.yml +0 -54
- 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 -153
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,222 +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 `true` if
|
|
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 `true` if
|
|
564
|
-
#
|
|
565
|
-
#
|
|
566
|
-
#
|
|
567
|
-
#
|
|
568
|
-
#
|
|
569
|
-
# h0 <= h1 # => true
|
|
570
|
-
# h1 <= h0 # => false
|
|
571
|
-
#
|
|
572
|
-
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
|
|
573
|
-
#
|
|
574
|
-
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
|
|
575
|
-
# hash.
|
|
576
|
-
#
|
|
577
|
-
# 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
|
|
578
572
|
#
|
|
579
573
|
def <=: [A, B] (::Hash[A, B]) -> bool
|
|
580
574
|
|
|
581
575
|
# <!--
|
|
582
576
|
# rdoc-file=hash.c
|
|
583
|
-
# -
|
|
577
|
+
# - hash == object -> true or false
|
|
584
578
|
# -->
|
|
585
|
-
# Returns whether `self` and `object` are equal.
|
|
586
|
-
#
|
|
587
579
|
# Returns `true` if all of the following are true:
|
|
588
|
-
#
|
|
589
|
-
# * `
|
|
590
|
-
# *
|
|
591
|
-
# * 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]`.
|
|
592
583
|
#
|
|
593
584
|
# Otherwise, returns `false`.
|
|
594
585
|
#
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
#
|
|
598
|
-
#
|
|
599
|
-
#
|
|
600
|
-
#
|
|
601
|
-
# h == {} # => false # Different number of entries.
|
|
602
|
-
# h == {foo: 0, bar: 1} # => false # Different key.
|
|
603
|
-
# h == {foo: 0, bar: 1} # => false # Different value.
|
|
604
|
-
#
|
|
605
|
-
# 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
|
|
606
592
|
#
|
|
607
593
|
def ==: (untyped other) -> bool
|
|
608
594
|
|
|
609
595
|
# <!--
|
|
610
596
|
# rdoc-file=hash.c
|
|
611
|
-
# -
|
|
597
|
+
# - hash > other_hash -> true or false
|
|
612
598
|
# -->
|
|
613
|
-
# Returns `true` if
|
|
614
|
-
#
|
|
615
|
-
#
|
|
616
|
-
#
|
|
617
|
-
#
|
|
618
|
-
#
|
|
619
|
-
#
|
|
620
|
-
# h > {baz: 2, bar: 1, foo: 0} # => false # Not a proper superset.
|
|
621
|
-
# h > {foo: 0, bar: 1} # => false # Different key.
|
|
622
|
-
# h > {foo: 0, bar: 1} # => false # Different value.
|
|
623
|
-
#
|
|
624
|
-
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
|
|
625
|
-
#
|
|
626
|
-
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
|
|
627
|
-
# hash.
|
|
628
|
-
#
|
|
629
|
-
# 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
|
|
630
606
|
#
|
|
631
607
|
def >: [A, B] (::Hash[A, B]) -> bool
|
|
632
608
|
|
|
633
609
|
# <!--
|
|
634
610
|
# rdoc-file=hash.c
|
|
635
|
-
# -
|
|
611
|
+
# - hash >= other_hash -> true or false
|
|
636
612
|
# -->
|
|
637
|
-
# Returns `true` if
|
|
638
|
-
#
|
|
639
|
-
#
|
|
640
|
-
#
|
|
641
|
-
#
|
|
642
|
-
#
|
|
643
|
-
# h0 >= h0 # => true
|
|
644
|
-
# h1 >= h0 # => false
|
|
645
|
-
#
|
|
646
|
-
# See [Hash Inclusion](rdoc-ref:language/hash_inclusion.rdoc).
|
|
647
|
-
#
|
|
648
|
-
# Raises TypeError if `other_hash` is not a hash and cannot be converted to a
|
|
649
|
-
# hash.
|
|
650
|
-
#
|
|
651
|
-
# 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
|
|
652
619
|
#
|
|
653
620
|
def >=: [A, B] (::Hash[A, B]) -> bool
|
|
654
621
|
|
|
655
622
|
# <!--
|
|
656
623
|
# rdoc-file=hash.c
|
|
657
|
-
# -
|
|
624
|
+
# - hash[key] -> value
|
|
658
625
|
# -->
|
|
659
|
-
#
|
|
660
|
-
#
|
|
661
|
-
#
|
|
662
|
-
# If the key is found, returns its value:
|
|
663
|
-
#
|
|
664
|
-
# {foo: 0, bar: 1, baz: 2}
|
|
665
|
-
# h[:bar] # => 1
|
|
666
|
-
#
|
|
667
|
-
# Otherwise, returns a default value (see [Hash
|
|
668
|
-
# 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
|
|
669
629
|
#
|
|
670
|
-
#
|
|
671
|
-
#
|
|
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
|
|
672
634
|
#
|
|
673
635
|
def []: %a{implicitly-returns-nil} (K arg0) -> V
|
|
674
636
|
|
|
675
637
|
# <!--
|
|
676
638
|
# rdoc-file=hash.c
|
|
677
|
-
# -
|
|
639
|
+
# - hash[key] = value -> value
|
|
640
|
+
# - hash.store(key, value)
|
|
678
641
|
# -->
|
|
679
|
-
# Associates the given `
|
|
680
|
-
#
|
|
681
|
-
# Searches for a hash key equivalent to the given `key`; see [Hash Key
|
|
682
|
-
# Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
|
|
683
|
-
#
|
|
684
|
-
# If the key is found, replaces its value with the given `object`; the ordering
|
|
685
|
-
# is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
|
|
642
|
+
# Associates the given `value` with the given `key`; returns `value`.
|
|
686
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)):
|
|
687
646
|
# h = {foo: 0, bar: 1}
|
|
688
647
|
# h[:foo] = 2 # => 2
|
|
689
|
-
# h
|
|
690
|
-
#
|
|
691
|
-
# If `key` is not found, creates a new entry for the given `key` and `object`;
|
|
692
|
-
# the new entry is last in the order (see [Entry
|
|
693
|
-
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
648
|
+
# h.store(:bar, 3) # => 3
|
|
649
|
+
# h # => {:foo=>2, :bar=>3}
|
|
694
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)):
|
|
695
653
|
# h = {foo: 0, bar: 1}
|
|
696
654
|
# h[:baz] = 2 # => 2
|
|
697
|
-
# h
|
|
698
|
-
# h
|
|
699
|
-
#
|
|
700
|
-
# Related: #[]; see also [Methods for
|
|
701
|
-
# Assigning](rdoc-ref:Hash@Methods+for+Assigning).
|
|
655
|
+
# h.store(:bat, 3) # => 3
|
|
656
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
|
|
702
657
|
#
|
|
703
658
|
def []=: (K arg0, V arg1) -> V
|
|
704
659
|
|
|
705
660
|
# <!--
|
|
706
661
|
# rdoc-file=hash.c
|
|
707
|
-
# - any? -> true or false
|
|
708
|
-
# - any?(
|
|
709
|
-
# - 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
|
|
710
665
|
# -->
|
|
711
666
|
# Returns `true` if any element satisfies a given criterion; `false` otherwise.
|
|
712
667
|
#
|
|
713
|
-
# If `self` has no element, returns `false` and argument or block are not used
|
|
714
|
-
# otherwise behaves as below.
|
|
668
|
+
# If `self` has no element, returns `false` and argument or block are not used.
|
|
715
669
|
#
|
|
716
|
-
# With no argument and no block, returns `true` if `self` is non-empty
|
|
717
|
-
#
|
|
718
|
-
#
|
|
719
|
-
# With argument `entry` and no block, returns `true` if for any key `key`
|
|
720
|
-
# `self.assoc(key) == entry`, `false` otherwise:
|
|
670
|
+
# With no argument and no block, returns `true` if `self` is non-empty; `false`
|
|
671
|
+
# if empty.
|
|
721
672
|
#
|
|
673
|
+
# With argument `object` and no block, returns `true` if for any key `key`
|
|
674
|
+
# `h.assoc(key) == object`:
|
|
722
675
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
723
|
-
# h.assoc(:bar) # => [:bar, 1]
|
|
724
676
|
# h.any?([:bar, 1]) # => true
|
|
725
677
|
# h.any?([:bar, 0]) # => false
|
|
678
|
+
# h.any?([:baz, 1]) # => false
|
|
726
679
|
#
|
|
727
|
-
# With no argument and a block
|
|
728
|
-
# returns `true` if the block returns
|
|
729
|
-
#
|
|
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:
|
|
730
682
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
731
683
|
# h.any? {|key, value| value < 3 } # => true
|
|
732
684
|
# h.any? {|key, value| value > 3 } # => false
|
|
733
685
|
#
|
|
734
|
-
#
|
|
735
|
-
# block.
|
|
736
|
-
#
|
|
737
|
-
# Related: Enumerable#any? (which this method overrides); see also [Methods for
|
|
738
|
-
# Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
686
|
+
# Related: Enumerable#any?
|
|
739
687
|
#
|
|
740
688
|
def any?: () -> bool
|
|
741
689
|
| (untyped pattern) -> bool
|
|
@@ -743,99 +691,81 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
743
691
|
|
|
744
692
|
# <!--
|
|
745
693
|
# rdoc-file=hash.c
|
|
746
|
-
# - assoc(key) ->
|
|
694
|
+
# - hash.assoc(key) -> new_array or nil
|
|
747
695
|
# -->
|
|
748
|
-
# If the given `key` is found, returns
|
|
749
|
-
#
|
|
750
|
-
#
|
|
696
|
+
# If the given `key` is found, returns a 2-element Array containing that key and
|
|
697
|
+
# its value:
|
|
751
698
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
752
699
|
# h.assoc(:bar) # => [:bar, 1]
|
|
753
700
|
#
|
|
754
|
-
# Returns `nil` if
|
|
755
|
-
#
|
|
756
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
701
|
+
# Returns `nil` if key `key` is not found.
|
|
757
702
|
#
|
|
758
703
|
def assoc: (K arg0) -> [ K, V ]?
|
|
759
704
|
|
|
760
705
|
# <!--
|
|
761
706
|
# rdoc-file=hash.c
|
|
762
|
-
# - clear -> self
|
|
707
|
+
# - hash.clear -> self
|
|
763
708
|
# -->
|
|
764
|
-
# Removes all entries
|
|
765
|
-
#
|
|
766
|
-
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
709
|
+
# Removes all hash entries; returns `self`.
|
|
767
710
|
#
|
|
768
711
|
def clear: () -> self
|
|
769
712
|
|
|
770
713
|
# <!--
|
|
771
714
|
# rdoc-file=hash.c
|
|
772
|
-
# - compact -> new_hash
|
|
715
|
+
# - hash.compact -> new_hash
|
|
773
716
|
# -->
|
|
774
717
|
# Returns a copy of `self` with all `nil`-valued entries removed:
|
|
775
|
-
#
|
|
776
718
|
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
|
777
|
-
# h.compact
|
|
778
|
-
#
|
|
779
|
-
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
719
|
+
# h1 = h.compact
|
|
720
|
+
# h1 # => {:foo=>0, :baz=>2}
|
|
780
721
|
#
|
|
781
722
|
def compact: () -> ::Hash[K, V]
|
|
782
723
|
|
|
783
724
|
# <!--
|
|
784
725
|
# rdoc-file=hash.c
|
|
785
|
-
# - compact! -> self or nil
|
|
726
|
+
# - hash.compact! -> self or nil
|
|
786
727
|
# -->
|
|
787
|
-
#
|
|
788
|
-
# `nil`-valued entries removed; returns `nil` otherwise:
|
|
789
|
-
#
|
|
728
|
+
# Returns `self` with all its `nil`-valued entries removed (in place):
|
|
790
729
|
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
|
791
|
-
# h.compact!
|
|
792
|
-
# h # => {foo: 0, baz: 2}
|
|
793
|
-
# h.compact! # => nil
|
|
730
|
+
# h.compact! # => {:foo=>0, :baz=>2}
|
|
794
731
|
#
|
|
795
|
-
#
|
|
732
|
+
# Returns `nil` if no entries were removed.
|
|
796
733
|
#
|
|
797
734
|
def compact!: () -> self?
|
|
798
735
|
|
|
799
736
|
# <!--
|
|
800
737
|
# rdoc-file=hash.c
|
|
801
|
-
# - compare_by_identity -> self
|
|
738
|
+
# - hash.compare_by_identity -> self
|
|
802
739
|
# -->
|
|
803
|
-
# Sets `self` to
|
|
804
|
-
# returns `self
|
|
805
|
-
#
|
|
806
|
-
# By default, two keys are considered to be the same key if and only if they are
|
|
807
|
-
# *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`.
|
|
808
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'
|
|
809
747
|
# h = {}
|
|
810
|
-
# h
|
|
811
|
-
# h[
|
|
748
|
+
# h.compare_by_identity? # => false
|
|
749
|
+
# h[s0] = 0
|
|
750
|
+
# h[s1] = 1
|
|
812
751
|
# h # => {"x"=>1}
|
|
813
752
|
#
|
|
814
|
-
#
|
|
815
|
-
#
|
|
816
|
-
#
|
|
817
|
-
# h.compare_by_identity
|
|
818
|
-
# h
|
|
819
|
-
# h
|
|
820
|
-
#
|
|
821
|
-
#
|
|
822
|
-
# 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}
|
|
823
761
|
#
|
|
824
762
|
def compare_by_identity: () -> self
|
|
825
763
|
|
|
826
764
|
# <!--
|
|
827
765
|
# rdoc-file=hash.c
|
|
828
|
-
# - compare_by_identity? -> true or false
|
|
766
|
+
# - hash.compare_by_identity? -> true or false
|
|
829
767
|
# -->
|
|
830
|
-
# Returns
|
|
831
|
-
#
|
|
832
|
-
# h = {}
|
|
833
|
-
# h.compare_by_identity? # => false
|
|
834
|
-
# h.compare_by_identity
|
|
835
|
-
# h.compare_by_identity? # => true
|
|
836
|
-
#
|
|
837
|
-
# Related: #compare_by_identity; see also [Methods for
|
|
838
|
-
# Comparing](rdoc-ref:Hash@Methods+for+Comparing).
|
|
768
|
+
# Returns `true` if #compare_by_identity has been called, `false` otherwise.
|
|
839
769
|
#
|
|
840
770
|
def compare_by_identity?: () -> bool
|
|
841
771
|
|
|
@@ -848,12 +778,12 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
848
778
|
|
|
849
779
|
# <!--
|
|
850
780
|
# rdoc-file=hash.c
|
|
851
|
-
# - default -> object
|
|
852
|
-
# - default(key) -> object
|
|
781
|
+
# - hash.default -> object
|
|
782
|
+
# - hash.default(key) -> object
|
|
853
783
|
# -->
|
|
854
784
|
# Returns the default value for the given `key`. The returned value will be
|
|
855
|
-
# determined either by the default proc or by the default value. See [
|
|
856
|
-
#
|
|
785
|
+
# determined either by the default proc or by the default value. See [Default
|
|
786
|
+
# Values](rdoc-ref:Hash@Default+Values).
|
|
857
787
|
#
|
|
858
788
|
# With no argument, returns the current default value:
|
|
859
789
|
# h = {}
|
|
@@ -869,7 +799,7 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
869
799
|
|
|
870
800
|
# <!--
|
|
871
801
|
# rdoc-file=hash.c
|
|
872
|
-
# - default = value -> object
|
|
802
|
+
# - hash.default = value -> object
|
|
873
803
|
# -->
|
|
874
804
|
# Sets the default value to `value`; returns `value`:
|
|
875
805
|
# h = {}
|
|
@@ -877,16 +807,16 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
877
807
|
# h.default = false # => false
|
|
878
808
|
# h.default # => false
|
|
879
809
|
#
|
|
880
|
-
# See [
|
|
810
|
+
# See [Default Values](rdoc-ref:Hash@Default+Values).
|
|
881
811
|
#
|
|
882
812
|
def default=: (V arg0) -> V
|
|
883
813
|
|
|
884
814
|
# <!--
|
|
885
815
|
# rdoc-file=hash.c
|
|
886
|
-
# - default_proc -> proc or nil
|
|
816
|
+
# - hash.default_proc -> proc or nil
|
|
887
817
|
# -->
|
|
888
|
-
# Returns the default proc for `self` (see [
|
|
889
|
-
#
|
|
818
|
+
# Returns the default proc for `self` (see [Default
|
|
819
|
+
# Values](rdoc-ref:Hash@Default+Values)):
|
|
890
820
|
# h = {}
|
|
891
821
|
# h.default_proc # => nil
|
|
892
822
|
# h.default_proc = proc {|hash, key| "Default value for #{key}" }
|
|
@@ -896,10 +826,10 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
896
826
|
|
|
897
827
|
# <!--
|
|
898
828
|
# rdoc-file=hash.c
|
|
899
|
-
# - default_proc = proc -> proc
|
|
829
|
+
# - hash.default_proc = proc -> proc
|
|
900
830
|
# -->
|
|
901
|
-
# Sets the default proc for `self` to `proc` (see [
|
|
902
|
-
#
|
|
831
|
+
# Sets the default proc for `self` to `proc` (see [Default
|
|
832
|
+
# Values](rdoc-ref:Hash@Default+Values)):
|
|
903
833
|
# h = {}
|
|
904
834
|
# h.default_proc # => nil
|
|
905
835
|
# h.default_proc = proc { |hash, key| "Default value for #{key}" }
|
|
@@ -911,200 +841,208 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
911
841
|
|
|
912
842
|
# <!--
|
|
913
843
|
# rdoc-file=hash.c
|
|
914
|
-
# - delete(key) -> value or nil
|
|
915
|
-
# - delete(key) {|key| ... } -> object
|
|
844
|
+
# - hash.delete(key) -> value or nil
|
|
845
|
+
# - hash.delete(key) {|key| ... } -> object
|
|
916
846
|
# -->
|
|
917
|
-
#
|
|
918
|
-
# associated value; otherwise returns `nil` or calls the given block.
|
|
919
|
-
#
|
|
920
|
-
# 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.
|
|
921
848
|
#
|
|
849
|
+
# If no block is given and `key` is found, deletes the entry and returns the
|
|
850
|
+
# associated value:
|
|
922
851
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
923
852
|
# h.delete(:bar) # => 1
|
|
924
|
-
# h # => {foo
|
|
925
|
-
#
|
|
926
|
-
# With no block given and `key` not found, returns `nil`.
|
|
853
|
+
# h # => {:foo=>0, :baz=>2}
|
|
927
854
|
#
|
|
928
|
-
#
|
|
929
|
-
# returns its value:
|
|
855
|
+
# If no block given and `key` is not found, returns `nil`.
|
|
930
856
|
#
|
|
857
|
+
# If a block is given and `key` is found, ignores the block, deletes the entry,
|
|
858
|
+
# and returns the associated value:
|
|
931
859
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
932
860
|
# h.delete(:baz) { |key| raise 'Will never happen'} # => 2
|
|
933
|
-
# h # => {foo
|
|
861
|
+
# h # => {:foo=>0, :bar=>1}
|
|
934
862
|
#
|
|
935
|
-
#
|
|
863
|
+
# If a block is given and `key` is not found, calls the block and returns the
|
|
936
864
|
# block's return value:
|
|
937
|
-
#
|
|
938
865
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
939
866
|
# h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
|
|
940
|
-
# h # => {foo
|
|
941
|
-
#
|
|
942
|
-
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
867
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2}
|
|
943
868
|
#
|
|
944
869
|
def delete: (K arg0) -> V?
|
|
945
870
|
| [U] (K arg0) { (K arg0) -> U } -> (U | V)
|
|
946
871
|
|
|
947
872
|
# <!--
|
|
948
873
|
# rdoc-file=hash.c
|
|
949
|
-
# - delete_if {|key, value| ... } -> self
|
|
950
|
-
# - delete_if -> new_enumerator
|
|
874
|
+
# - hash.delete_if {|key, value| ... } -> self
|
|
875
|
+
# - hash.delete_if -> new_enumerator
|
|
951
876
|
# -->
|
|
952
|
-
#
|
|
953
|
-
#
|
|
954
|
-
#
|
|
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`:
|
|
955
879
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
956
|
-
# h.delete_if {|key, value| value > 0 } # => {foo
|
|
957
|
-
#
|
|
958
|
-
# With no block given, returns a new Enumerator.
|
|
880
|
+
# h.delete_if {|key, value| value > 0 } # => {:foo=>0}
|
|
959
881
|
#
|
|
960
|
-
#
|
|
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}
|
|
961
886
|
#
|
|
962
887
|
def delete_if: () { (K, V) -> boolish } -> self
|
|
963
888
|
| () -> ::Enumerator[[ K, V ], self]
|
|
964
889
|
|
|
965
890
|
# <!--
|
|
966
891
|
# rdoc-file=hash.c
|
|
967
|
-
# - dig(key, *identifiers) -> object
|
|
892
|
+
# - hash.dig(key, *identifiers) -> object
|
|
968
893
|
# -->
|
|
969
|
-
# Finds and returns
|
|
970
|
-
# `identifiers`.
|
|
971
|
-
#
|
|
972
|
-
# The nested objects may be instances of various classes. See [Dig
|
|
973
|
-
# Methods](rdoc-ref:dig_methods.rdoc).
|
|
974
|
-
#
|
|
975
|
-
# 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).
|
|
976
897
|
#
|
|
898
|
+
# Nested Hashes:
|
|
977
899
|
# h = {foo: {bar: {baz: 2}}}
|
|
978
|
-
# h.dig(:foo) # => {bar:
|
|
979
|
-
# h.dig(:foo, :bar) # => {baz
|
|
900
|
+
# h.dig(:foo) # => {:bar=>{:baz=>2}}
|
|
901
|
+
# h.dig(:foo, :bar) # => {:baz=>2}
|
|
980
902
|
# h.dig(:foo, :bar, :baz) # => 2
|
|
981
903
|
# h.dig(:foo, :bar, :BAZ) # => nil
|
|
982
904
|
#
|
|
983
|
-
# Nested
|
|
984
|
-
#
|
|
905
|
+
# Nested Hashes and Arrays:
|
|
985
906
|
# h = {foo: {bar: [:a, :b, :c]}}
|
|
986
907
|
# h.dig(:foo, :bar, 2) # => :c
|
|
987
908
|
#
|
|
988
|
-
#
|
|
989
|
-
#
|
|
990
|
-
#
|
|
909
|
+
# This method will use the [default values](rdoc-ref:Hash@Default+Values) for
|
|
910
|
+
# keys that are not present:
|
|
991
911
|
# h = {foo: {bar: [:a, :b, :c]}}
|
|
992
912
|
# h.dig(:hello) # => nil
|
|
993
913
|
# h.default_proc = -> (hash, _key) { hash }
|
|
994
|
-
# h.dig(:hello, :world)
|
|
995
|
-
#
|
|
996
|
-
#
|
|
997
|
-
# 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
|
|
998
916
|
#
|
|
999
917
|
def dig: (K, *untyped) -> untyped
|
|
1000
918
|
|
|
1001
919
|
# <!-- rdoc-file=hash.c -->
|
|
1002
|
-
#
|
|
1003
|
-
#
|
|
920
|
+
# Calls the given block with each key-value pair; returns `self`:
|
|
1004
921
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1005
|
-
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo
|
|
922
|
+
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
|
|
1006
923
|
#
|
|
1007
924
|
# Output:
|
|
1008
|
-
#
|
|
1009
925
|
# foo: 0
|
|
1010
926
|
# bar: 1
|
|
1011
927
|
# baz: 2
|
|
1012
928
|
#
|
|
1013
|
-
#
|
|
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}
|
|
1014
934
|
#
|
|
1015
|
-
#
|
|
935
|
+
# Output:
|
|
936
|
+
# foo: 0
|
|
937
|
+
# bar: 1
|
|
938
|
+
# baz: 2
|
|
1016
939
|
#
|
|
1017
940
|
def each: () { ([ K, V ] arg0) -> untyped } -> self
|
|
1018
941
|
| () -> ::Enumerator[[ K, V ], self]
|
|
1019
942
|
|
|
1020
943
|
# <!--
|
|
1021
944
|
# rdoc-file=hash.c
|
|
1022
|
-
# - each_key {|key| ... } -> self
|
|
1023
|
-
# - each_key -> new_enumerator
|
|
945
|
+
# - hash.each_key {|key| ... } -> self
|
|
946
|
+
# - hash.each_key -> new_enumerator
|
|
1024
947
|
# -->
|
|
1025
|
-
#
|
|
1026
|
-
#
|
|
948
|
+
# Calls the given block with each key; returns `self`:
|
|
1027
949
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1028
|
-
# h.each_key {|key| puts key } # => {foo
|
|
950
|
+
# h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
|
|
1029
951
|
#
|
|
1030
952
|
# Output:
|
|
1031
953
|
# foo
|
|
1032
954
|
# bar
|
|
1033
955
|
# baz
|
|
1034
956
|
#
|
|
1035
|
-
#
|
|
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}
|
|
1036
962
|
#
|
|
1037
|
-
#
|
|
963
|
+
# Output:
|
|
964
|
+
# foo
|
|
965
|
+
# bar
|
|
966
|
+
# baz
|
|
1038
967
|
#
|
|
1039
968
|
def each_key: () { (K arg0) -> untyped } -> ::Hash[K, V]
|
|
1040
969
|
| () -> ::Enumerator[K, self]
|
|
1041
970
|
|
|
1042
971
|
# <!--
|
|
1043
972
|
# rdoc-file=hash.c
|
|
1044
|
-
# -
|
|
1045
|
-
# - 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
|
|
1046
977
|
# -->
|
|
1047
|
-
#
|
|
1048
|
-
#
|
|
978
|
+
# Calls the given block with each key-value pair; returns `self`:
|
|
1049
979
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1050
|
-
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo
|
|
980
|
+
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
|
|
1051
981
|
#
|
|
1052
982
|
# Output:
|
|
1053
|
-
#
|
|
1054
983
|
# foo: 0
|
|
1055
984
|
# bar: 1
|
|
1056
985
|
# baz: 2
|
|
1057
986
|
#
|
|
1058
|
-
#
|
|
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}
|
|
1059
992
|
#
|
|
1060
|
-
#
|
|
993
|
+
# Output:
|
|
994
|
+
# foo: 0
|
|
995
|
+
# bar: 1
|
|
996
|
+
# baz: 2
|
|
1061
997
|
#
|
|
1062
998
|
alias each_pair each
|
|
1063
999
|
|
|
1064
1000
|
# <!--
|
|
1065
1001
|
# rdoc-file=hash.c
|
|
1066
|
-
# - each_value {|value| ... } -> self
|
|
1067
|
-
# - each_value -> new_enumerator
|
|
1002
|
+
# - hash.each_value {|value| ... } -> self
|
|
1003
|
+
# - hash.each_value -> new_enumerator
|
|
1068
1004
|
# -->
|
|
1069
|
-
#
|
|
1070
|
-
#
|
|
1005
|
+
# Calls the given block with each value; returns `self`:
|
|
1071
1006
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1072
|
-
# h.each_value {|value| puts value } # => {foo
|
|
1007
|
+
# h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
|
|
1073
1008
|
#
|
|
1074
1009
|
# Output:
|
|
1075
1010
|
# 0
|
|
1076
1011
|
# 1
|
|
1077
1012
|
# 2
|
|
1078
1013
|
#
|
|
1079
|
-
#
|
|
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}
|
|
1080
1019
|
#
|
|
1081
|
-
#
|
|
1020
|
+
# Output:
|
|
1021
|
+
# 0
|
|
1022
|
+
# 1
|
|
1023
|
+
# 2
|
|
1082
1024
|
#
|
|
1083
1025
|
def each_value: () { (V arg0) -> untyped } -> self
|
|
1084
1026
|
| () -> ::Enumerator[V, self]
|
|
1085
1027
|
|
|
1086
1028
|
# <!--
|
|
1087
1029
|
# rdoc-file=hash.c
|
|
1088
|
-
# - empty? -> true or false
|
|
1030
|
+
# - hash.empty? -> true or false
|
|
1089
1031
|
# -->
|
|
1090
1032
|
# Returns `true` if there are no hash entries, `false` otherwise:
|
|
1091
|
-
#
|
|
1092
1033
|
# {}.empty? # => true
|
|
1093
|
-
# {foo: 0}.empty? # => false
|
|
1094
|
-
#
|
|
1095
|
-
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1034
|
+
# {foo: 0, bar: 1, baz: 2}.empty? # => false
|
|
1096
1035
|
#
|
|
1097
1036
|
def empty?: () -> bool
|
|
1098
1037
|
|
|
1099
1038
|
# <!--
|
|
1100
1039
|
# rdoc-file=hash.c
|
|
1101
|
-
# - eql?(object) -> true or false
|
|
1040
|
+
# - hash.eql?(object) -> true or false
|
|
1102
1041
|
# -->
|
|
1103
1042
|
# Returns `true` if all of the following are true:
|
|
1104
|
-
#
|
|
1105
|
-
# *
|
|
1106
|
-
# *
|
|
1107
|
-
# * 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])`.
|
|
1108
1046
|
#
|
|
1109
1047
|
# Otherwise, returns `false`.
|
|
1110
1048
|
#
|
|
@@ -1114,427 +1052,378 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
1114
1052
|
# h3 = {baz: 2, bar: 1, foo: 0}
|
|
1115
1053
|
# h1.eql? h3 # => true
|
|
1116
1054
|
#
|
|
1117
|
-
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1118
|
-
#
|
|
1119
1055
|
def eql?: (untyped) -> bool
|
|
1120
1056
|
|
|
1121
1057
|
# <!--
|
|
1122
1058
|
# rdoc-file=hash.c
|
|
1123
|
-
# - except(*keys) ->
|
|
1059
|
+
# - hsh.except(*keys) -> a_hash
|
|
1124
1060
|
# -->
|
|
1125
|
-
# Returns a
|
|
1126
|
-
#
|
|
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}
|
|
1127
1064
|
#
|
|
1128
|
-
#
|
|
1129
|
-
# h.except(:baz, :foo) # => {:bar=>1}
|
|
1130
|
-
# h.except(:bar, :nosuch) # => {:foo=>0, :baz=>2}
|
|
1131
|
-
#
|
|
1132
|
-
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1065
|
+
# Any given `keys` that are not found are ignored.
|
|
1133
1066
|
#
|
|
1134
1067
|
def except: (*K) -> ::Hash[K, V]
|
|
1135
1068
|
|
|
1136
1069
|
# <!--
|
|
1137
1070
|
# rdoc-file=hash.c
|
|
1138
|
-
# - fetch(key) -> object
|
|
1139
|
-
# - fetch(key, default_value) -> object
|
|
1140
|
-
# - fetch(key) {|key| ... } -> object
|
|
1071
|
+
# - hash.fetch(key) -> object
|
|
1072
|
+
# - hash.fetch(key, default_value) -> object
|
|
1073
|
+
# - hash.fetch(key) {|key| ... } -> object
|
|
1141
1074
|
# -->
|
|
1142
|
-
#
|
|
1143
|
-
#
|
|
1075
|
+
# Returns the value for the given `key`, if found.
|
|
1144
1076
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1145
|
-
# h.fetch(:bar)
|
|
1077
|
+
# h.fetch(:bar) # => 1
|
|
1146
1078
|
#
|
|
1147
|
-
# If
|
|
1148
|
-
#
|
|
1149
|
-
#
|
|
1150
|
-
# h.fetch(:nosuch, :default) # => :default
|
|
1151
|
-
# h.fetch(:nosuch) # Raises KeyError.
|
|
1152
|
-
#
|
|
1153
|
-
# With a block given, calls the block with `key` and returns the block's return
|
|
1154
|
-
# value:
|
|
1079
|
+
# If `key` is not found and no block was given, returns `default_value`:
|
|
1080
|
+
# {}.fetch(:nosuch, :default) # => :default
|
|
1155
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:
|
|
1156
1084
|
# {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
|
|
1157
1085
|
#
|
|
1086
|
+
# Raises KeyError if neither `default_value` nor a block was given.
|
|
1087
|
+
#
|
|
1158
1088
|
# Note that this method does not use the values of either #default or
|
|
1159
1089
|
# #default_proc.
|
|
1160
1090
|
#
|
|
1161
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1162
|
-
#
|
|
1163
1091
|
def fetch: (K arg0) -> V
|
|
1164
1092
|
| [X] (K arg0, X arg1) -> (V | X)
|
|
1165
1093
|
| [X] (K arg0) { (K arg0) -> X } -> (V | X)
|
|
1166
1094
|
|
|
1167
1095
|
# <!--
|
|
1168
1096
|
# rdoc-file=hash.c
|
|
1169
|
-
# - fetch_values(*keys) -> new_array
|
|
1170
|
-
# - fetch_values(*keys) {|key| ... } -> new_array
|
|
1097
|
+
# - hash.fetch_values(*keys) -> new_array
|
|
1098
|
+
# - hash.fetch_values(*keys) {|key| ... } -> new_array
|
|
1171
1099
|
# -->
|
|
1172
|
-
#
|
|
1173
|
-
#
|
|
1174
|
-
#
|
|
1100
|
+
# Returns a new Array containing the values associated with the given keys
|
|
1101
|
+
# *keys:
|
|
1175
1102
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1176
1103
|
# h.fetch_values(:baz, :foo) # => [2, 0]
|
|
1177
1104
|
#
|
|
1178
|
-
#
|
|
1179
|
-
# each unfound key and uses the block's return value as the value for that key:
|
|
1180
|
-
#
|
|
1181
|
-
# h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
|
|
1182
|
-
# # => [1, 0, "bad", "bam"]
|
|
1105
|
+
# Returns a new empty Array if no arguments given.
|
|
1183
1106
|
#
|
|
1184
|
-
# 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"]
|
|
1185
1112
|
#
|
|
1186
|
-
#
|
|
1113
|
+
# When no block is given, raises an exception if any given key is not found.
|
|
1187
1114
|
#
|
|
1188
1115
|
def fetch_values: (*K) -> ::Array[V]
|
|
1189
1116
|
| [X] (*K) { (K) -> X } -> ::Array[V | X]
|
|
1190
1117
|
|
|
1191
1118
|
# <!-- rdoc-file=hash.c -->
|
|
1192
|
-
#
|
|
1193
|
-
#
|
|
1194
|
-
#
|
|
1119
|
+
# Returns a new `Hash` object whose entries are those for which the block
|
|
1120
|
+
# returns a truthy value:
|
|
1195
1121
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1196
|
-
# h.select {|key, value| value < 2 } # => {foo
|
|
1122
|
+
# h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
|
|
1197
1123
|
#
|
|
1198
|
-
#
|
|
1199
|
-
#
|
|
1200
|
-
#
|
|
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}
|
|
1201
1128
|
#
|
|
1202
1129
|
def filter: () { (K, V) -> boolish } -> ::Hash[K, V]
|
|
1203
1130
|
| () -> ::Enumerator[[ K, V ], ::Hash[K, V]]
|
|
1204
1131
|
|
|
1205
1132
|
# <!-- rdoc-file=hash.c -->
|
|
1206
|
-
#
|
|
1207
|
-
#
|
|
1208
|
-
#
|
|
1209
|
-
# Returns `self` if any entries were removed, `nil` otherwise:
|
|
1210
|
-
#
|
|
1133
|
+
# Returns `self`, whose entries are those for which the block returns a truthy
|
|
1134
|
+
# value:
|
|
1211
1135
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1212
|
-
# h.select! {|key, value| value < 2 }
|
|
1213
|
-
# h.select! {|key, value| value < 2 } # => nil
|
|
1136
|
+
# h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
|
|
1214
1137
|
#
|
|
1215
|
-
#
|
|
1138
|
+
# Returns `nil` if no entries were removed.
|
|
1216
1139
|
#
|
|
1217
|
-
#
|
|
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}
|
|
1218
1144
|
#
|
|
1219
1145
|
def filter!: () { (K, V) -> boolish } -> self?
|
|
1220
1146
|
| () -> ::Enumerator[[ K, V ], self?]
|
|
1221
1147
|
|
|
1222
1148
|
# <!--
|
|
1223
1149
|
# rdoc-file=hash.c
|
|
1224
|
-
# - flatten
|
|
1150
|
+
# - hash.flatten -> new_array
|
|
1151
|
+
# - hash.flatten(level) -> new_array
|
|
1225
1152
|
# -->
|
|
1226
|
-
#
|
|
1227
|
-
# flattening of `self` to the given `depth`.
|
|
1228
|
-
#
|
|
1229
|
-
# At each level of recursion:
|
|
1230
|
-
#
|
|
1231
|
-
# * Each element whose value is an array is "flattened" (that is, replaced by
|
|
1232
|
-
# its individual array elements); see Array#flatten.
|
|
1233
|
-
# * Each element whose value is not an array is unchanged. even if the value
|
|
1234
|
-
# is an object that has instance method flatten (such as a hash).
|
|
1235
|
-
#
|
|
1236
|
-
# Examples; note that entry `foo: {bar: 1, baz: 2}` is never flattened.
|
|
1237
|
-
#
|
|
1238
|
-
# h = {foo: {bar: 1, baz: 2}, bat: [:bam, [:bap, [:bah]]]}
|
|
1239
|
-
# h.flatten(1) # => [:foo, {:bar=>1, :baz=>2}, :bat, [:bam, [:bap, [:bah]]]]
|
|
1240
|
-
# h.flatten(2) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, [:bap, [:bah]]]
|
|
1241
|
-
# h.flatten(3) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, [:bah]]
|
|
1242
|
-
# h.flatten(4) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
|
|
1243
|
-
# 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`.
|
|
1244
1154
|
#
|
|
1245
|
-
#
|
|
1155
|
+
# ---
|
|
1246
1156
|
#
|
|
1247
|
-
#
|
|
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]
|
|
1248
1160
|
#
|
|
1249
|
-
#
|
|
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]
|
|
1250
1167
|
#
|
|
1251
|
-
#
|
|
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]
|
|
1252
1172
|
#
|
|
1253
|
-
#
|
|
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
|
|
1254
1177
|
#
|
|
1255
1178
|
def flatten: () -> ::Array[K | V]
|
|
1256
1179
|
| (1 level) -> ::Array[K | V]
|
|
1257
1180
|
| (Integer level) -> Array[untyped]
|
|
1258
1181
|
|
|
1259
1182
|
# <!-- rdoc-file=hash.c -->
|
|
1260
|
-
# Returns
|
|
1261
|
-
#
|
|
1262
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1263
|
-
# h.include?(:bar) # => true
|
|
1264
|
-
# h.include?(:BAR) # => false
|
|
1265
|
-
#
|
|
1266
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1183
|
+
# Returns `true` if `key` is a key in `self`, otherwise `false`.
|
|
1267
1184
|
#
|
|
1268
1185
|
def has_key?: (K arg0) -> bool
|
|
1269
1186
|
|
|
1270
1187
|
# <!--
|
|
1271
1188
|
# rdoc-file=hash.c
|
|
1272
|
-
# - has_value?(value) -> true or false
|
|
1189
|
+
# - hash.has_value?(value) -> true or false
|
|
1190
|
+
# - hash.value?(value) -> true or false
|
|
1273
1191
|
# -->
|
|
1274
|
-
# Returns
|
|
1275
|
-
#
|
|
1276
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1192
|
+
# Returns `true` if `value` is a value in `self`, otherwise `false`.
|
|
1277
1193
|
#
|
|
1278
1194
|
def has_value?: (V arg0) -> bool
|
|
1279
1195
|
|
|
1280
1196
|
# <!--
|
|
1281
1197
|
# rdoc-file=hash.c
|
|
1282
|
-
# - hash -> an_integer
|
|
1198
|
+
# - hash.hash -> an_integer
|
|
1283
1199
|
# -->
|
|
1284
|
-
# Returns the
|
|
1285
|
-
#
|
|
1286
|
-
# Two hashes have the same hash-code if their content is the same (regardless of
|
|
1287
|
-
# order):
|
|
1200
|
+
# Returns the Integer hash-code for the hash.
|
|
1288
1201
|
#
|
|
1202
|
+
# Two `Hash` objects have the same hash-code if their content is the same
|
|
1203
|
+
# (regardless of order):
|
|
1289
1204
|
# h1 = {foo: 0, bar: 1, baz: 2}
|
|
1290
1205
|
# h2 = {baz: 2, bar: 1, foo: 0}
|
|
1291
1206
|
# h2.hash == h1.hash # => true
|
|
1292
1207
|
# h2.eql? h1 # => true
|
|
1293
1208
|
#
|
|
1294
|
-
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1295
|
-
#
|
|
1296
1209
|
def hash: () -> Integer
|
|
1297
1210
|
|
|
1298
1211
|
# <!--
|
|
1299
1212
|
# rdoc-file=hash.c
|
|
1300
|
-
# - 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
|
|
1301
1217
|
# -->
|
|
1302
|
-
# Returns
|
|
1303
|
-
#
|
|
1304
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1305
|
-
# h.include?(:bar) # => true
|
|
1306
|
-
# h.include?(:BAR) # => false
|
|
1307
|
-
#
|
|
1308
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1218
|
+
# Returns `true` if `key` is a key in `self`, otherwise `false`.
|
|
1309
1219
|
#
|
|
1310
1220
|
alias include? has_key?
|
|
1311
1221
|
|
|
1312
1222
|
# <!--
|
|
1313
1223
|
# rdoc-file=hash.c
|
|
1314
|
-
# - inspect -> new_string
|
|
1224
|
+
# - hash.inspect -> new_string
|
|
1315
1225
|
# -->
|
|
1316
|
-
# Returns a new
|
|
1226
|
+
# Returns a new String containing the hash entries:
|
|
1317
1227
|
#
|
|
1318
1228
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1319
1229
|
# h.inspect # => "{foo: 0, bar: 1, baz: 2}"
|
|
1320
1230
|
#
|
|
1321
|
-
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1322
|
-
#
|
|
1323
1231
|
def inspect: () -> String
|
|
1324
1232
|
|
|
1325
1233
|
# <!--
|
|
1326
1234
|
# rdoc-file=hash.c
|
|
1327
|
-
# - invert -> new_hash
|
|
1235
|
+
# - hash.invert -> new_hash
|
|
1328
1236
|
# -->
|
|
1329
|
-
# Returns a new
|
|
1330
|
-
#
|
|
1237
|
+
# Returns a new `Hash` object with the each key-value pair inverted:
|
|
1331
1238
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1332
1239
|
# h1 = h.invert
|
|
1333
1240
|
# h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
|
|
1334
1241
|
#
|
|
1335
|
-
# Overwrites any repeated new keys (see [Entry
|
|
1242
|
+
# Overwrites any repeated new keys: (see [Entry
|
|
1336
1243
|
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
1337
|
-
#
|
|
1338
1244
|
# h = {foo: 0, bar: 0, baz: 0}
|
|
1339
1245
|
# h.invert # => {0=>:baz}
|
|
1340
1246
|
#
|
|
1341
|
-
# Related: see [Methods for Transforming Keys and
|
|
1342
|
-
# Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values).
|
|
1343
|
-
#
|
|
1344
1247
|
def invert: () -> ::Hash[V, K]
|
|
1345
1248
|
|
|
1346
1249
|
# <!--
|
|
1347
1250
|
# rdoc-file=hash.c
|
|
1348
|
-
# - keep_if {|key, value| ... } -> self
|
|
1349
|
-
# - keep_if -> new_enumerator
|
|
1251
|
+
# - hash.keep_if {|key, value| ... } -> self
|
|
1252
|
+
# - hash.keep_if -> new_enumerator
|
|
1350
1253
|
# -->
|
|
1351
|
-
#
|
|
1352
|
-
#
|
|
1353
|
-
# `self`:
|
|
1354
|
-
#
|
|
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`.
|
|
1355
1256
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1356
|
-
# h.keep_if { |key, value| key.start_with?('b') } # => {bar
|
|
1357
|
-
#
|
|
1358
|
-
# With no block given, returns a new Enumerator.
|
|
1257
|
+
# h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
|
|
1359
1258
|
#
|
|
1360
|
-
#
|
|
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}
|
|
1361
1263
|
#
|
|
1362
1264
|
def keep_if: () { (K, V) -> boolish } -> self
|
|
1363
1265
|
| () -> ::Enumerator[[ K, V ], self]
|
|
1364
1266
|
|
|
1365
1267
|
# <!--
|
|
1366
1268
|
# rdoc-file=hash.c
|
|
1367
|
-
# - key(value) -> key or nil
|
|
1269
|
+
# - hash.key(value) -> key or nil
|
|
1368
1270
|
# -->
|
|
1369
1271
|
# Returns the key for the first-found entry with the given `value` (see [Entry
|
|
1370
1272
|
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
1371
|
-
#
|
|
1372
1273
|
# h = {foo: 0, bar: 2, baz: 2}
|
|
1373
1274
|
# h.key(0) # => :foo
|
|
1374
1275
|
# h.key(2) # => :bar
|
|
1375
1276
|
#
|
|
1376
1277
|
# Returns `nil` if no such value is found.
|
|
1377
1278
|
#
|
|
1378
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1379
|
-
#
|
|
1380
1279
|
def key: (V) -> K?
|
|
1381
1280
|
|
|
1382
1281
|
# <!-- rdoc-file=hash.c -->
|
|
1383
|
-
# Returns
|
|
1384
|
-
#
|
|
1385
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1386
|
-
# h.include?(:bar) # => true
|
|
1387
|
-
# h.include?(:BAR) # => false
|
|
1388
|
-
#
|
|
1389
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1282
|
+
# Returns `true` if `key` is a key in `self`, otherwise `false`.
|
|
1390
1283
|
#
|
|
1391
1284
|
alias key? has_key?
|
|
1392
1285
|
|
|
1393
1286
|
# <!--
|
|
1394
1287
|
# rdoc-file=hash.c
|
|
1395
|
-
# - keys -> new_array
|
|
1288
|
+
# - hash.keys -> new_array
|
|
1396
1289
|
# -->
|
|
1397
|
-
# Returns a new
|
|
1398
|
-
#
|
|
1290
|
+
# Returns a new Array containing all keys in `self`:
|
|
1399
1291
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1400
1292
|
# h.keys # => [:foo, :bar, :baz]
|
|
1401
1293
|
#
|
|
1402
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1403
|
-
#
|
|
1404
1294
|
def keys: () -> ::Array[K]
|
|
1405
1295
|
|
|
1406
1296
|
# <!-- rdoc-file=hash.c -->
|
|
1407
1297
|
# Returns the count of entries in `self`:
|
|
1408
1298
|
#
|
|
1409
|
-
# {foo: 0, bar: 1, baz: 2}.
|
|
1410
|
-
#
|
|
1411
|
-
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1299
|
+
# {foo: 0, bar: 1, baz: 2}.length # => 3
|
|
1412
1300
|
#
|
|
1413
1301
|
def length: () -> Integer
|
|
1414
1302
|
|
|
1415
1303
|
# <!-- rdoc-file=hash.c -->
|
|
1416
|
-
# Returns
|
|
1417
|
-
#
|
|
1418
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1419
|
-
# h.include?(:bar) # => true
|
|
1420
|
-
# h.include?(:BAR) # => false
|
|
1421
|
-
#
|
|
1422
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1304
|
+
# Returns `true` if `key` is a key in `self`, otherwise `false`.
|
|
1423
1305
|
#
|
|
1424
1306
|
alias member? has_key?
|
|
1425
1307
|
|
|
1426
1308
|
# <!--
|
|
1427
1309
|
# rdoc-file=hash.c
|
|
1428
|
-
# - merge
|
|
1429
|
-
# - 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
|
|
1430
1313
|
# -->
|
|
1431
|
-
#
|
|
1314
|
+
# Returns the new `Hash` formed by merging each of `other_hashes` into a copy of
|
|
1315
|
+
# `self`.
|
|
1432
1316
|
#
|
|
1433
|
-
#
|
|
1434
|
-
# by merging each successive `other_hash` into a copy of `self`; returns that
|
|
1435
|
-
# copy; for each successive entry in `other_hash`:
|
|
1317
|
+
# Each argument in `other_hashes` must be a `Hash`.
|
|
1436
1318
|
#
|
|
1437
|
-
#
|
|
1438
|
-
# * For duplicate key, the entry overwrites the entry in `self`, whose
|
|
1439
|
-
# position is unchanged.
|
|
1319
|
+
# ---
|
|
1440
1320
|
#
|
|
1441
|
-
#
|
|
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.
|
|
1442
1326
|
#
|
|
1327
|
+
# Example:
|
|
1443
1328
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1444
1329
|
# h1 = {bat: 3, bar: 4}
|
|
1445
1330
|
# h2 = {bam: 5, bat:6}
|
|
1446
|
-
# h.merge(h1, h2) # => {foo
|
|
1331
|
+
# h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
|
|
1447
1332
|
#
|
|
1448
|
-
# With arguments
|
|
1449
|
-
#
|
|
1450
|
-
#
|
|
1451
|
-
#
|
|
1452
|
-
# *
|
|
1453
|
-
#
|
|
1454
|
-
#
|
|
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.
|
|
1455
1341
|
#
|
|
1456
1342
|
# Example:
|
|
1457
|
-
#
|
|
1458
1343
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1459
1344
|
# h1 = {bat: 3, bar: 4}
|
|
1460
1345
|
# h2 = {bam: 5, bat:6}
|
|
1461
|
-
# h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
|
|
1462
|
-
# # => {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}
|
|
1463
1348
|
#
|
|
1464
|
-
# With no arguments
|
|
1349
|
+
# With no arguments:
|
|
1350
|
+
# * Returns a copy of `self`.
|
|
1351
|
+
# * The block, if given, is ignored.
|
|
1465
1352
|
#
|
|
1466
|
-
#
|
|
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}
|
|
1467
1358
|
#
|
|
1468
1359
|
def merge: [A, B] (*::Hash[A, B] other_hashes) -> ::Hash[A | K, B | V]
|
|
1469
1360
|
| [A, B, C] (*::Hash[A, B] other_hashes) { (K key, V oldval, B newval) -> C } -> ::Hash[A | K, B | V | C]
|
|
1470
1361
|
|
|
1471
1362
|
# <!-- rdoc-file=hash.c -->
|
|
1472
|
-
#
|
|
1473
|
-
#
|
|
1474
|
-
# Each argument `other_hash` in `other_hashes` must be a hash.
|
|
1363
|
+
# Merges each of `other_hashes` into `self`; returns `self`.
|
|
1475
1364
|
#
|
|
1476
|
-
#
|
|
1477
|
-
# successive `other_hash`:
|
|
1365
|
+
# Each argument in `other_hashes` must be a `Hash`.
|
|
1478
1366
|
#
|
|
1479
|
-
#
|
|
1480
|
-
#
|
|
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.
|
|
1481
1372
|
#
|
|
1482
|
-
#
|
|
1483
|
-
#
|
|
1484
|
-
#
|
|
1485
|
-
#
|
|
1486
|
-
#
|
|
1487
|
-
#
|
|
1488
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1489
|
-
# h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4}
|
|
1490
|
-
#
|
|
1491
|
-
# With a block given, for each successive entry `key`/`new_value` in each
|
|
1492
|
-
# successive `other_hash`:
|
|
1493
|
-
#
|
|
1494
|
-
# * If `key` is in `self`, fetches `old_value` from `self[key]`, calls the
|
|
1495
|
-
# block with `key`, `old_value`, and `new_value`, and sets `self[key] =
|
|
1496
|
-
# 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}
|
|
1497
1378
|
#
|
|
1498
|
-
#
|
|
1499
|
-
#
|
|
1500
|
-
#
|
|
1501
|
-
#
|
|
1502
|
-
#
|
|
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.
|
|
1503
1386
|
#
|
|
1504
|
-
#
|
|
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}
|
|
1505
1393
|
#
|
|
1506
|
-
#
|
|
1507
|
-
#
|
|
1508
|
-
#
|
|
1394
|
+
# With no arguments:
|
|
1395
|
+
# * Returns `self`, unmodified.
|
|
1396
|
+
# * The block, if given, is ignored.
|
|
1509
1397
|
#
|
|
1510
|
-
#
|
|
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}
|
|
1511
1403
|
#
|
|
1512
1404
|
def merge!: (*::Hash[K, V] other_hashes) -> self
|
|
1513
1405
|
| (*::Hash[K, V] other_hashes) { (K key, V oldval, V newval) -> V } -> self
|
|
1514
1406
|
|
|
1515
1407
|
# <!--
|
|
1516
1408
|
# rdoc-file=hash.c
|
|
1517
|
-
# - rassoc(value) -> new_array or nil
|
|
1409
|
+
# - hash.rassoc(value) -> new_array or nil
|
|
1518
1410
|
# -->
|
|
1519
|
-
#
|
|
1520
|
-
# see [Entry
|
|
1521
|
-
#
|
|
1522
|
-
# If the entry is found, returns its key and value as a 2-element array; returns
|
|
1523
|
-
# `nil` if not found:
|
|
1524
|
-
#
|
|
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)):
|
|
1525
1414
|
# h = {foo: 0, bar: 1, baz: 1}
|
|
1526
1415
|
# h.rassoc(1) # => [:bar, 1]
|
|
1527
1416
|
#
|
|
1528
|
-
#
|
|
1417
|
+
# Returns `nil` if no such value found.
|
|
1529
1418
|
#
|
|
1530
1419
|
def rassoc: (V) -> [ K, V ]?
|
|
1531
1420
|
|
|
1532
1421
|
# <!--
|
|
1533
1422
|
# rdoc-file=hash.c
|
|
1534
|
-
# - rehash -> self
|
|
1423
|
+
# - hash.rehash -> self
|
|
1535
1424
|
# -->
|
|
1536
|
-
# Rebuilds the hash table
|
|
1537
|
-
#
|
|
1425
|
+
# Rebuilds the hash table by recomputing the hash index for each key; returns
|
|
1426
|
+
# `self`.
|
|
1538
1427
|
#
|
|
1539
1428
|
# The hash table becomes invalid if the hash value of a key has changed after
|
|
1540
1429
|
# the entry was created. See [Modifying an Active Hash
|
|
@@ -1544,41 +1433,40 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
1544
1433
|
|
|
1545
1434
|
# <!--
|
|
1546
1435
|
# rdoc-file=hash.c
|
|
1547
|
-
# - reject {|key, value| ... } -> new_hash
|
|
1548
|
-
# - reject -> new_enumerator
|
|
1436
|
+
# - hash.reject {|key, value| ... } -> new_hash
|
|
1437
|
+
# - hash.reject -> new_enumerator
|
|
1549
1438
|
# -->
|
|
1550
|
-
#
|
|
1551
|
-
#
|
|
1552
|
-
# copy if the block returns a truthy value, includes it otherwise:
|
|
1553
|
-
#
|
|
1439
|
+
# Returns a new `Hash` object whose entries are all those from `self` for which
|
|
1440
|
+
# the block returns `false` or `nil`:
|
|
1554
1441
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1555
|
-
# h.reject {|key, value| key.start_with?('b') }
|
|
1556
|
-
# # => {foo
|
|
1557
|
-
#
|
|
1558
|
-
# With no block given, returns a new Enumerator.
|
|
1442
|
+
# h1 = h.reject {|key, value| key.start_with?('b') }
|
|
1443
|
+
# h1 # => {:foo=>0}
|
|
1559
1444
|
#
|
|
1560
|
-
#
|
|
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}
|
|
1561
1450
|
#
|
|
1562
1451
|
def reject: () -> ::Enumerator[[ K, V ], ::Hash[K, V]]
|
|
1563
1452
|
| () { (K, V) -> boolish } -> ::Hash[K, V]
|
|
1564
1453
|
|
|
1565
1454
|
# <!--
|
|
1566
1455
|
# rdoc-file=hash.c
|
|
1567
|
-
# - reject! {|key, value| ... } -> self or nil
|
|
1568
|
-
# - reject! -> new_enumerator
|
|
1456
|
+
# - hash.reject! {|key, value| ... } -> self or nil
|
|
1457
|
+
# - hash.reject! -> new_enumerator
|
|
1569
1458
|
# -->
|
|
1570
|
-
#
|
|
1571
|
-
#
|
|
1572
|
-
#
|
|
1573
|
-
# Return `self` if any entries were removed, `nil` otherwise:
|
|
1574
|
-
#
|
|
1459
|
+
# Returns `self`, whose remaining entries are those for which the block returns
|
|
1460
|
+
# `false` or `nil`:
|
|
1575
1461
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1576
|
-
# h.reject! {|key, value| value < 2 } # => {baz
|
|
1577
|
-
# h.reject! {|key, value| value < 2 } # => nil
|
|
1462
|
+
# h.reject! {|key, value| value < 2 } # => {:baz=>2}
|
|
1578
1463
|
#
|
|
1579
|
-
#
|
|
1464
|
+
# Returns `nil` if no entries are removed.
|
|
1580
1465
|
#
|
|
1581
|
-
#
|
|
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}
|
|
1582
1470
|
#
|
|
1583
1471
|
def reject!: () -> ::Enumerator[[ K, V ], self?]
|
|
1584
1472
|
| () { (K, V) -> boolish } -> self?
|
|
@@ -1586,182 +1474,147 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
1586
1474
|
# <!-- rdoc-file=hash.c -->
|
|
1587
1475
|
# Replaces the entire contents of `self` with the contents of `other_hash`;
|
|
1588
1476
|
# returns `self`:
|
|
1589
|
-
#
|
|
1590
1477
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1591
|
-
# h.replace({bat: 3, bam: 4}) # => {bat
|
|
1592
|
-
#
|
|
1593
|
-
# Also replaces the default value or proc of `self` with the default value or
|
|
1594
|
-
# proc of `other_hash`.
|
|
1595
|
-
#
|
|
1596
|
-
# h = {}
|
|
1597
|
-
# other = Hash.new(:ok)
|
|
1598
|
-
# h.replace(other)
|
|
1599
|
-
# h.default # => :ok
|
|
1600
|
-
#
|
|
1601
|
-
# Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
|
|
1478
|
+
# h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
|
|
1602
1479
|
#
|
|
1603
1480
|
def replace: (Hash[K, V]) -> self
|
|
1604
1481
|
|
|
1605
1482
|
# <!--
|
|
1606
1483
|
# rdoc-file=hash.c
|
|
1607
|
-
# - select {|key, value| ... } -> new_hash
|
|
1608
|
-
# - select -> new_enumerator
|
|
1484
|
+
# - hash.select {|key, value| ... } -> new_hash
|
|
1485
|
+
# - hash.select -> new_enumerator
|
|
1609
1486
|
# -->
|
|
1610
|
-
#
|
|
1611
|
-
#
|
|
1612
|
-
#
|
|
1487
|
+
# Returns a new `Hash` object whose entries are those for which the block
|
|
1488
|
+
# returns a truthy value:
|
|
1613
1489
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1614
|
-
# h.select {|key, value| value < 2 } # => {foo
|
|
1490
|
+
# h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
|
|
1615
1491
|
#
|
|
1616
|
-
#
|
|
1617
|
-
#
|
|
1618
|
-
#
|
|
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}
|
|
1619
1496
|
#
|
|
1620
1497
|
alias select filter
|
|
1621
1498
|
|
|
1622
1499
|
# <!--
|
|
1623
1500
|
# rdoc-file=hash.c
|
|
1624
|
-
# - select! {|key, value| ... } -> self or nil
|
|
1625
|
-
# - select! -> new_enumerator
|
|
1501
|
+
# - hash.select! {|key, value| ... } -> self or nil
|
|
1502
|
+
# - hash.select! -> new_enumerator
|
|
1626
1503
|
# -->
|
|
1627
|
-
#
|
|
1628
|
-
#
|
|
1629
|
-
#
|
|
1630
|
-
# Returns `self` if any entries were removed, `nil` otherwise:
|
|
1631
|
-
#
|
|
1504
|
+
# Returns `self`, whose entries are those for which the block returns a truthy
|
|
1505
|
+
# value:
|
|
1632
1506
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1633
|
-
# h.select! {|key, value| value < 2 }
|
|
1634
|
-
# h.select! {|key, value| value < 2 } # => nil
|
|
1507
|
+
# h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
|
|
1635
1508
|
#
|
|
1636
|
-
#
|
|
1509
|
+
# Returns `nil` if no entries were removed.
|
|
1637
1510
|
#
|
|
1638
|
-
#
|
|
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}
|
|
1639
1515
|
#
|
|
1640
1516
|
alias select! filter!
|
|
1641
1517
|
|
|
1642
1518
|
# <!--
|
|
1643
1519
|
# rdoc-file=hash.c
|
|
1644
|
-
# - shift -> [key, value] or nil
|
|
1520
|
+
# - hash.shift -> [key, value] or nil
|
|
1645
1521
|
# -->
|
|
1646
|
-
# Removes
|
|
1647
|
-
#
|
|
1648
|
-
#
|
|
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:
|
|
1649
1524
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1650
1525
|
# h.shift # => [:foo, 0]
|
|
1651
|
-
# h
|
|
1652
|
-
#
|
|
1653
|
-
# Returns `nil` if `self` is empty.
|
|
1526
|
+
# h # => {:bar=>1, :baz=>2}
|
|
1654
1527
|
#
|
|
1655
|
-
#
|
|
1528
|
+
# Returns nil if the hash is empty.
|
|
1656
1529
|
#
|
|
1657
1530
|
def shift: () -> [ K, V ]?
|
|
1658
1531
|
|
|
1659
1532
|
# <!--
|
|
1660
1533
|
# rdoc-file=hash.c
|
|
1661
|
-
# -
|
|
1534
|
+
# - hash.length -> integer
|
|
1535
|
+
# - hash.size -> integer
|
|
1662
1536
|
# -->
|
|
1663
1537
|
# Returns the count of entries in `self`:
|
|
1664
1538
|
#
|
|
1665
|
-
# {foo: 0, bar: 1, baz: 2}.
|
|
1666
|
-
#
|
|
1667
|
-
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1539
|
+
# {foo: 0, bar: 1, baz: 2}.length # => 3
|
|
1668
1540
|
#
|
|
1669
1541
|
alias size length
|
|
1670
1542
|
|
|
1671
1543
|
# <!--
|
|
1672
1544
|
# rdoc-file=hash.c
|
|
1673
|
-
# - slice(*keys) -> new_hash
|
|
1545
|
+
# - hash.slice(*keys) -> new_hash
|
|
1674
1546
|
# -->
|
|
1675
|
-
# Returns a new
|
|
1676
|
-
# ignores any keys that are not found:
|
|
1677
|
-
#
|
|
1547
|
+
# Returns a new `Hash` object containing the entries for the given `keys`:
|
|
1678
1548
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1679
|
-
# h.slice(:baz, :foo
|
|
1549
|
+
# h.slice(:baz, :foo) # => {:baz=>2, :foo=>0}
|
|
1680
1550
|
#
|
|
1681
|
-
#
|
|
1551
|
+
# Any given `keys` that are not found are ignored.
|
|
1682
1552
|
#
|
|
1683
1553
|
def slice: (*K) -> ::Hash[K, V]
|
|
1684
1554
|
|
|
1685
1555
|
# <!-- rdoc-file=hash.c -->
|
|
1686
|
-
# Associates the given `
|
|
1687
|
-
#
|
|
1688
|
-
# Searches for a hash key equivalent to the given `key`; see [Hash Key
|
|
1689
|
-
# Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
|
|
1690
|
-
#
|
|
1691
|
-
# If the key is found, replaces its value with the given `object`; the ordering
|
|
1692
|
-
# is not affected (see [Entry Order](rdoc-ref:Hash@Entry+Order)):
|
|
1556
|
+
# Associates the given `value` with the given `key`; returns `value`.
|
|
1693
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)):
|
|
1694
1560
|
# h = {foo: 0, bar: 1}
|
|
1695
1561
|
# h[:foo] = 2 # => 2
|
|
1696
|
-
# h
|
|
1697
|
-
#
|
|
1698
|
-
# If `key` is not found, creates a new entry for the given `key` and `object`;
|
|
1699
|
-
# the new entry is last in the order (see [Entry
|
|
1700
|
-
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
1562
|
+
# h.store(:bar, 3) # => 3
|
|
1563
|
+
# h # => {:foo=>2, :bar=>3}
|
|
1701
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)):
|
|
1702
1567
|
# h = {foo: 0, bar: 1}
|
|
1703
1568
|
# h[:baz] = 2 # => 2
|
|
1704
|
-
# h
|
|
1705
|
-
# h
|
|
1706
|
-
#
|
|
1707
|
-
# Related: #[]; see also [Methods for
|
|
1708
|
-
# Assigning](rdoc-ref:Hash@Methods+for+Assigning).
|
|
1569
|
+
# h.store(:bat, 3) # => 3
|
|
1570
|
+
# h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
|
|
1709
1571
|
#
|
|
1710
1572
|
alias store []=
|
|
1711
1573
|
|
|
1712
1574
|
# <!--
|
|
1713
1575
|
# rdoc-file=hash.c
|
|
1714
|
-
# - to_a -> new_array
|
|
1576
|
+
# - hash.to_a -> new_array
|
|
1715
1577
|
# -->
|
|
1716
|
-
# Returns
|
|
1717
|
-
#
|
|
1718
|
-
#
|
|
1578
|
+
# Returns a new Array of 2-element Array objects; each nested Array contains a
|
|
1579
|
+
# key-value pair from `self`:
|
|
1719
1580
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1720
1581
|
# h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
|
|
1721
1582
|
#
|
|
1722
|
-
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1723
|
-
#
|
|
1724
1583
|
def to_a: () -> ::Array[[ K, V ]]
|
|
1725
1584
|
|
|
1726
1585
|
# <!--
|
|
1727
1586
|
# rdoc-file=hash.c
|
|
1728
|
-
# - to_h
|
|
1729
|
-
# - to_h
|
|
1587
|
+
# - hash.to_h -> self or new_hash
|
|
1588
|
+
# - hash.to_h {|key, value| ... } -> new_hash
|
|
1730
1589
|
# -->
|
|
1731
|
-
#
|
|
1732
|
-
# the block is called with each entry's key and value; the block should return a
|
|
1733
|
-
# 2-element array containing the key and value to be included in the returned
|
|
1734
|
-
# array:
|
|
1590
|
+
# For an instance of `Hash`, returns `self`.
|
|
1735
1591
|
#
|
|
1736
|
-
#
|
|
1737
|
-
# h.to_h {|key, value| [value, key] }
|
|
1738
|
-
# # => {0 => :foo, 1 => :bar, 2 => :baz}
|
|
1739
|
-
#
|
|
1740
|
-
# With no block given, returns `self` if `self` is an instance of `Hash`; if
|
|
1741
|
-
# `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
|
|
1742
1593
|
# `self`.
|
|
1743
1594
|
#
|
|
1744
|
-
#
|
|
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}
|
|
1745
1601
|
#
|
|
1746
1602
|
def to_h: () -> Hash[K, V]
|
|
1747
1603
|
| [A, B] () { (K, V) -> [ A, B ] } -> Hash[A, B]
|
|
1748
1604
|
|
|
1749
1605
|
# <!--
|
|
1750
1606
|
# rdoc-file=hash.c
|
|
1751
|
-
# - to_hash -> self
|
|
1607
|
+
# - hash.to_hash -> self
|
|
1752
1608
|
# -->
|
|
1753
1609
|
# Returns `self`.
|
|
1754
1610
|
#
|
|
1755
|
-
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1756
|
-
#
|
|
1757
1611
|
def to_hash: () -> self
|
|
1758
1612
|
|
|
1759
1613
|
# <!--
|
|
1760
1614
|
# rdoc-file=hash.c
|
|
1761
|
-
# - to_proc -> proc
|
|
1615
|
+
# - hash.to_proc -> proc
|
|
1762
1616
|
# -->
|
|
1763
1617
|
# Returns a Proc object that maps a key to its value:
|
|
1764
|
-
#
|
|
1765
1618
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1766
1619
|
# proc = h.to_proc
|
|
1767
1620
|
# proc.class # => Proc
|
|
@@ -1769,366 +1622,227 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
1769
1622
|
# proc.call(:bar) # => 1
|
|
1770
1623
|
# proc.call(:nosuch) # => nil
|
|
1771
1624
|
#
|
|
1772
|
-
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1773
|
-
#
|
|
1774
1625
|
def to_proc: () -> ^(K) -> V?
|
|
1775
1626
|
|
|
1776
1627
|
# <!-- rdoc-file=hash.c -->
|
|
1777
|
-
# Returns a new
|
|
1628
|
+
# Returns a new String containing the hash entries:
|
|
1778
1629
|
#
|
|
1779
1630
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1780
1631
|
# h.inspect # => "{foo: 0, bar: 1, baz: 2}"
|
|
1781
1632
|
#
|
|
1782
|
-
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1783
|
-
#
|
|
1784
1633
|
alias to_s inspect
|
|
1785
1634
|
|
|
1786
1635
|
# <!--
|
|
1787
1636
|
# rdoc-file=hash.c
|
|
1788
|
-
# - transform_keys {|
|
|
1789
|
-
# - transform_keys(
|
|
1790
|
-
# - transform_keys(
|
|
1791
|
-
# - 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
|
|
1792
1641
|
# -->
|
|
1793
|
-
#
|
|
1794
|
-
#
|
|
1795
|
-
#
|
|
1642
|
+
# Returns a new `Hash` object; each entry has:
|
|
1643
|
+
# * A key provided by the block.
|
|
1644
|
+
# * The value from `self`.
|
|
1796
1645
|
#
|
|
1797
|
-
#
|
|
1798
|
-
# block
|
|
1799
|
-
#
|
|
1800
|
-
# For each key/value pair `old_key/value` in `self`, calls the block with
|
|
1801
|
-
# `old_key`; the block's return value becomes `new_key`; sets `new_hash[new_key]
|
|
1802
|
-
# = 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.
|
|
1803
1649
|
#
|
|
1650
|
+
# Transform keys:
|
|
1804
1651
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1805
|
-
# h.transform_keys {|
|
|
1806
|
-
# # => {"foo"
|
|
1807
|
-
# h.transform_keys {|old_key| 'xxx' }
|
|
1808
|
-
# # => {"xxx" => 2}
|
|
1809
|
-
#
|
|
1810
|
-
# With argument `other_hash` given and no block, `new_hash` may have new keys
|
|
1811
|
-
# provided by `other_hash` and unchanged keys provided by `self`.
|
|
1812
|
-
#
|
|
1813
|
-
# For each key/value pair `old_key/old_value` in `self`, looks for key `old_key`
|
|
1814
|
-
# in `other_hash`:
|
|
1815
|
-
#
|
|
1816
|
-
# * If `old_key` is found, its value `other_hash[old_key]` is taken as
|
|
1817
|
-
# `new_key`; sets `new_hash[new_key] = value`; a duplicate key overwrites:
|
|
1818
|
-
#
|
|
1819
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1820
|
-
# h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO)
|
|
1821
|
-
# # => {FOO: 0, BAR: 1, BAZ: 2}
|
|
1822
|
-
# h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO)
|
|
1823
|
-
# # => {FOO: 2}
|
|
1652
|
+
# h1 = h.transform_keys {|key| key.to_s }
|
|
1653
|
+
# h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
|
|
1824
1654
|
#
|
|
1825
|
-
#
|
|
1826
|
-
#
|
|
1655
|
+
# h.transform_keys(foo: :bar, bar: :foo)
|
|
1656
|
+
# #=> {bar: 0, foo: 1, baz: 2}
|
|
1827
1657
|
#
|
|
1828
|
-
#
|
|
1829
|
-
#
|
|
1830
|
-
# # => {foo: 0, bar: 1, baz: 2}
|
|
1831
|
-
# h.transform_keys(baz: :foo)
|
|
1832
|
-
# # => {foo: 2, bar: 1}
|
|
1833
|
-
#
|
|
1834
|
-
# Unused keys in `other_hash` are ignored:
|
|
1658
|
+
# h.transform_keys(foo: :hello, &:to_s)
|
|
1659
|
+
# #=> {:hello=>0, "bar"=>1, "baz"=>2}
|
|
1835
1660
|
#
|
|
1661
|
+
# Overwrites values for duplicate keys:
|
|
1836
1662
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1837
|
-
# h.transform_keys
|
|
1838
|
-
# # => {
|
|
1839
|
-
#
|
|
1840
|
-
# With both argument `other_hash` and a block given, `new_hash` has new keys
|
|
1841
|
-
# specified by `other_hash` or by the block, and unchanged keys provided by
|
|
1842
|
-
# `self`.
|
|
1843
|
-
#
|
|
1844
|
-
# For each pair `old_key` and `value` in `self`:
|
|
1845
|
-
#
|
|
1846
|
-
# * If `other_hash` has key `old_key` (with value `new_key`), does not call
|
|
1847
|
-
# the block for that key; sets `new_hash[new_key] = value`; a duplicate key
|
|
1848
|
-
# overwrites:
|
|
1663
|
+
# h1 = h.transform_keys {|key| :bat }
|
|
1664
|
+
# h1 # => {:bat=>2}
|
|
1849
1665
|
#
|
|
1850
|
-
#
|
|
1851
|
-
#
|
|
1852
|
-
#
|
|
1853
|
-
#
|
|
1854
|
-
#
|
|
1855
|
-
# `old_key` and takes its return value as `new_key`; sets `new_hash[new_key]
|
|
1856
|
-
# = value`; a duplicate key overwrites:
|
|
1857
|
-
#
|
|
1858
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1859
|
-
# h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse }
|
|
1860
|
-
# # => {"oof" => 0, "rab" => 1, BAZ: 2}
|
|
1861
|
-
# h.transform_keys(baz: :BAZ) {|key| 'ook' }
|
|
1862
|
-
# # => {"ook" => 1, BAZ: 2}
|
|
1863
|
-
#
|
|
1864
|
-
# With no argument and no block given, returns a new Enumerator.
|
|
1865
|
-
#
|
|
1866
|
-
# Related: see [Methods for Transforming Keys and
|
|
1867
|
-
# 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}
|
|
1868
1671
|
#
|
|
1869
1672
|
def transform_keys: () -> Enumerator[K, Hash[untyped, V]]
|
|
1870
1673
|
| [A] () { (K) -> A } -> Hash[A, V]
|
|
1871
1674
|
|
|
1872
1675
|
# <!--
|
|
1873
1676
|
# rdoc-file=hash.c
|
|
1874
|
-
# - transform_keys! {|
|
|
1875
|
-
# - transform_keys!(
|
|
1876
|
-
# - transform_keys!(
|
|
1877
|
-
# - 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
|
|
1878
1681
|
# -->
|
|
1879
|
-
#
|
|
1880
|
-
#
|
|
1881
|
-
#
|
|
1882
|
-
# With a block given and no argument, derives keys only from the block; all,
|
|
1883
|
-
# some, or none of the keys in `self` may be changed.
|
|
1884
|
-
#
|
|
1885
|
-
# For each key/value pair `old_key/value` in `self`, calls the block with
|
|
1886
|
-
# `old_key`; the block's return value becomes `new_key`; removes the entry for
|
|
1887
|
-
# `old_key`: `self.delete(old_key)`; sets `self[new_key] = value`; a duplicate
|
|
1888
|
-
# key overwrites:
|
|
1889
|
-
#
|
|
1890
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1891
|
-
# h.transform_keys! {|old_key| old_key.to_s }
|
|
1892
|
-
# # => {"foo" => 0, "bar" => 1, "baz" => 2}
|
|
1893
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1894
|
-
# h.transform_keys! {|old_key| 'xxx' }
|
|
1895
|
-
# # => {"xxx" => 2}
|
|
1896
|
-
#
|
|
1897
|
-
# With argument `other_hash` given and no block, derives keys for `self` from
|
|
1898
|
-
# `other_hash` and `self`; all, some, or none of the keys in `self` may be
|
|
1899
|
-
# changed.
|
|
1900
|
-
#
|
|
1901
|
-
# For each key/value pair `old_key/old_value` in `self`, looks for key `old_key`
|
|
1902
|
-
# in `other_hash`:
|
|
1903
|
-
#
|
|
1904
|
-
# * If `old_key` is found, takes value `other_hash[old_key]` as `new_key`;
|
|
1905
|
-
# removes the entry for `old_key`: `self.delete(old_key)`; sets
|
|
1906
|
-
# `self[new_key] = value`; a duplicate key overwrites:
|
|
1907
|
-
#
|
|
1908
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1909
|
-
# h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO)
|
|
1910
|
-
# # => {FOO: 0, BAR: 1, BAZ: 2}
|
|
1911
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1912
|
-
# h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO)
|
|
1913
|
-
# # => {FOO: 2}
|
|
1914
|
-
#
|
|
1915
|
-
# * If `old_key` is not found, does nothing:
|
|
1916
|
-
#
|
|
1917
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1918
|
-
# h.transform_keys!({})
|
|
1919
|
-
# # => {foo: 0, bar: 1, baz: 2}
|
|
1920
|
-
# h.transform_keys!(baz: :foo)
|
|
1921
|
-
# # => {foo: 2, bar: 1}
|
|
1922
|
-
#
|
|
1923
|
-
# Unused keys in `other_hash` are ignored:
|
|
1924
|
-
#
|
|
1925
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1926
|
-
# h.transform_keys!(bat: 3)
|
|
1927
|
-
# # => {foo: 0, bar: 1, baz: 2}
|
|
1928
|
-
#
|
|
1929
|
-
# With both argument `other_hash` and a block given, derives keys from
|
|
1930
|
-
# `other_hash`, the block, and `self`; all, some, or none of the keys in `self`
|
|
1931
|
-
# may be changed.
|
|
1932
|
-
#
|
|
1933
|
-
# For each pair `old_key` and `value` in `self`:
|
|
1934
|
-
#
|
|
1935
|
-
# * If `other_hash` has key `old_key` (with value `new_key`), does not call
|
|
1936
|
-
# the block for that key; removes the entry for `old_key`:
|
|
1937
|
-
# `self.delete(old_key)`; sets `self[new_key] = value`; a duplicate key
|
|
1938
|
-
# overwrites:
|
|
1939
|
-
#
|
|
1940
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1941
|
-
# h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' }
|
|
1942
|
-
# # => {FOO: 0, BAR: 1, BAZ: 2}
|
|
1943
|
-
#
|
|
1944
|
-
# * If `other_hash` does not have key `old_key`, calls the block with
|
|
1945
|
-
# `old_key` and takes its return value as `new_key`; removes the entry for
|
|
1946
|
-
# `old_key`: `self.delete(old_key)`; sets `self[new_key] = value`; a
|
|
1947
|
-
# duplicate key overwrites:
|
|
1948
|
-
#
|
|
1949
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1950
|
-
# h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse }
|
|
1951
|
-
# # => {"oof" => 0, "rab" => 1, BAZ: 2}
|
|
1952
|
-
# h = {foo: 0, bar: 1, baz: 2}
|
|
1953
|
-
# h.transform_keys!(baz: :BAZ) {|key| 'ook' }
|
|
1954
|
-
# # => {"ook" => 1, BAZ: 2}
|
|
1955
|
-
#
|
|
1956
|
-
# With no argument and no block given, returns a new Enumerator.
|
|
1957
|
-
#
|
|
1958
|
-
# Related: see [Methods for Transforming Keys and
|
|
1959
|
-
# 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.
|
|
1960
1684
|
#
|
|
1961
1685
|
def transform_keys!: () -> Enumerator[K, self]
|
|
1962
1686
|
| () { (K) -> K } -> self
|
|
1963
1687
|
|
|
1964
1688
|
# <!--
|
|
1965
1689
|
# rdoc-file=hash.c
|
|
1966
|
-
# - transform_values {|value| ... } -> new_hash
|
|
1967
|
-
# - transform_values -> new_enumerator
|
|
1690
|
+
# - hash.transform_values {|value| ... } -> new_hash
|
|
1691
|
+
# - hash.transform_values -> new_enumerator
|
|
1968
1692
|
# -->
|
|
1969
|
-
#
|
|
1970
|
-
#
|
|
1971
|
-
#
|
|
1693
|
+
# Returns a new `Hash` object; each entry has:
|
|
1694
|
+
# * A key from `self`.
|
|
1695
|
+
# * A value provided by the block.
|
|
1972
1696
|
#
|
|
1697
|
+
# Transform values:
|
|
1973
1698
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1974
1699
|
# h1 = h.transform_values {|value| value * 100}
|
|
1975
|
-
# h1 # => {foo
|
|
1976
|
-
#
|
|
1977
|
-
# With no block given, returns a new Enumerator.
|
|
1700
|
+
# h1 # => {:foo=>0, :bar=>100, :baz=>200}
|
|
1978
1701
|
#
|
|
1979
|
-
#
|
|
1980
|
-
#
|
|
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}
|
|
1981
1707
|
#
|
|
1982
1708
|
def transform_values: () -> Enumerator[V, Hash[K, untyped]]
|
|
1983
1709
|
| [A] () { (V) -> A } -> Hash[K, A]
|
|
1984
1710
|
|
|
1985
1711
|
# <!--
|
|
1986
1712
|
# rdoc-file=hash.c
|
|
1987
|
-
# - transform_values! {|
|
|
1988
|
-
# - transform_values! -> new_enumerator
|
|
1713
|
+
# - hash.transform_values! {|value| ... } -> self
|
|
1714
|
+
# - hash.transform_values! -> new_enumerator
|
|
1989
1715
|
# -->
|
|
1990
|
-
#
|
|
1991
|
-
#
|
|
1992
|
-
#
|
|
1993
|
-
# For each entry `key`/`old_value` in `self`, calls the block with `old_value`,
|
|
1994
|
-
# captures its return value as `new_value`, and sets `self[key] = new_value`:
|
|
1995
|
-
#
|
|
1716
|
+
# Returns `self`, whose keys are unchanged, and whose values are determined by
|
|
1717
|
+
# the given block.
|
|
1996
1718
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
1997
|
-
# h.transform_values! {|value| value * 100} # => {foo
|
|
1998
|
-
#
|
|
1999
|
-
# With no block given, returns a new Enumerator.
|
|
1719
|
+
# h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
|
|
2000
1720
|
#
|
|
2001
|
-
#
|
|
2002
|
-
#
|
|
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}
|
|
2003
1726
|
#
|
|
2004
1727
|
def transform_values!: () -> Enumerator[V, self]
|
|
2005
1728
|
| () { (V) -> V } -> self
|
|
2006
1729
|
|
|
2007
1730
|
# <!--
|
|
2008
1731
|
# rdoc-file=hash.c
|
|
2009
|
-
# -
|
|
2010
|
-
# -
|
|
1732
|
+
# - hash.merge! -> self
|
|
1733
|
+
# - hash.merge!(*other_hashes) -> self
|
|
1734
|
+
# - hash.merge!(*other_hashes) { |key, old_value, new_value| ... } -> self
|
|
2011
1735
|
# -->
|
|
2012
|
-
#
|
|
2013
|
-
#
|
|
2014
|
-
# Each argument `other_hash` in `other_hashes` must be a hash.
|
|
2015
|
-
#
|
|
2016
|
-
# With no block given, for each successive entry `key`/`new_value` in each
|
|
2017
|
-
# successive `other_hash`:
|
|
1736
|
+
# Merges each of `other_hashes` into `self`; returns `self`.
|
|
2018
1737
|
#
|
|
2019
|
-
#
|
|
2020
|
-
# unchanged:
|
|
1738
|
+
# Each argument in `other_hashes` must be a `Hash`.
|
|
2021
1739
|
#
|
|
2022
|
-
#
|
|
2023
|
-
#
|
|
2024
|
-
#
|
|
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.
|
|
2025
1745
|
#
|
|
2026
|
-
#
|
|
2027
|
-
#
|
|
2028
|
-
#
|
|
2029
|
-
#
|
|
2030
|
-
#
|
|
2031
|
-
# With a block given, for each successive entry `key`/`new_value` in each
|
|
2032
|
-
# successive `other_hash`:
|
|
2033
|
-
#
|
|
2034
|
-
# * If `key` is in `self`, fetches `old_value` from `self[key]`, calls the
|
|
2035
|
-
# block with `key`, `old_value`, and `new_value`, and sets `self[key] =
|
|
2036
|
-
# 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}
|
|
2037
1751
|
#
|
|
2038
|
-
#
|
|
2039
|
-
#
|
|
2040
|
-
#
|
|
2041
|
-
#
|
|
2042
|
-
#
|
|
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.
|
|
2043
1759
|
#
|
|
2044
|
-
#
|
|
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}
|
|
2045
1766
|
#
|
|
2046
|
-
#
|
|
2047
|
-
#
|
|
2048
|
-
#
|
|
1767
|
+
# With no arguments:
|
|
1768
|
+
# * Returns `self`, unmodified.
|
|
1769
|
+
# * The block, if given, is ignored.
|
|
2049
1770
|
#
|
|
2050
|
-
#
|
|
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}
|
|
2051
1776
|
#
|
|
2052
1777
|
alias update merge!
|
|
2053
1778
|
|
|
2054
1779
|
# <!-- rdoc-file=hash.c -->
|
|
2055
|
-
# Returns
|
|
2056
|
-
#
|
|
2057
|
-
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1780
|
+
# Returns `true` if `value` is a value in `self`, otherwise `false`.
|
|
2058
1781
|
#
|
|
2059
1782
|
alias value? has_value?
|
|
2060
1783
|
|
|
2061
1784
|
# <!--
|
|
2062
1785
|
# rdoc-file=hash.c
|
|
2063
|
-
# - values -> new_array
|
|
1786
|
+
# - hash.values -> new_array
|
|
2064
1787
|
# -->
|
|
2065
|
-
# Returns a new
|
|
2066
|
-
#
|
|
1788
|
+
# Returns a new Array containing all values in `self`:
|
|
2067
1789
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
2068
1790
|
# h.values # => [0, 1, 2]
|
|
2069
1791
|
#
|
|
2070
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
2071
|
-
#
|
|
2072
1792
|
def values: () -> ::Array[V]
|
|
2073
1793
|
|
|
2074
1794
|
# <!--
|
|
2075
1795
|
# rdoc-file=hash.c
|
|
2076
|
-
# - values_at(*keys) -> new_array
|
|
1796
|
+
# - hash.values_at(*keys) -> new_array
|
|
2077
1797
|
# -->
|
|
2078
|
-
# Returns a new
|
|
2079
|
-
#
|
|
1798
|
+
# Returns a new Array containing values for the given `keys`:
|
|
2080
1799
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
2081
1800
|
# h.values_at(:baz, :foo) # => [2, 0]
|
|
2082
1801
|
#
|
|
2083
|
-
# The [
|
|
2084
|
-
#
|
|
2085
|
-
#
|
|
1802
|
+
# The [default values](rdoc-ref:Hash@Default+Values) are returned for any keys
|
|
1803
|
+
# that are not found:
|
|
2086
1804
|
# h.values_at(:hello, :foo) # => [nil, 0]
|
|
2087
1805
|
#
|
|
2088
|
-
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
2089
|
-
#
|
|
2090
1806
|
def values_at: (*K arg0) -> ::Array[V?]
|
|
2091
1807
|
|
|
2092
1808
|
private
|
|
2093
1809
|
|
|
2094
1810
|
# <!--
|
|
2095
1811
|
# rdoc-file=hash.rb
|
|
2096
|
-
# - Hash.new(default_value = nil
|
|
2097
|
-
# - 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
|
|
2098
1816
|
# -->
|
|
2099
|
-
# Returns a new empty Hash object.
|
|
2100
|
-
#
|
|
2101
|
-
# Initializes the values of Hash#default and Hash#default_proc, which determine
|
|
2102
|
-
# the behavior when a given key is not found; see [Key Not
|
|
2103
|
-
# Found?](rdoc-ref:Hash@Key+Not+Found-3F).
|
|
1817
|
+
# Returns a new empty `Hash` object.
|
|
2104
1818
|
#
|
|
2105
|
-
#
|
|
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).
|
|
2106
1821
|
#
|
|
2107
|
-
#
|
|
2108
|
-
#
|
|
2109
|
-
# h
|
|
2110
|
-
#
|
|
2111
|
-
#
|
|
2112
|
-
#
|
|
2113
|
-
# h = Hash.new(false) # => {}
|
|
2114
|
-
# h.default # => false
|
|
2115
|
-
# h.default_proc # => nil
|
|
2116
|
-
#
|
|
2117
|
-
# 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
|
|
2118
1827
|
#
|
|
2119
|
-
#
|
|
2120
|
-
#
|
|
2121
|
-
# h
|
|
2122
|
-
# 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
|
|
2123
1833
|
#
|
|
2124
|
-
#
|
|
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"
|
|
2125
1840
|
#
|
|
2126
|
-
# If
|
|
2127
|
-
# `n`, initializes the hash with enough capacity to accommodate `n` entries
|
|
2128
|
-
# without resizing.
|
|
1841
|
+
# If both a block and a `default_value` are given, raises an `ArgumentError`
|
|
2129
1842
|
#
|
|
2130
|
-
#
|
|
2131
|
-
#
|
|
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.
|
|
2132
1846
|
#
|
|
2133
1847
|
def initialize: (?capacity: int) -> void
|
|
2134
1848
|
| (V default, ?capacity: int) -> void
|
|
@@ -2136,23 +1850,12 @@ class Hash[unchecked out K, unchecked out V] < Object
|
|
|
2136
1850
|
|
|
2137
1851
|
# <!--
|
|
2138
1852
|
# rdoc-file=hash.c
|
|
2139
|
-
# - replace(other_hash) -> self
|
|
1853
|
+
# - hash.replace(other_hash) -> self
|
|
2140
1854
|
# -->
|
|
2141
1855
|
# Replaces the entire contents of `self` with the contents of `other_hash`;
|
|
2142
1856
|
# returns `self`:
|
|
2143
|
-
#
|
|
2144
1857
|
# h = {foo: 0, bar: 1, baz: 2}
|
|
2145
|
-
# h.replace({bat: 3, bam: 4}) # => {bat
|
|
2146
|
-
#
|
|
2147
|
-
# Also replaces the default value or proc of `self` with the default value or
|
|
2148
|
-
# proc of `other_hash`.
|
|
2149
|
-
#
|
|
2150
|
-
# h = {}
|
|
2151
|
-
# other = Hash.new(:ok)
|
|
2152
|
-
# h.replace(other)
|
|
2153
|
-
# h.default # => :ok
|
|
2154
|
-
#
|
|
2155
|
-
# Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
|
|
1858
|
+
# h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
|
|
2156
1859
|
#
|
|
2157
1860
|
def initialize_copy: (self object) -> self
|
|
2158
1861
|
end
|