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