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