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