rbs 4.1.0.pre.2-java
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 +7 -0
- data/.clang-format +75 -0
- data/.clangd +2 -0
- data/.github/dependabot.yml +24 -0
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +61 -0
- data/.github/workflows/comments.yml +37 -0
- data/.github/workflows/dependabot.yml +30 -0
- data/.github/workflows/jruby.yml +67 -0
- data/.github/workflows/milestone.yml +83 -0
- data/.github/workflows/ruby.yml +158 -0
- data/.github/workflows/rust.yml +184 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +39 -0
- data/.github/workflows/wasm.yml +53 -0
- data/.github/workflows/windows.yml +49 -0
- data/.gitignore +38 -0
- data/.rubocop.yml +72 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +2292 -0
- data/COPYING +56 -0
- data/README.md +240 -0
- data/Rakefile +869 -0
- data/Steepfile +53 -0
- data/config.yml +913 -0
- data/core/array.rbs +4142 -0
- data/core/basic_object.rbs +376 -0
- data/core/binding.rbs +148 -0
- data/core/builtin.rbs +278 -0
- data/core/class.rbs +223 -0
- data/core/comparable.rbs +192 -0
- data/core/complex.rbs +812 -0
- data/core/constants.rbs +96 -0
- data/core/data.rbs +415 -0
- data/core/dir.rbs +993 -0
- data/core/encoding.rbs +1368 -0
- data/core/enumerable.rbs +2506 -0
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator/product.rbs +92 -0
- data/core/enumerator.rbs +705 -0
- data/core/env.rbs +6 -0
- data/core/errno.rbs +682 -0
- data/core/errors.rbs +789 -0
- data/core/exception.rbs +485 -0
- data/core/false_class.rbs +82 -0
- data/core/fiber.rbs +570 -0
- data/core/fiber_error.rbs +11 -0
- data/core/file.rbs +2045 -0
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/file_test.rbs +331 -0
- data/core/float.rbs +1316 -0
- data/core/gc.rbs +788 -0
- data/core/global_variables.rbs +184 -0
- data/core/hash.rbs +2183 -0
- data/core/integer.rbs +1374 -0
- data/core/io/buffer.rbs +995 -0
- data/core/io/wait.rbs +48 -0
- data/core/io.rbs +3472 -0
- data/core/kernel.rbs +3172 -0
- data/core/marshal.rbs +207 -0
- data/core/match_data.rbs +637 -0
- data/core/math.rbs +770 -0
- data/core/method.rbs +422 -0
- data/core/module.rbs +1856 -0
- data/core/nil_class.rbs +210 -0
- data/core/numeric.rbs +832 -0
- data/core/object.rbs +108 -0
- data/core/object_space/weak_key_map.rbs +166 -0
- data/core/object_space.rbs +197 -0
- data/core/pathname.rbs +1322 -0
- data/core/proc.rbs +905 -0
- data/core/process.rbs +2316 -0
- data/core/ractor.rbs +924 -0
- data/core/random.rbs +255 -0
- data/core/range.rbs +1209 -0
- data/core/rational.rbs +502 -0
- data/core/rb_config.rbs +88 -0
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +1236 -0
- data/core/rbs/unnamed/env_class.rbs +1214 -0
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +186 -0
- data/core/refinement.rbs +59 -0
- data/core/regexp.rbs +1974 -0
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +809 -0
- data/core/rubygems/basic_specification.rbs +6 -0
- data/core/rubygems/config_file.rbs +38 -0
- data/core/rubygems/dependency_installer.rbs +6 -0
- data/core/rubygems/errors.rbs +109 -0
- data/core/rubygems/installer.rbs +15 -0
- data/core/rubygems/path_support.rbs +6 -0
- data/core/rubygems/platform.rbs +7 -0
- data/core/rubygems/request_set.rbs +49 -0
- data/core/rubygems/requirement.rbs +148 -0
- data/core/rubygems/rubygems.rbs +1105 -0
- data/core/rubygems/source_list.rbs +15 -0
- data/core/rubygems/specification.rbs +23 -0
- data/core/rubygems/stream_ui.rbs +5 -0
- data/core/rubygems/uninstaller.rbs +10 -0
- data/core/rubygems/version.rbs +293 -0
- data/core/set.rbs +751 -0
- data/core/signal.rbs +110 -0
- data/core/string.rbs +5532 -0
- data/core/struct.rbs +668 -0
- data/core/symbol.rbs +482 -0
- data/core/thread.rbs +1826 -0
- data/core/thread_group.rbs +79 -0
- data/core/time.rbs +1793 -0
- data/core/trace_point.rbs +483 -0
- data/core/true_class.rbs +98 -0
- data/core/unbound_method.rbs +337 -0
- data/core/warning.rbs +87 -0
- data/docs/CONTRIBUTING.md +107 -0
- data/docs/aliases.md +79 -0
- data/docs/architecture.md +110 -0
- data/docs/collection.md +192 -0
- data/docs/config.md +171 -0
- data/docs/data_and_struct.md +86 -0
- data/docs/encoding.md +56 -0
- data/docs/gem.md +56 -0
- data/docs/inline.md +634 -0
- data/docs/rbs_by_example.md +309 -0
- data/docs/repo.md +125 -0
- data/docs/rust.md +96 -0
- data/docs/sigs.md +167 -0
- data/docs/stdlib.md +147 -0
- data/docs/syntax.md +940 -0
- data/docs/tools.md +17 -0
- data/docs/type_fingerprint.md +21 -0
- data/docs/wasm_serialization.md +80 -0
- data/exe/rbs +7 -0
- data/ext/rbs_extension/ast_translation.c +1855 -0
- data/ext/rbs_extension/ast_translation.h +41 -0
- data/ext/rbs_extension/class_constants.c +187 -0
- data/ext/rbs_extension/class_constants.h +104 -0
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +40 -0
- data/ext/rbs_extension/legacy_location.c +294 -0
- data/ext/rbs_extension/legacy_location.h +82 -0
- data/ext/rbs_extension/main.c +613 -0
- data/ext/rbs_extension/rbs_extension.h +16 -0
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/goodcheck.yml +91 -0
- data/include/rbs/ast.h +1047 -0
- data/include/rbs/defines.h +99 -0
- data/include/rbs/lexer.h +207 -0
- data/include/rbs/location.h +40 -0
- data/include/rbs/parser.h +153 -0
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +155 -0
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +14 -0
- data/lib/rbs/ancestor_graph.rb +92 -0
- data/lib/rbs/annotate/annotations.rb +199 -0
- data/lib/rbs/annotate/formatter.rb +82 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +131 -0
- data/lib/rbs/annotate.rb +8 -0
- data/lib/rbs/ast/annotation.rb +29 -0
- data/lib/rbs/ast/comment.rb +29 -0
- data/lib/rbs/ast/declarations.rb +472 -0
- data/lib/rbs/ast/directives.rb +49 -0
- data/lib/rbs/ast/members.rb +451 -0
- data/lib/rbs/ast/ruby/annotations.rb +451 -0
- data/lib/rbs/ast/ruby/comment_block.rb +247 -0
- data/lib/rbs/ast/ruby/declarations.rb +291 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +762 -0
- data/lib/rbs/ast/type_param.rb +235 -0
- data/lib/rbs/ast/visitor.rb +137 -0
- data/lib/rbs/buffer.rb +189 -0
- data/lib/rbs/builtin_names.rb +58 -0
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +84 -0
- data/lib/rbs/cli/validate.rb +294 -0
- data/lib/rbs/cli.rb +1253 -0
- data/lib/rbs/collection/cleaner.rb +38 -0
- data/lib/rbs/collection/config/lockfile.rb +92 -0
- data/lib/rbs/collection/config/lockfile_generator.rb +269 -0
- data/lib/rbs/collection/config.rb +81 -0
- data/lib/rbs/collection/installer.rb +32 -0
- data/lib/rbs/collection/sources/base.rb +14 -0
- data/lib/rbs/collection/sources/git.rb +265 -0
- data/lib/rbs/collection/sources/local.rb +81 -0
- data/lib/rbs/collection/sources/rubygems.rb +48 -0
- data/lib/rbs/collection/sources/stdlib.rb +50 -0
- data/lib/rbs/collection/sources.rb +38 -0
- data/lib/rbs/collection.rb +16 -0
- data/lib/rbs/constant.rb +28 -0
- data/lib/rbs/definition.rb +415 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +678 -0
- data/lib/rbs/definition_builder/method_builder.rb +295 -0
- data/lib/rbs/definition_builder.rb +1054 -0
- data/lib/rbs/diff.rb +131 -0
- data/lib/rbs/environment/class_entry.rb +69 -0
- data/lib/rbs/environment/module_entry.rb +66 -0
- data/lib/rbs/environment/use_map.rb +77 -0
- data/lib/rbs/environment.rb +1028 -0
- data/lib/rbs/environment_loader.rb +167 -0
- data/lib/rbs/environment_walker.rb +155 -0
- data/lib/rbs/errors.rb +634 -0
- data/lib/rbs/factory.rb +18 -0
- data/lib/rbs/file_finder.rb +28 -0
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +568 -0
- data/lib/rbs/location_aux.rb +170 -0
- data/lib/rbs/locator.rb +247 -0
- data/lib/rbs/method_type.rb +145 -0
- data/lib/rbs/namespace.rb +154 -0
- data/lib/rbs/parser/lex_result.rb +15 -0
- data/lib/rbs/parser/token.rb +23 -0
- data/lib/rbs/parser_aux.rb +142 -0
- data/lib/rbs/prototype/helpers.rb +197 -0
- data/lib/rbs/prototype/node_usage.rb +99 -0
- data/lib/rbs/prototype/rb.rb +816 -0
- data/lib/rbs/prototype/rbi.rb +625 -0
- data/lib/rbs/prototype/runtime/helpers.rb +59 -0
- data/lib/rbs/prototype/runtime/reflection.rb +19 -0
- data/lib/rbs/prototype/runtime/value_object_generator.rb +279 -0
- data/lib/rbs/prototype/runtime.rb +680 -0
- data/lib/rbs/repository.rb +127 -0
- data/lib/rbs/resolver/constant_resolver.rb +219 -0
- data/lib/rbs/resolver/type_name_resolver.rb +167 -0
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/sorter.rb +198 -0
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/substitution.rb +83 -0
- data/lib/rbs/subtractor.rb +204 -0
- data/lib/rbs/test/errors.rb +80 -0
- data/lib/rbs/test/guaranteed.rb +30 -0
- data/lib/rbs/test/hook.rb +212 -0
- data/lib/rbs/test/observer.rb +19 -0
- data/lib/rbs/test/setup.rb +84 -0
- data/lib/rbs/test/setup_helper.rb +50 -0
- data/lib/rbs/test/tester.rb +167 -0
- data/lib/rbs/test/type_check.rb +457 -0
- data/lib/rbs/test.rb +112 -0
- data/lib/rbs/type_alias_dependency.rb +100 -0
- data/lib/rbs/type_alias_regularity.rb +126 -0
- data/lib/rbs/type_name.rb +122 -0
- data/lib/rbs/types.rb +1604 -0
- data/lib/rbs/unit_test/convertibles.rb +177 -0
- data/lib/rbs/unit_test/spy.rb +138 -0
- data/lib/rbs/unit_test/type_assertions.rb +383 -0
- data/lib/rbs/unit_test/with_aliases.rb +145 -0
- data/lib/rbs/unit_test.rb +6 -0
- data/lib/rbs/validator.rb +186 -0
- data/lib/rbs/variance_calculator.rb +189 -0
- data/lib/rbs/vendorer.rb +71 -0
- data/lib/rbs/version.rb +5 -0
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/jars/asm-analysis.jar +0 -0
- data/lib/rbs/wasm/jars/asm-commons.jar +0 -0
- data/lib/rbs/wasm/jars/asm-tree.jar +0 -0
- data/lib/rbs/wasm/jars/asm-util.jar +0 -0
- data/lib/rbs/wasm/jars/asm.jar +0 -0
- data/lib/rbs/wasm/jars/compiler.jar +0 -0
- data/lib/rbs/wasm/jars/log.jar +0 -0
- data/lib/rbs/wasm/jars/runtime.jar +0 -0
- data/lib/rbs/wasm/jars/wasi.jar +0 -0
- data/lib/rbs/wasm/jars/wasm.jar +0 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/rbs_parser.wasm +0 -0
- data/lib/rbs/wasm/runtime.rb +217 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs/writer.rb +424 -0
- data/lib/rbs.rb +117 -0
- data/lib/rdoc/discover.rb +20 -0
- data/lib/rdoc_plugin/parser.rb +163 -0
- data/rbs.gemspec +68 -0
- data/schema/annotation.json +14 -0
- data/schema/comment.json +26 -0
- data/schema/decls.json +326 -0
- data/schema/function.json +87 -0
- data/schema/location.json +56 -0
- data/schema/members.json +266 -0
- data/schema/methodType.json +50 -0
- data/schema/typeParam.json +52 -0
- data/schema/types.json +317 -0
- data/sig/ancestor_builder.rbs +163 -0
- data/sig/ancestor_graph.rbs +60 -0
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +85 -0
- data/sig/annotate/rdoc_source.rbs +32 -0
- data/sig/annotation.rbs +27 -0
- data/sig/ast/ruby/annotations.rbs +470 -0
- data/sig/ast/ruby/comment_block.rbs +127 -0
- data/sig/ast/ruby/declarations.rbs +158 -0
- data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
- data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
- data/sig/ast/ruby/members.rbs +198 -0
- data/sig/buffer.rbs +108 -0
- data/sig/builtin_names.rbs +44 -0
- data/sig/cli/colored_io.rbs +15 -0
- data/sig/cli/diff.rbs +15 -0
- data/sig/cli/validate.rbs +47 -0
- data/sig/cli.rbs +89 -0
- data/sig/collection/cleaner.rbs +13 -0
- data/sig/collection/config/lockfile.rbs +74 -0
- data/sig/collection/config/lockfile_generator.rbs +68 -0
- data/sig/collection/config.rbs +46 -0
- data/sig/collection/installer.rbs +17 -0
- data/sig/collection/sources.rbs +214 -0
- data/sig/collection.rbs +4 -0
- data/sig/comment.rbs +26 -0
- data/sig/constant.rbs +21 -0
- data/sig/declarations.rbs +274 -0
- data/sig/definition.rbs +232 -0
- data/sig/definition_builder.rbs +181 -0
- data/sig/diff.rbs +28 -0
- data/sig/directives.rbs +77 -0
- data/sig/environment/class_entry.rbs +50 -0
- data/sig/environment/module_entry.rbs +50 -0
- data/sig/environment.rbs +286 -0
- data/sig/environment_loader.rbs +111 -0
- data/sig/environment_walker.rbs +65 -0
- data/sig/errors.rbs +408 -0
- data/sig/factory.rbs +5 -0
- data/sig/file_finder.rbs +28 -0
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +126 -0
- data/sig/location.rbs +135 -0
- data/sig/locator.rbs +56 -0
- data/sig/manifest.yaml +5 -0
- data/sig/members.rbs +258 -0
- data/sig/method_builder.rbs +89 -0
- data/sig/method_types.rbs +58 -0
- data/sig/namespace.rbs +161 -0
- data/sig/parser.rbs +164 -0
- data/sig/prototype/helpers.rbs +29 -0
- data/sig/prototype/node_usage.rbs +20 -0
- data/sig/prototype/rb.rbs +96 -0
- data/sig/prototype/rbi.rbs +75 -0
- data/sig/prototype/runtime.rbs +182 -0
- data/sig/rbs.rbs +21 -0
- data/sig/rdoc/rbs.rbs +67 -0
- data/sig/repository.rbs +85 -0
- data/sig/resolver/constant_resolver.rbs +92 -0
- data/sig/resolver/context.rbs +34 -0
- data/sig/resolver/type_name_resolver.rbs +61 -0
- data/sig/rewriter.rbs +45 -0
- data/sig/shims/bundler.rbs +38 -0
- data/sig/shims/enumerable.rbs +5 -0
- data/sig/shims/rubygems.rbs +19 -0
- data/sig/sorter.rbs +41 -0
- data/sig/source.rbs +48 -0
- data/sig/substitution.rbs +48 -0
- data/sig/subtractor.rbs +37 -0
- data/sig/test/errors.rbs +52 -0
- data/sig/test/guranteed.rbs +9 -0
- data/sig/test/type_check.rbs +19 -0
- data/sig/test.rbs +82 -0
- data/sig/type_alias_dependency.rbs +53 -0
- data/sig/type_alias_regularity.rbs +98 -0
- data/sig/type_param.rbs +115 -0
- data/sig/typename.rbs +89 -0
- data/sig/types.rbs +578 -0
- data/sig/unit_test/convertibles.rbs +154 -0
- data/sig/unit_test/spy.rbs +22 -0
- data/sig/unit_test/type_assertions.rbs +211 -0
- data/sig/unit_test/with_aliases.rbs +136 -0
- data/sig/use_map.rbs +35 -0
- data/sig/util.rbs +9 -0
- data/sig/validator.rbs +63 -0
- data/sig/variance_calculator.rbs +87 -0
- data/sig/vendorer.rbs +51 -0
- data/sig/version.rbs +3 -0
- data/sig/visitor.rbs +47 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/sig/writer.rbs +127 -0
- data/src/ast.c +1628 -0
- data/src/lexer.c +3217 -0
- data/src/lexer.re +155 -0
- data/src/lexstate.c +217 -0
- data/src/location.c +31 -0
- data/src/parser.c +4255 -0
- data/src/serialize.c +958 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +162 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +268 -0
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +167 -0
- data/stdlib/abbrev/0/abbrev.rbs +66 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +355 -0
- data/stdlib/benchmark/0/benchmark.rbs +452 -0
- data/stdlib/bigdecimal/0/big_decimal.rbs +1647 -0
- data/stdlib/bigdecimal-math/0/big_math.rbs +280 -0
- data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
- data/stdlib/cgi/0/core.rbs +911 -0
- data/stdlib/cgi/0/manifest.yaml +4 -0
- data/stdlib/cgi-escape/0/escape.rbs +171 -0
- data/stdlib/coverage/0/coverage.rbs +266 -0
- data/stdlib/csv/0/csv.rbs +3776 -0
- data/stdlib/csv/0/manifest.yaml +3 -0
- data/stdlib/date/0/date.rbs +1598 -0
- data/stdlib/date/0/date_time.rbs +617 -0
- data/stdlib/date/0/time.rbs +26 -0
- data/stdlib/dbm/0/dbm.rbs +421 -0
- data/stdlib/delegate/0/delegator.rbs +187 -0
- data/stdlib/delegate/0/kernel.rbs +47 -0
- data/stdlib/delegate/0/simple_delegator.rbs +96 -0
- data/stdlib/did_you_mean/0/did_you_mean.rbs +344 -0
- data/stdlib/digest/0/digest.rbs +687 -0
- data/stdlib/erb/0/erb.rbs +933 -0
- data/stdlib/etc/0/etc.rbs +884 -0
- data/stdlib/fileutils/0/fileutils.rbs +1753 -0
- data/stdlib/find/0/find.rbs +49 -0
- data/stdlib/forwardable/0/forwardable.rbs +271 -0
- data/stdlib/io-console/0/io-console.rbs +414 -0
- data/stdlib/ipaddr/0/ipaddr.rbs +436 -0
- data/stdlib/json/0/json.rbs +1963 -0
- data/stdlib/kconv/0/kconv.rbs +166 -0
- data/stdlib/logger/0/formatter.rbs +45 -0
- data/stdlib/logger/0/log_device.rbs +100 -0
- data/stdlib/logger/0/logger.rbs +796 -0
- data/stdlib/logger/0/manifest.yaml +2 -0
- data/stdlib/logger/0/period.rbs +17 -0
- data/stdlib/logger/0/severity.rbs +34 -0
- data/stdlib/minitest/0/kernel.rbs +42 -0
- data/stdlib/minitest/0/minitest/abstract_reporter.rbs +52 -0
- data/stdlib/minitest/0/minitest/assertion.rbs +17 -0
- data/stdlib/minitest/0/minitest/assertions.rbs +590 -0
- data/stdlib/minitest/0/minitest/backtrace_filter.rbs +23 -0
- data/stdlib/minitest/0/minitest/bench_spec.rbs +102 -0
- data/stdlib/minitest/0/minitest/benchmark.rbs +259 -0
- data/stdlib/minitest/0/minitest/composite_reporter.rbs +25 -0
- data/stdlib/minitest/0/minitest/compress.rbs +13 -0
- data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
- data/stdlib/minitest/0/minitest/expectation.rbs +2 -0
- data/stdlib/minitest/0/minitest/expectations.rbs +21 -0
- data/stdlib/minitest/0/minitest/guard.rbs +64 -0
- data/stdlib/minitest/0/minitest/mock.rbs +64 -0
- data/stdlib/minitest/0/minitest/parallel/executor.rbs +46 -0
- data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +5 -0
- data/stdlib/minitest/0/minitest/parallel/test.rbs +3 -0
- data/stdlib/minitest/0/minitest/parallel.rbs +2 -0
- data/stdlib/minitest/0/minitest/pride_io.rbs +62 -0
- data/stdlib/minitest/0/minitest/pride_lol.rbs +19 -0
- data/stdlib/minitest/0/minitest/progress_reporter.rbs +11 -0
- data/stdlib/minitest/0/minitest/reportable.rbs +53 -0
- data/stdlib/minitest/0/minitest/reporter.rbs +5 -0
- data/stdlib/minitest/0/minitest/result.rbs +28 -0
- data/stdlib/minitest/0/minitest/runnable.rbs +163 -0
- data/stdlib/minitest/0/minitest/skip.rbs +6 -0
- data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +48 -0
- data/stdlib/minitest/0/minitest/spec/dsl.rbs +129 -0
- data/stdlib/minitest/0/minitest/spec.rbs +11 -0
- data/stdlib/minitest/0/minitest/statistics_reporter.rbs +81 -0
- data/stdlib/minitest/0/minitest/summary_reporter.rbs +18 -0
- data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +92 -0
- data/stdlib/minitest/0/minitest/test.rbs +69 -0
- data/stdlib/minitest/0/minitest/unexpected_error.rbs +12 -0
- data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
- data/stdlib/minitest/0/minitest/unit/test_case.rbs +3 -0
- data/stdlib/minitest/0/minitest/unit.rbs +4 -0
- data/stdlib/minitest/0/minitest.rbs +115 -0
- data/stdlib/monitor/0/monitor.rbs +363 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +104 -0
- data/stdlib/net-http/0/manifest.yaml +3 -0
- data/stdlib/net-http/0/net-http.rbs +5580 -0
- data/stdlib/net-protocol/0/manifest.yaml +2 -0
- data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
- data/stdlib/net-smtp/0/manifest.yaml +2 -0
- data/stdlib/net-smtp/0/net-smtp.rbs +55 -0
- data/stdlib/nkf/0/nkf.rbs +402 -0
- data/stdlib/objspace/0/objspace.rbs +470 -0
- data/stdlib/observable/0/observable.rbs +217 -0
- data/stdlib/open-uri/0/manifest.yaml +4 -0
- data/stdlib/open-uri/0/open-uri.rbs +433 -0
- data/stdlib/open3/0/open3.rbs +606 -0
- data/stdlib/openssl/0/manifest.yaml +3 -0
- data/stdlib/openssl/0/openssl.rbs +12231 -0
- data/stdlib/optparse/0/optparse.rbs +1734 -0
- data/stdlib/pathname/0/pathname.rbs +36 -0
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +301 -0
- data/stdlib/prettyprint/0/prettyprint.rbs +383 -0
- data/stdlib/pstore/0/pstore.rbs +608 -0
- data/stdlib/psych/0/core_ext.rbs +12 -0
- data/stdlib/psych/0/dbm.rbs +237 -0
- data/stdlib/psych/0/manifest.yaml +3 -0
- data/stdlib/psych/0/psych.rbs +455 -0
- data/stdlib/psych/0/store.rbs +57 -0
- data/stdlib/pty/0/pty.rbs +240 -0
- data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
- data/stdlib/rdoc/0/code_object.rbs +52 -0
- data/stdlib/rdoc/0/comment.rbs +61 -0
- data/stdlib/rdoc/0/context.rbs +153 -0
- data/stdlib/rdoc/0/markup.rbs +117 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/parser.rbs +56 -0
- data/stdlib/rdoc/0/rdoc.rbs +393 -0
- data/stdlib/rdoc/0/ri.rbs +17 -0
- data/stdlib/rdoc/0/store.rbs +48 -0
- data/stdlib/rdoc/0/top_level.rbs +97 -0
- data/stdlib/resolv/0/manifest.yaml +3 -0
- data/stdlib/resolv/0/resolv.rbs +1787 -0
- data/stdlib/ripper/0/ripper.rbs +1654 -0
- data/stdlib/securerandom/0/manifest.yaml +2 -0
- data/stdlib/securerandom/0/securerandom.rbs +49 -0
- data/stdlib/shellwords/0/shellwords.rbs +229 -0
- data/stdlib/singleton/0/singleton.rbs +134 -0
- data/stdlib/socket/0/addrinfo.rbs +666 -0
- data/stdlib/socket/0/basic_socket.rbs +590 -0
- data/stdlib/socket/0/constants.rbs +2295 -0
- data/stdlib/socket/0/ip_socket.rbs +94 -0
- data/stdlib/socket/0/socket.rbs +4170 -0
- data/stdlib/socket/0/socket_error.rbs +5 -0
- data/stdlib/socket/0/tcp_server.rbs +192 -0
- data/stdlib/socket/0/tcp_socket.rbs +87 -0
- data/stdlib/socket/0/udp_socket.rbs +133 -0
- data/stdlib/socket/0/unix_server.rbs +169 -0
- data/stdlib/socket/0/unix_socket.rbs +172 -0
- data/stdlib/stringio/0/stringio.rbs +1681 -0
- data/stdlib/strscan/0/string_scanner.rbs +1648 -0
- data/stdlib/tempfile/0/tempfile.rbs +483 -0
- data/stdlib/time/0/time.rbs +434 -0
- data/stdlib/timeout/0/timeout.rbs +137 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +69 -0
- data/stdlib/tsort/0/cyclic.rbs +8 -0
- data/stdlib/tsort/0/interfaces.rbs +20 -0
- data/stdlib/tsort/0/tsort.rbs +410 -0
- data/stdlib/uri/0/common.rbs +621 -0
- data/stdlib/uri/0/file.rbs +118 -0
- data/stdlib/uri/0/ftp.rbs +13 -0
- data/stdlib/uri/0/generic.rbs +1116 -0
- data/stdlib/uri/0/http.rbs +104 -0
- data/stdlib/uri/0/https.rbs +14 -0
- data/stdlib/uri/0/ldap.rbs +230 -0
- data/stdlib/uri/0/ldaps.rbs +14 -0
- data/stdlib/uri/0/mailto.rbs +92 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +189 -0
- data/stdlib/uri/0/rfc3986_parser.rbs +2 -0
- data/stdlib/uri/0/ws.rbs +13 -0
- data/stdlib/uri/0/wss.rbs +9 -0
- data/stdlib/yaml/0/manifest.yaml +2 -0
- data/stdlib/yaml/0/yaml.rbs +1 -0
- data/stdlib/zlib/0/buf_error.rbs +10 -0
- data/stdlib/zlib/0/data_error.rbs +10 -0
- data/stdlib/zlib/0/deflate.rbs +211 -0
- data/stdlib/zlib/0/error.rbs +20 -0
- data/stdlib/zlib/0/gzip_file/crc_error.rbs +12 -0
- data/stdlib/zlib/0/gzip_file/error.rbs +23 -0
- data/stdlib/zlib/0/gzip_file/length_error.rbs +12 -0
- data/stdlib/zlib/0/gzip_file/no_footer.rbs +11 -0
- data/stdlib/zlib/0/gzip_file.rbs +156 -0
- data/stdlib/zlib/0/gzip_reader.rbs +293 -0
- data/stdlib/zlib/0/gzip_writer.rbs +168 -0
- data/stdlib/zlib/0/inflate.rbs +180 -0
- data/stdlib/zlib/0/mem_error.rbs +10 -0
- data/stdlib/zlib/0/need_dict.rbs +13 -0
- data/stdlib/zlib/0/stream_end.rbs +11 -0
- data/stdlib/zlib/0/stream_error.rbs +11 -0
- data/stdlib/zlib/0/version_error.rbs +11 -0
- data/stdlib/zlib/0/zlib.rbs +449 -0
- data/stdlib/zlib/0/zstream.rbs +201 -0
- data/wasm/README.md +59 -0
- data/wasm/rbs_wasm.c +411 -0
- metadata +660 -0
data/core/hash.rbs
ADDED
|
@@ -0,0 +1,2183 @@
|
|
|
1
|
+
# <!-- rdoc-file=hash.c -->
|
|
2
|
+
# A Hash object maps each of its unique keys to a specific value.
|
|
3
|
+
#
|
|
4
|
+
# A hash has certain similarities to an Array, but:
|
|
5
|
+
#
|
|
6
|
+
# * An array index is always an integer.
|
|
7
|
+
# * A hash key can be (almost) any object.
|
|
8
|
+
#
|
|
9
|
+
# ### Hash Data Syntax
|
|
10
|
+
#
|
|
11
|
+
# The original syntax for a hash entry uses the "hash rocket," <code>=></code>:
|
|
12
|
+
#
|
|
13
|
+
# h = {:foo => 0, :bar => 1, :baz => 2}
|
|
14
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
15
|
+
#
|
|
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:
|
|
18
|
+
#
|
|
19
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
20
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
21
|
+
#
|
|
22
|
+
# You can also use a string in place of a bareword:
|
|
23
|
+
#
|
|
24
|
+
# h = {'foo': 0, 'bar': 1, 'baz': 2}
|
|
25
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
26
|
+
#
|
|
27
|
+
# And you can mix the styles:
|
|
28
|
+
#
|
|
29
|
+
# h = {foo: 0, :bar => 1, 'baz': 2}
|
|
30
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
31
|
+
#
|
|
32
|
+
# But it's an error to try the JSON-style syntax for a key that's not a bareword
|
|
33
|
+
# or a string:
|
|
34
|
+
#
|
|
35
|
+
# # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
|
|
36
|
+
# h = {0: 'zero'}
|
|
37
|
+
#
|
|
38
|
+
# The value can be omitted, meaning that value will be fetched from the context
|
|
39
|
+
# by the name of the key:
|
|
40
|
+
#
|
|
41
|
+
# x = 0
|
|
42
|
+
# y = 100
|
|
43
|
+
# h = {x:, y:}
|
|
44
|
+
# h # => {x: 0, y: 100}
|
|
45
|
+
#
|
|
46
|
+
# ### Common Uses
|
|
47
|
+
#
|
|
48
|
+
# You can use a hash to give names to objects:
|
|
49
|
+
#
|
|
50
|
+
# person = {name: 'Matz', language: 'Ruby'}
|
|
51
|
+
# person # => {name: "Matz", language: "Ruby"}
|
|
52
|
+
#
|
|
53
|
+
# You can use a hash to give names to method arguments:
|
|
54
|
+
#
|
|
55
|
+
# def some_method(hash)
|
|
56
|
+
# p hash
|
|
57
|
+
# end
|
|
58
|
+
# some_method({foo: 0, bar: 1, baz: 2}) # => {foo: 0, bar: 1, baz: 2}
|
|
59
|
+
#
|
|
60
|
+
# Note: when the last argument in a method call is a hash, the curly braces may
|
|
61
|
+
# be omitted:
|
|
62
|
+
#
|
|
63
|
+
# some_method(foo: 0, bar: 1, baz: 2) # => {foo: 0, bar: 1, baz: 2}
|
|
64
|
+
#
|
|
65
|
+
# You can use a hash to initialize an object:
|
|
66
|
+
#
|
|
67
|
+
# class Dev
|
|
68
|
+
# attr_accessor :name, :language
|
|
69
|
+
# def initialize(hash)
|
|
70
|
+
# self.name = hash[:name]
|
|
71
|
+
# self.language = hash[:language]
|
|
72
|
+
# end
|
|
73
|
+
# end
|
|
74
|
+
# matz = Dev.new(name: 'Matz', language: 'Ruby')
|
|
75
|
+
# matz # => #<Dev: @name="Matz", @language="Ruby">
|
|
76
|
+
#
|
|
77
|
+
# ### Creating a Hash
|
|
78
|
+
#
|
|
79
|
+
# You can create a Hash object explicitly with:
|
|
80
|
+
#
|
|
81
|
+
# * A [hash literal](rdoc-ref:syntax/literals.rdoc@Hash+Literals).
|
|
82
|
+
#
|
|
83
|
+
# You can convert certain objects to hashes with:
|
|
84
|
+
#
|
|
85
|
+
# * Method Kernel#Hash.
|
|
86
|
+
#
|
|
87
|
+
# You can create a hash by calling method Hash.new:
|
|
88
|
+
#
|
|
89
|
+
# # Create an empty hash.
|
|
90
|
+
# h = Hash.new
|
|
91
|
+
# h # => {}
|
|
92
|
+
# h.class # => Hash
|
|
93
|
+
#
|
|
94
|
+
# You can create a hash by calling method Hash.[]:
|
|
95
|
+
#
|
|
96
|
+
# # Create an empty hash.
|
|
97
|
+
# h = Hash[]
|
|
98
|
+
# h # => {}
|
|
99
|
+
# # Create a hash with initial entries.
|
|
100
|
+
# h = Hash[foo: 0, bar: 1, baz: 2]
|
|
101
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
102
|
+
#
|
|
103
|
+
# You can create a hash by using its literal form (curly braces):
|
|
104
|
+
#
|
|
105
|
+
# # Create an empty hash.
|
|
106
|
+
# h = {}
|
|
107
|
+
# h # => {}
|
|
108
|
+
# # Create a +Hash+ with initial entries.
|
|
109
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
110
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
111
|
+
#
|
|
112
|
+
# ### Hash Value Basics
|
|
113
|
+
#
|
|
114
|
+
# The simplest way to retrieve a hash value (instance method #[]):
|
|
115
|
+
#
|
|
116
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
117
|
+
# h[:foo] # => 0
|
|
118
|
+
#
|
|
119
|
+
# The simplest way to create or update a hash value (instance method #[]=):
|
|
120
|
+
#
|
|
121
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
122
|
+
# h[:bat] = 3 # => 3
|
|
123
|
+
# h # => {foo: 0, bar: 1, baz: 2, bat: 3}
|
|
124
|
+
# h[:foo] = 4 # => 4
|
|
125
|
+
# h # => {foo: 4, bar: 1, baz: 2, bat: 3}
|
|
126
|
+
#
|
|
127
|
+
# The simplest way to delete a hash entry (instance method #delete):
|
|
128
|
+
#
|
|
129
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
130
|
+
# h.delete(:bar) # => 1
|
|
131
|
+
# h # => {foo: 0, baz: 2}
|
|
132
|
+
#
|
|
133
|
+
# ### Entry Order
|
|
134
|
+
#
|
|
135
|
+
# A Hash object presents its entries in the order of their creation. This is
|
|
136
|
+
# seen in:
|
|
137
|
+
#
|
|
138
|
+
# * Iterative methods such as `each`, `each_key`, `each_pair`, `each_value`.
|
|
139
|
+
# * Other order-sensitive methods such as `shift`, `keys`, `values`.
|
|
140
|
+
# * The string returned by method `inspect`.
|
|
141
|
+
#
|
|
142
|
+
# A new hash has its initial ordering per the given entries:
|
|
143
|
+
#
|
|
144
|
+
# h = Hash[foo: 0, bar: 1]
|
|
145
|
+
# h # => {foo: 0, bar: 1}
|
|
146
|
+
#
|
|
147
|
+
# New entries are added at the end:
|
|
148
|
+
#
|
|
149
|
+
# h[:baz] = 2
|
|
150
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
151
|
+
#
|
|
152
|
+
# Updating a value does not affect the order:
|
|
153
|
+
#
|
|
154
|
+
# h[:baz] = 3
|
|
155
|
+
# h # => {foo: 0, bar: 1, baz: 3}
|
|
156
|
+
#
|
|
157
|
+
# But re-creating a deleted entry can affect the order:
|
|
158
|
+
#
|
|
159
|
+
# h.delete(:foo)
|
|
160
|
+
# h[:foo] = 5
|
|
161
|
+
# h # => {bar: 1, baz: 3, foo: 5}
|
|
162
|
+
#
|
|
163
|
+
# ### `Hash` Keys
|
|
164
|
+
#
|
|
165
|
+
# #### `Hash` Key Equivalence
|
|
166
|
+
#
|
|
167
|
+
# Two objects are treated as the same hash key when their `hash` value is
|
|
168
|
+
# identical and the two objects are <code>eql?</code> to each other.
|
|
169
|
+
#
|
|
170
|
+
# #### Modifying an Active `Hash` Key
|
|
171
|
+
#
|
|
172
|
+
# Modifying a `Hash` key while it is in use damages the hash's index.
|
|
173
|
+
#
|
|
174
|
+
# This `Hash` has keys that are Arrays:
|
|
175
|
+
#
|
|
176
|
+
# a0 = [ :foo, :bar ]
|
|
177
|
+
# a1 = [ :baz, :bat ]
|
|
178
|
+
# h = {a0 => 0, a1 => 1}
|
|
179
|
+
# h.include?(a0) # => true
|
|
180
|
+
# h[a0] # => 0
|
|
181
|
+
# a0.hash # => 110002110
|
|
182
|
+
#
|
|
183
|
+
# Modifying array element <code>a0[0]</code> changes its hash value:
|
|
184
|
+
#
|
|
185
|
+
# a0[0] = :bam
|
|
186
|
+
# a0.hash # => 1069447059
|
|
187
|
+
#
|
|
188
|
+
# And damages the `Hash` index:
|
|
189
|
+
#
|
|
190
|
+
# h.include?(a0) # => false
|
|
191
|
+
# h[a0] # => nil
|
|
192
|
+
#
|
|
193
|
+
# You can repair the hash index using method `rehash`:
|
|
194
|
+
#
|
|
195
|
+
# h.rehash # => {[:bam, :bar]=>0, [:baz, :bat]=>1}
|
|
196
|
+
# h.include?(a0) # => true
|
|
197
|
+
# h[a0] # => 0
|
|
198
|
+
#
|
|
199
|
+
# A String key is always safe. That's because an unfrozen String passed as a key
|
|
200
|
+
# will be replaced by a duplicated and frozen String:
|
|
201
|
+
#
|
|
202
|
+
# s = 'foo'
|
|
203
|
+
# s.frozen? # => false
|
|
204
|
+
# h = {s => 0}
|
|
205
|
+
# first_key = h.keys.first
|
|
206
|
+
# first_key.frozen? # => true
|
|
207
|
+
#
|
|
208
|
+
# #### User-Defined `Hash` Keys
|
|
209
|
+
#
|
|
210
|
+
# To be usable as a `Hash` key, objects must implement the methods `hash` and
|
|
211
|
+
# <code>eql?</code>. Note: this requirement does not apply if the `Hash` uses
|
|
212
|
+
# #compare_by_identity since comparison will then rely on the keys' object id
|
|
213
|
+
# instead of `hash` and <code>eql?</code>.
|
|
214
|
+
#
|
|
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.
|
|
219
|
+
#
|
|
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:
|
|
222
|
+
#
|
|
223
|
+
# class Book
|
|
224
|
+
# attr_reader :author, :title
|
|
225
|
+
#
|
|
226
|
+
# def initialize(author, title)
|
|
227
|
+
# @author = author
|
|
228
|
+
# @title = title
|
|
229
|
+
# end
|
|
230
|
+
#
|
|
231
|
+
# def ==(other)
|
|
232
|
+
# self.class === other &&
|
|
233
|
+
# other.author == @author &&
|
|
234
|
+
# other.title == @title
|
|
235
|
+
# end
|
|
236
|
+
#
|
|
237
|
+
# alias eql? ==
|
|
238
|
+
#
|
|
239
|
+
# def hash
|
|
240
|
+
# [self.class, @author, @title].hash
|
|
241
|
+
# end
|
|
242
|
+
# end
|
|
243
|
+
#
|
|
244
|
+
# book1 = Book.new 'matz', 'Ruby in a Nutshell'
|
|
245
|
+
# book2 = Book.new 'matz', 'Ruby in a Nutshell'
|
|
246
|
+
#
|
|
247
|
+
# reviews = {}
|
|
248
|
+
#
|
|
249
|
+
# reviews[book1] = 'Great reference!'
|
|
250
|
+
# reviews[book2] = 'Nice and compact!'
|
|
251
|
+
#
|
|
252
|
+
# reviews.length #=> 1
|
|
253
|
+
#
|
|
254
|
+
# ### Key Not Found?
|
|
255
|
+
#
|
|
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.
|
|
258
|
+
#
|
|
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.
|
|
261
|
+
#
|
|
262
|
+
# #### Nil Return Value
|
|
263
|
+
#
|
|
264
|
+
# If you want `nil` returned for a not-found key, you can call:
|
|
265
|
+
#
|
|
266
|
+
# * #[](key) (usually written as <code>#[key]</code>.
|
|
267
|
+
# * #assoc(key).
|
|
268
|
+
# * #dig(key, *identifiers).
|
|
269
|
+
# * #values_at(*keys).
|
|
270
|
+
#
|
|
271
|
+
# You can override these behaviors for #[], #dig, and #values_at (but not
|
|
272
|
+
# #assoc); see [Hash Default](rdoc-ref:Hash@Hash+Default).
|
|
273
|
+
#
|
|
274
|
+
# #### KeyError
|
|
275
|
+
#
|
|
276
|
+
# If you want KeyError raised for a not-found key, you can call:
|
|
277
|
+
#
|
|
278
|
+
# * #fetch(key).
|
|
279
|
+
# * #fetch_values(*keys).
|
|
280
|
+
#
|
|
281
|
+
# #### Hash Default
|
|
282
|
+
#
|
|
283
|
+
# For certain methods (#[], #dig, and #values_at), the return value for a
|
|
284
|
+
# not-found key is determined by two hash properties:
|
|
285
|
+
#
|
|
286
|
+
# * *default value*: returned by method #default.
|
|
287
|
+
# * *default proc*: returned by method #default_proc.
|
|
288
|
+
#
|
|
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.
|
|
291
|
+
#
|
|
292
|
+
# Note that this entire section ("Hash Default"):
|
|
293
|
+
#
|
|
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.
|
|
297
|
+
#
|
|
298
|
+
# ##### Any-Key Default
|
|
299
|
+
#
|
|
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:
|
|
302
|
+
#
|
|
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.
|
|
306
|
+
#
|
|
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=.
|
|
309
|
+
#
|
|
310
|
+
# Note: although the value of #default may be any object, it may not be a good
|
|
311
|
+
# idea to use a mutable object.
|
|
312
|
+
#
|
|
313
|
+
# ##### Per-Key Defaults
|
|
314
|
+
#
|
|
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.
|
|
317
|
+
#
|
|
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=.
|
|
320
|
+
#
|
|
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
|
|
323
|
+
# the same key.
|
|
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
|
+
#
|
|
334
|
+
# ### What's Here
|
|
335
|
+
#
|
|
336
|
+
# First, what's elsewhere. Class `Hash`:
|
|
337
|
+
#
|
|
338
|
+
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
|
|
339
|
+
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
|
|
340
|
+
# provides dozens of additional methods.
|
|
341
|
+
#
|
|
342
|
+
# Here, class `Hash` provides methods that are useful for:
|
|
343
|
+
#
|
|
344
|
+
# * [Creating a Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash)
|
|
345
|
+
# * [Setting Hash State](rdoc-ref:Hash@Methods+for+Setting+Hash+State)
|
|
346
|
+
# * [Querying](rdoc-ref:Hash@Methods+for+Querying)
|
|
347
|
+
# * [Comparing](rdoc-ref:Hash@Methods+for+Comparing)
|
|
348
|
+
# * [Fetching](rdoc-ref:Hash@Methods+for+Fetching)
|
|
349
|
+
# * [Assigning](rdoc-ref:Hash@Methods+for+Assigning)
|
|
350
|
+
# * [Deleting](rdoc-ref:Hash@Methods+for+Deleting)
|
|
351
|
+
# * [Iterating](rdoc-ref:Hash@Methods+for+Iterating)
|
|
352
|
+
# * [Converting](rdoc-ref:Hash@Methods+for+Converting)
|
|
353
|
+
# * [Transforming Keys and
|
|
354
|
+
# Values](rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values)
|
|
355
|
+
#
|
|
356
|
+
# Class `Hash` also includes methods from module Enumerable.
|
|
357
|
+
#
|
|
358
|
+
# #### Methods for Creating a `Hash`
|
|
359
|
+
#
|
|
360
|
+
# * ::[]: Returns a new hash populated with given objects.
|
|
361
|
+
# * ::new: Returns a new empty hash.
|
|
362
|
+
# * ::try_convert: Returns a new hash created from a given object.
|
|
363
|
+
#
|
|
364
|
+
# #### Methods for Setting `Hash` State
|
|
365
|
+
#
|
|
366
|
+
# * #compare_by_identity: Sets `self` to consider only identity in comparing
|
|
367
|
+
# keys.
|
|
368
|
+
# * #default=: Sets the default to a given value.
|
|
369
|
+
# * #default_proc=: Sets the default proc to a given proc.
|
|
370
|
+
# * #rehash: Rebuilds the hash table by recomputing the hash index for each
|
|
371
|
+
# key.
|
|
372
|
+
#
|
|
373
|
+
# #### Methods for Querying
|
|
374
|
+
#
|
|
375
|
+
# * #any?: Returns whether any element satisfies a given criterion.
|
|
376
|
+
# * #compare_by_identity?: Returns whether the hash considers only identity
|
|
377
|
+
# when comparing keys.
|
|
378
|
+
# * #default: Returns the default value, or the default value for a given key.
|
|
379
|
+
# * #default_proc: Returns the default proc.
|
|
380
|
+
# * #empty?: Returns whether there are no entries.
|
|
381
|
+
# * #eql?: Returns whether a given object is equal to `self`.
|
|
382
|
+
# * #hash: Returns the integer hash code.
|
|
383
|
+
# * #has_value? (aliased as #value?): Returns whether a given object is a
|
|
384
|
+
# value in `self`.
|
|
385
|
+
# * #include? (aliased as #has_key?, #member?, #key?): Returns whether a given
|
|
386
|
+
# object is a key in `self`.
|
|
387
|
+
# * #size (aliased as #length): Returns the count of entries.
|
|
388
|
+
#
|
|
389
|
+
# #### Methods for Comparing
|
|
390
|
+
#
|
|
391
|
+
# * #<: Returns whether `self` is a proper subset of a given object.
|
|
392
|
+
# * #<=: Returns whether `self` is a subset of a given object.
|
|
393
|
+
# * #==: Returns whether a given object is equal to `self`.
|
|
394
|
+
# * #>: Returns whether `self` is a proper superset of a given object
|
|
395
|
+
# * #>=: Returns whether `self` is a superset of a given object.
|
|
396
|
+
#
|
|
397
|
+
# #### Methods for Fetching
|
|
398
|
+
#
|
|
399
|
+
# * #[]: Returns the value associated with a given key.
|
|
400
|
+
# * #assoc: Returns a 2-element array containing a given key and its value.
|
|
401
|
+
# * #dig: Returns the object in nested objects that is specified by a given
|
|
402
|
+
# key and additional arguments.
|
|
403
|
+
# * #fetch: Returns the value for a given key.
|
|
404
|
+
# * #fetch_values: Returns array containing the values associated with given
|
|
405
|
+
# keys.
|
|
406
|
+
# * #key: Returns the key for the first-found entry with a given value.
|
|
407
|
+
# * #keys: Returns an array containing all keys in `self`.
|
|
408
|
+
# * #rassoc: Returns a 2-element array consisting of the key and value of the
|
|
409
|
+
# first-found entry having a given value.
|
|
410
|
+
# * #values: Returns an array containing all values in `self`.
|
|
411
|
+
# * #values_at: Returns an array containing values for given keys.
|
|
412
|
+
#
|
|
413
|
+
# #### Methods for Assigning
|
|
414
|
+
#
|
|
415
|
+
# * #[]= (aliased as #store): Associates a given key with a given value.
|
|
416
|
+
# * #merge: Returns the hash formed by merging each given hash into a copy of
|
|
417
|
+
# `self`.
|
|
418
|
+
# * #update (aliased as #merge!): Merges each given hash into `self`.
|
|
419
|
+
# * #replace (aliased as #initialize_copy): Replaces the entire contents of
|
|
420
|
+
# `self` with the contents of a given hash.
|
|
421
|
+
#
|
|
422
|
+
# #### Methods for Deleting
|
|
423
|
+
#
|
|
424
|
+
# These methods remove entries from `self`:
|
|
425
|
+
#
|
|
426
|
+
# * #clear: Removes all entries from `self`.
|
|
427
|
+
# * #compact!: Removes all `nil`-valued entries from `self`.
|
|
428
|
+
# * #delete: Removes the entry for a given key.
|
|
429
|
+
# * #delete_if: Removes entries selected by a given block.
|
|
430
|
+
# * #select! (aliased as #filter!): Keep only those entries selected by a
|
|
431
|
+
# given block.
|
|
432
|
+
# * #keep_if: Keep only those entries selected by a given block.
|
|
433
|
+
# * #reject!: Removes entries selected by a given block.
|
|
434
|
+
# * #shift: Removes and returns the first entry.
|
|
435
|
+
#
|
|
436
|
+
# These methods return a copy of `self` with some entries removed:
|
|
437
|
+
#
|
|
438
|
+
# * #compact: Returns a copy of `self` with all `nil`-valued entries removed.
|
|
439
|
+
# * #except: Returns a copy of `self` with entries removed for specified keys.
|
|
440
|
+
# * #select (aliased as #filter): Returns a copy of `self` with only those
|
|
441
|
+
# entries selected by a given block.
|
|
442
|
+
# * #reject: Returns a copy of `self` with entries removed as specified by a
|
|
443
|
+
# given block.
|
|
444
|
+
# * #slice: Returns a hash containing the entries for given keys.
|
|
445
|
+
#
|
|
446
|
+
# #### Methods for Iterating
|
|
447
|
+
# * #each_pair (aliased as #each): Calls a given block with each key-value
|
|
448
|
+
# pair.
|
|
449
|
+
# * #each_key: Calls a given block with each key.
|
|
450
|
+
# * #each_value: Calls a given block with each value.
|
|
451
|
+
#
|
|
452
|
+
# #### Methods for Converting
|
|
453
|
+
#
|
|
454
|
+
# * #flatten: Returns an array that is a 1-dimensional flattening of `self`.
|
|
455
|
+
# * #inspect (aliased as #to_s): Returns a new String containing the hash
|
|
456
|
+
# entries.
|
|
457
|
+
# * #to_a: Returns a new array of 2-element arrays; each nested array contains
|
|
458
|
+
# a key-value pair from `self`.
|
|
459
|
+
# * #to_h: Returns `self` if a `Hash`; if a subclass of `Hash`, returns a
|
|
460
|
+
# `Hash` containing the entries from `self`.
|
|
461
|
+
# * #to_hash: Returns `self`.
|
|
462
|
+
# * #to_proc: Returns a proc that maps a given key to its value.
|
|
463
|
+
#
|
|
464
|
+
# #### Methods for Transforming Keys and Values
|
|
465
|
+
#
|
|
466
|
+
# * #invert: Returns a hash with the each key-value pair inverted.
|
|
467
|
+
# * #transform_keys: Returns a copy of `self` with modified keys.
|
|
468
|
+
# * #transform_keys!: Modifies keys in `self`
|
|
469
|
+
# * #transform_values: Returns a copy of `self` with modified values.
|
|
470
|
+
# * #transform_values!: Modifies values in `self`.
|
|
471
|
+
#
|
|
472
|
+
class Hash[unchecked out K, unchecked out V]
|
|
473
|
+
include Enumerable[[ K, V ]]
|
|
474
|
+
|
|
475
|
+
# Interface that indicates a type can be used as a key to `Hash` lookup methods.
|
|
476
|
+
#
|
|
477
|
+
interface _Key
|
|
478
|
+
def hash: () -> Integer
|
|
479
|
+
|
|
480
|
+
def eql?: (untyped rhs) -> boolish
|
|
481
|
+
end
|
|
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
|
+
|
|
495
|
+
# <!--
|
|
496
|
+
# rdoc-file=hash.c
|
|
497
|
+
# - Hash[] -> new_empty_hash
|
|
498
|
+
# - Hash[other_hash] -> new_hash
|
|
499
|
+
# - Hash[ [*2_element_arrays] ] -> new_hash
|
|
500
|
+
# - Hash[*objects] -> new_hash
|
|
501
|
+
# -->
|
|
502
|
+
# Returns a new Hash object populated with the given objects, if any. See
|
|
503
|
+
# Hash::new.
|
|
504
|
+
#
|
|
505
|
+
# With no argument given, returns a new empty hash.
|
|
506
|
+
#
|
|
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`):
|
|
510
|
+
#
|
|
511
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
512
|
+
# Hash[h] # => {foo: 0, bar: 1, baz: 2}
|
|
513
|
+
#
|
|
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:
|
|
517
|
+
#
|
|
518
|
+
# Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {foo: 0, bar: 1}
|
|
519
|
+
#
|
|
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:
|
|
522
|
+
#
|
|
523
|
+
# Hash[:foo, 0, :bar, 1] # => {foo: 0, bar: 1}
|
|
524
|
+
#
|
|
525
|
+
# Raises ArgumentError if the argument list does not conform to any of the
|
|
526
|
+
# above.
|
|
527
|
+
#
|
|
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]
|
|
534
|
+
|
|
535
|
+
# <!--
|
|
536
|
+
# rdoc-file=hash.c
|
|
537
|
+
# - Hash.try_convert(object) -> object, new_hash, or nil
|
|
538
|
+
# -->
|
|
539
|
+
# If `object` is a hash, returns `object`.
|
|
540
|
+
#
|
|
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.
|
|
544
|
+
#
|
|
545
|
+
# Otherwise if `object` does not respond to <code>:to_hash</code>, returns
|
|
546
|
+
# `nil`.
|
|
547
|
+
#
|
|
548
|
+
def self.try_convert: [K, V] (hash[K, V] hash) -> Hash[K, V]
|
|
549
|
+
| (untyped) -> Hash[untyped, untyped]?
|
|
550
|
+
|
|
551
|
+
# <!--
|
|
552
|
+
# rdoc-file=hash.c
|
|
553
|
+
# - self < other -> true or false
|
|
554
|
+
# -->
|
|
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).
|
|
567
|
+
#
|
|
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
|
|
574
|
+
|
|
575
|
+
# <!--
|
|
576
|
+
# rdoc-file=hash.c
|
|
577
|
+
# - self <= other -> true or false
|
|
578
|
+
# -->
|
|
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
|
|
586
|
+
#
|
|
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
|
|
595
|
+
|
|
596
|
+
# <!--
|
|
597
|
+
# rdoc-file=hash.c
|
|
598
|
+
# - self == object -> true or false
|
|
599
|
+
# -->
|
|
600
|
+
# Returns whether `self` and `object` are equal.
|
|
601
|
+
#
|
|
602
|
+
# Returns `true` if all of the following are true:
|
|
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>.
|
|
607
|
+
#
|
|
608
|
+
# Otherwise, returns `false`.
|
|
609
|
+
#
|
|
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).
|
|
621
|
+
#
|
|
622
|
+
def ==: (untyped other) -> bool
|
|
623
|
+
|
|
624
|
+
# <!--
|
|
625
|
+
# rdoc-file=hash.c
|
|
626
|
+
# - self > other_hash -> true or false
|
|
627
|
+
# -->
|
|
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.
|
|
643
|
+
#
|
|
644
|
+
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
|
|
645
|
+
#
|
|
646
|
+
def >: (hash[untyped, untyped] other_hash) -> bool
|
|
647
|
+
|
|
648
|
+
# <!--
|
|
649
|
+
# rdoc-file=hash.c
|
|
650
|
+
# - self >= other_hash -> true or false
|
|
651
|
+
# -->
|
|
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.
|
|
665
|
+
#
|
|
666
|
+
# Related: see [Methods for Comparing](rdoc-ref:Hash@Methods+for+Comparing).
|
|
667
|
+
#
|
|
668
|
+
def >=: (hash[untyped, untyped] other_hash) -> bool
|
|
669
|
+
|
|
670
|
+
# <!--
|
|
671
|
+
# rdoc-file=hash.c
|
|
672
|
+
# - self[key] -> object
|
|
673
|
+
# -->
|
|
674
|
+
# Searches for a hash key equivalent to the given `key`; see [Hash Key
|
|
675
|
+
# Equivalence](rdoc-ref:Hash@Hash+Key+Equivalence).
|
|
676
|
+
#
|
|
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)).
|
|
684
|
+
#
|
|
685
|
+
# Related: #[]=; see also [Methods for
|
|
686
|
+
# Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
687
|
+
#
|
|
688
|
+
def []: %a{implicitly-returns-nil} (_Key key) -> V
|
|
689
|
+
|
|
690
|
+
# <!--
|
|
691
|
+
# rdoc-file=hash.c
|
|
692
|
+
# - self[key] = object -> object
|
|
693
|
+
# -->
|
|
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)):
|
|
701
|
+
#
|
|
702
|
+
# h = {foo: 0, bar: 1}
|
|
703
|
+
# h[:foo] = 2 # => 2
|
|
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)):
|
|
709
|
+
#
|
|
710
|
+
# h = {foo: 0, bar: 1}
|
|
711
|
+
# h[:baz] = 2 # => 2
|
|
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).
|
|
717
|
+
#
|
|
718
|
+
def []=: (K key, V value) -> V
|
|
719
|
+
|
|
720
|
+
# <!--
|
|
721
|
+
# rdoc-file=hash.c
|
|
722
|
+
# - any? -> true or false
|
|
723
|
+
# - any?(entry) -> true or false
|
|
724
|
+
# - any? {|key, value| ... } -> true or false
|
|
725
|
+
# -->
|
|
726
|
+
# Returns `true` if any element satisfies a given criterion; `false` otherwise.
|
|
727
|
+
#
|
|
728
|
+
# If `self` has no element, returns `false` and argument or block are not used;
|
|
729
|
+
# otherwise behaves as below.
|
|
730
|
+
#
|
|
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:
|
|
736
|
+
#
|
|
737
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
738
|
+
# h.assoc(:bar) # => [:bar, 1]
|
|
739
|
+
# h.any?([:bar, 1]) # => true
|
|
740
|
+
# h.any?([:bar, 0]) # => false
|
|
741
|
+
#
|
|
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
|
+
#
|
|
745
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
746
|
+
# h.any? {|key, value| value < 3 } # => true
|
|
747
|
+
# h.any? {|key, value| value > 3 } # => false
|
|
748
|
+
#
|
|
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).
|
|
754
|
+
#
|
|
755
|
+
def any?: () -> bool
|
|
756
|
+
| (Enumerable::_Pattern pattern) -> bool
|
|
757
|
+
| () { ([ K, V ] pair) -> boolish } -> bool
|
|
758
|
+
|
|
759
|
+
# <!--
|
|
760
|
+
# rdoc-file=hash.c
|
|
761
|
+
# - assoc(key) -> entry or nil
|
|
762
|
+
# -->
|
|
763
|
+
# If the given `key` is found, returns its entry as a 2-element array containing
|
|
764
|
+
# that key and its value:
|
|
765
|
+
#
|
|
766
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
767
|
+
# h.assoc(:bar) # => [:bar, 1]
|
|
768
|
+
#
|
|
769
|
+
# Returns `nil` if the key is not found.
|
|
770
|
+
#
|
|
771
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
772
|
+
#
|
|
773
|
+
def assoc: (_Equals key) -> [ K, V ]?
|
|
774
|
+
|
|
775
|
+
# <!--
|
|
776
|
+
# rdoc-file=hash.c
|
|
777
|
+
# - clear -> self
|
|
778
|
+
# -->
|
|
779
|
+
# Removes all entries from `self`; returns emptied `self`.
|
|
780
|
+
#
|
|
781
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
782
|
+
#
|
|
783
|
+
def clear: () -> self
|
|
784
|
+
|
|
785
|
+
# <!--
|
|
786
|
+
# rdoc-file=hash.c
|
|
787
|
+
# - compact -> new_hash
|
|
788
|
+
# -->
|
|
789
|
+
# Returns a copy of `self` with all `nil`-valued entries removed:
|
|
790
|
+
#
|
|
791
|
+
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
|
792
|
+
# h.compact # => {foo: 0, baz: 2}
|
|
793
|
+
#
|
|
794
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
795
|
+
#
|
|
796
|
+
def compact: () -> instance
|
|
797
|
+
|
|
798
|
+
# <!--
|
|
799
|
+
# rdoc-file=hash.c
|
|
800
|
+
# - compact! -> self or nil
|
|
801
|
+
# -->
|
|
802
|
+
# If `self` contains any `nil`-valued entries, returns `self` with all
|
|
803
|
+
# `nil`-valued entries removed; returns `nil` otherwise:
|
|
804
|
+
#
|
|
805
|
+
# h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
|
806
|
+
# h.compact!
|
|
807
|
+
# h # => {foo: 0, baz: 2}
|
|
808
|
+
# h.compact! # => nil
|
|
809
|
+
#
|
|
810
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
811
|
+
#
|
|
812
|
+
def compact!: () -> self?
|
|
813
|
+
|
|
814
|
+
# <!--
|
|
815
|
+
# rdoc-file=hash.c
|
|
816
|
+
# - compare_by_identity -> self
|
|
817
|
+
# -->
|
|
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?):
|
|
823
|
+
#
|
|
824
|
+
# h = {}
|
|
825
|
+
# h['x'] = 0
|
|
826
|
+
# h['x'] = 1 # Overwrites.
|
|
827
|
+
# h # => {"x"=>1}
|
|
828
|
+
#
|
|
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).
|
|
838
|
+
#
|
|
839
|
+
def compare_by_identity: () -> self
|
|
840
|
+
|
|
841
|
+
# <!--
|
|
842
|
+
# rdoc-file=hash.c
|
|
843
|
+
# - compare_by_identity? -> true or false
|
|
844
|
+
# -->
|
|
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).
|
|
854
|
+
#
|
|
855
|
+
def compare_by_identity?: () -> bool
|
|
856
|
+
|
|
857
|
+
# <!--
|
|
858
|
+
# rdoc-file=hash.c
|
|
859
|
+
# - deconstruct_keys(p1)
|
|
860
|
+
# -->
|
|
861
|
+
#
|
|
862
|
+
def deconstruct_keys: (untyped) -> self
|
|
863
|
+
|
|
864
|
+
# <!--
|
|
865
|
+
# rdoc-file=hash.c
|
|
866
|
+
# - default -> object
|
|
867
|
+
# - default(key) -> object
|
|
868
|
+
# -->
|
|
869
|
+
# Returns the default value for the given `key`. The returned value will be
|
|
870
|
+
# determined either by the default proc or by the default value. See [Hash
|
|
871
|
+
# Default](rdoc-ref:Hash@Hash+Default).
|
|
872
|
+
#
|
|
873
|
+
# With no argument, returns the current default value:
|
|
874
|
+
# h = {}
|
|
875
|
+
# h.default # => nil
|
|
876
|
+
#
|
|
877
|
+
# If `key` is given, returns the default value for `key`, regardless of whether
|
|
878
|
+
# that key exists:
|
|
879
|
+
# h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
|
|
880
|
+
# h[:foo] = "Hello"
|
|
881
|
+
# h.default(:foo) # => "No key foo"
|
|
882
|
+
#
|
|
883
|
+
def default: (?_Key key) -> V?
|
|
884
|
+
|
|
885
|
+
# <!--
|
|
886
|
+
# rdoc-file=hash.c
|
|
887
|
+
# - default = value -> object
|
|
888
|
+
# -->
|
|
889
|
+
# Sets the default value to `value`; returns `value`:
|
|
890
|
+
# h = {}
|
|
891
|
+
# h.default # => nil
|
|
892
|
+
# h.default = false # => false
|
|
893
|
+
# h.default # => false
|
|
894
|
+
#
|
|
895
|
+
# See [Hash Default](rdoc-ref:Hash@Hash+Default).
|
|
896
|
+
#
|
|
897
|
+
def default=: (V default) -> V
|
|
898
|
+
|
|
899
|
+
# <!--
|
|
900
|
+
# rdoc-file=hash.c
|
|
901
|
+
# - default_proc -> proc or nil
|
|
902
|
+
# -->
|
|
903
|
+
# Returns the default proc for `self` (see [Hash
|
|
904
|
+
# Default](rdoc-ref:Hash@Hash+Default)):
|
|
905
|
+
# h = {}
|
|
906
|
+
# h.default_proc # => nil
|
|
907
|
+
# h.default_proc = proc {|hash, key| "Default value for #{key}" }
|
|
908
|
+
# h.default_proc.class # => Proc
|
|
909
|
+
#
|
|
910
|
+
def default_proc: () -> (^(self, _Key key) -> V)?
|
|
911
|
+
|
|
912
|
+
# <!--
|
|
913
|
+
# rdoc-file=hash.c
|
|
914
|
+
# - default_proc = proc -> proc
|
|
915
|
+
# -->
|
|
916
|
+
# Sets the default proc for `self` to `proc` (see [Hash
|
|
917
|
+
# Default](rdoc-ref:Hash@Hash+Default)):
|
|
918
|
+
# h = {}
|
|
919
|
+
# h.default_proc # => nil
|
|
920
|
+
# h.default_proc = proc { |hash, key| "Default value for #{key}" }
|
|
921
|
+
# h.default_proc.class # => Proc
|
|
922
|
+
# h.default_proc = nil
|
|
923
|
+
# h.default_proc # => nil
|
|
924
|
+
#
|
|
925
|
+
def default_proc=: (nil) -> nil
|
|
926
|
+
| (^(self, _Key key) -> V proc) -> ^(self, _Key) -> V
|
|
927
|
+
| (_ToProc proc) -> Proc
|
|
928
|
+
|
|
929
|
+
# <!--
|
|
930
|
+
# rdoc-file=hash.c
|
|
931
|
+
# - delete(key) -> value or nil
|
|
932
|
+
# - delete(key) {|key| ... } -> object
|
|
933
|
+
# -->
|
|
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:
|
|
938
|
+
#
|
|
939
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
940
|
+
# h.delete(:bar) # => 1
|
|
941
|
+
# h # => {foo: 0, baz: 2}
|
|
942
|
+
#
|
|
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:
|
|
947
|
+
#
|
|
948
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
949
|
+
# h.delete(:baz) { |key| raise 'Will never happen'} # => 2
|
|
950
|
+
# h # => {foo: 0, bar: 1}
|
|
951
|
+
#
|
|
952
|
+
# With a block given and `key` not found, calls the block and returns the
|
|
953
|
+
# block's return value:
|
|
954
|
+
#
|
|
955
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
956
|
+
# h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
|
|
957
|
+
# h # => {foo: 0, bar: 1, baz: 2}
|
|
958
|
+
#
|
|
959
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
960
|
+
#
|
|
961
|
+
def delete: (_Key key) -> V?
|
|
962
|
+
| [T, K2 < _Key] (K2 key) { (K2 key) -> T } -> (T | V)
|
|
963
|
+
|
|
964
|
+
# <!--
|
|
965
|
+
# rdoc-file=hash.c
|
|
966
|
+
# - delete_if {|key, value| ... } -> self
|
|
967
|
+
# - delete_if -> new_enumerator
|
|
968
|
+
# -->
|
|
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`:
|
|
971
|
+
#
|
|
972
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
973
|
+
# h.delete_if {|key, value| value > 0 } # => {foo: 0}
|
|
974
|
+
#
|
|
975
|
+
# With no block given, returns a new Enumerator.
|
|
976
|
+
#
|
|
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
|
|
981
|
+
|
|
982
|
+
# <!--
|
|
983
|
+
# rdoc-file=hash.c
|
|
984
|
+
# - dig(key, *identifiers) -> object
|
|
985
|
+
# -->
|
|
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:
|
|
993
|
+
#
|
|
994
|
+
# h = {foo: {bar: {baz: 2}}}
|
|
995
|
+
# h.dig(:foo) # => {bar: {baz: 2}}
|
|
996
|
+
# h.dig(:foo, :bar) # => {baz: 2}
|
|
997
|
+
# h.dig(:foo, :bar, :baz) # => 2
|
|
998
|
+
# h.dig(:foo, :bar, :BAZ) # => nil
|
|
999
|
+
#
|
|
1000
|
+
# Nested hashes and arrays:
|
|
1001
|
+
#
|
|
1002
|
+
# h = {foo: {bar: [:a, :b, :c]}}
|
|
1003
|
+
# h.dig(:foo, :bar, 2) # => :c
|
|
1004
|
+
#
|
|
1005
|
+
# If no such object is found, returns the [hash
|
|
1006
|
+
# default](rdoc-ref:Hash@Hash+Default):
|
|
1007
|
+
#
|
|
1008
|
+
# h = {foo: {bar: [:a, :b, :c]}}
|
|
1009
|
+
# h.dig(:hello) # => nil
|
|
1010
|
+
# h.default_proc = -> (hash, _key) { hash }
|
|
1011
|
+
# h.dig(:hello, :world)
|
|
1012
|
+
# # => {:foo=>{:bar=>[:a, :b, :c]}}
|
|
1013
|
+
#
|
|
1014
|
+
# Related: [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1015
|
+
#
|
|
1016
|
+
def dig: (_Key key, *untyped identifiers) -> untyped
|
|
1017
|
+
|
|
1018
|
+
# <!-- rdoc-file=hash.c -->
|
|
1019
|
+
# With a block given, calls the block with each key-value pair; returns `self`:
|
|
1020
|
+
#
|
|
1021
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1022
|
+
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo: 0, bar: 1, baz: 2}
|
|
1023
|
+
#
|
|
1024
|
+
# Output:
|
|
1025
|
+
#
|
|
1026
|
+
# foo: 0
|
|
1027
|
+
# bar: 1
|
|
1028
|
+
# baz: 2
|
|
1029
|
+
#
|
|
1030
|
+
# With no block given, returns a new Enumerator.
|
|
1031
|
+
#
|
|
1032
|
+
# Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
|
|
1033
|
+
#
|
|
1034
|
+
def each: () -> Enumerator[[ K, V ], self]
|
|
1035
|
+
| () { ([ K, V ] pair) -> void } -> self
|
|
1036
|
+
|
|
1037
|
+
# <!--
|
|
1038
|
+
# rdoc-file=hash.c
|
|
1039
|
+
# - each_key {|key| ... } -> self
|
|
1040
|
+
# - each_key -> new_enumerator
|
|
1041
|
+
# -->
|
|
1042
|
+
# With a block given, calls the block with each key; returns `self`:
|
|
1043
|
+
#
|
|
1044
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1045
|
+
# h.each_key {|key| puts key } # => {foo: 0, bar: 1, baz: 2}
|
|
1046
|
+
#
|
|
1047
|
+
# Output:
|
|
1048
|
+
# foo
|
|
1049
|
+
# bar
|
|
1050
|
+
# baz
|
|
1051
|
+
#
|
|
1052
|
+
# With no block given, returns a new Enumerator.
|
|
1053
|
+
#
|
|
1054
|
+
# Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
|
|
1055
|
+
#
|
|
1056
|
+
def each_key: () -> Enumerator[K, self]
|
|
1057
|
+
| () { (K key) -> void } -> self
|
|
1058
|
+
|
|
1059
|
+
# <!--
|
|
1060
|
+
# rdoc-file=hash.c
|
|
1061
|
+
# - each_pair {|key, value| ... } -> self
|
|
1062
|
+
# - each_pair -> new_enumerator
|
|
1063
|
+
# -->
|
|
1064
|
+
# With a block given, calls the block with each key-value pair; returns `self`:
|
|
1065
|
+
#
|
|
1066
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1067
|
+
# h.each_pair {|key, value| puts "#{key}: #{value}"} # => {foo: 0, bar: 1, baz: 2}
|
|
1068
|
+
#
|
|
1069
|
+
# Output:
|
|
1070
|
+
#
|
|
1071
|
+
# foo: 0
|
|
1072
|
+
# bar: 1
|
|
1073
|
+
# baz: 2
|
|
1074
|
+
#
|
|
1075
|
+
# With no block given, returns a new Enumerator.
|
|
1076
|
+
#
|
|
1077
|
+
# Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
|
|
1078
|
+
#
|
|
1079
|
+
alias each_pair each
|
|
1080
|
+
|
|
1081
|
+
# <!--
|
|
1082
|
+
# rdoc-file=hash.c
|
|
1083
|
+
# - each_value {|value| ... } -> self
|
|
1084
|
+
# - each_value -> new_enumerator
|
|
1085
|
+
# -->
|
|
1086
|
+
# With a block given, calls the block with each value; returns `self`:
|
|
1087
|
+
#
|
|
1088
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1089
|
+
# h.each_value {|value| puts value } # => {foo: 0, bar: 1, baz: 2}
|
|
1090
|
+
#
|
|
1091
|
+
# Output:
|
|
1092
|
+
# 0
|
|
1093
|
+
# 1
|
|
1094
|
+
# 2
|
|
1095
|
+
#
|
|
1096
|
+
# With no block given, returns a new Enumerator.
|
|
1097
|
+
#
|
|
1098
|
+
# Related: see [Methods for Iterating](rdoc-ref:Hash@Methods+for+Iterating).
|
|
1099
|
+
#
|
|
1100
|
+
def each_value: () -> Enumerator[V, self]
|
|
1101
|
+
| () { (V value) -> void } -> self
|
|
1102
|
+
|
|
1103
|
+
# <!--
|
|
1104
|
+
# rdoc-file=hash.c
|
|
1105
|
+
# - empty? -> true or false
|
|
1106
|
+
# -->
|
|
1107
|
+
# Returns `true` if there are no hash entries, `false` otherwise:
|
|
1108
|
+
#
|
|
1109
|
+
# {}.empty? # => true
|
|
1110
|
+
# {foo: 0}.empty? # => false
|
|
1111
|
+
#
|
|
1112
|
+
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1113
|
+
#
|
|
1114
|
+
def empty?: () -> bool
|
|
1115
|
+
|
|
1116
|
+
# <!--
|
|
1117
|
+
# rdoc-file=hash.c
|
|
1118
|
+
# - eql?(object) -> true or false
|
|
1119
|
+
# -->
|
|
1120
|
+
# Returns `true` if all of the following are true:
|
|
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>.
|
|
1125
|
+
#
|
|
1126
|
+
# Otherwise, returns `false`.
|
|
1127
|
+
#
|
|
1128
|
+
# h1 = {foo: 0, bar: 1, baz: 2}
|
|
1129
|
+
# h2 = {foo: 0, bar: 1, baz: 2}
|
|
1130
|
+
# h1.eql? h2 # => true
|
|
1131
|
+
# h3 = {baz: 2, bar: 1, foo: 0}
|
|
1132
|
+
# h1.eql? h3 # => true
|
|
1133
|
+
#
|
|
1134
|
+
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1135
|
+
#
|
|
1136
|
+
def eql?: (untyped object) -> bool
|
|
1137
|
+
|
|
1138
|
+
# <!--
|
|
1139
|
+
# rdoc-file=hash.c
|
|
1140
|
+
# - except(*keys) -> new_hash
|
|
1141
|
+
# -->
|
|
1142
|
+
# Returns a copy of `self` that excludes entries for the given `keys`; any
|
|
1143
|
+
# `keys` that are not found are ignored:
|
|
1144
|
+
#
|
|
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}
|
|
1148
|
+
#
|
|
1149
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1150
|
+
#
|
|
1151
|
+
def except: (*_Key keys) -> Hash[K, V]
|
|
1152
|
+
|
|
1153
|
+
# <!--
|
|
1154
|
+
# rdoc-file=hash.c
|
|
1155
|
+
# - fetch(key) -> object
|
|
1156
|
+
# - fetch(key, default_value) -> object
|
|
1157
|
+
# - fetch(key) {|key| ... } -> object
|
|
1158
|
+
# -->
|
|
1159
|
+
# With no block given, returns the value for the given `key`, if found;
|
|
1160
|
+
#
|
|
1161
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1162
|
+
# h.fetch(:bar) # => 1
|
|
1163
|
+
#
|
|
1164
|
+
# If the key is not found, returns `default_value`, if given, or raises KeyError
|
|
1165
|
+
# otherwise:
|
|
1166
|
+
#
|
|
1167
|
+
# h.fetch(:nosuch, :default) # => :default
|
|
1168
|
+
# h.fetch(:nosuch) # Raises KeyError.
|
|
1169
|
+
#
|
|
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"
|
|
1174
|
+
#
|
|
1175
|
+
# Note that this method does not use the values of either #default or
|
|
1176
|
+
# #default_proc.
|
|
1177
|
+
#
|
|
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)
|
|
1183
|
+
|
|
1184
|
+
# <!--
|
|
1185
|
+
# rdoc-file=hash.c
|
|
1186
|
+
# - fetch_values(*keys) -> new_array
|
|
1187
|
+
# - fetch_values(*keys) {|key| ... } -> new_array
|
|
1188
|
+
# -->
|
|
1189
|
+
# When all given `keys` are found, returns a new array containing the values
|
|
1190
|
+
# associated with the given `keys`:
|
|
1191
|
+
#
|
|
1192
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1193
|
+
# h.fetch_values(:baz, :foo) # => [2, 0]
|
|
1194
|
+
#
|
|
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:
|
|
1197
|
+
#
|
|
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.
|
|
1202
|
+
#
|
|
1203
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1204
|
+
#
|
|
1205
|
+
def fetch_values: (*_Key keys) -> Array[V]
|
|
1206
|
+
| [K2 < _Key, T] (*K2 keys) { (K2 key) -> T } -> Array[V | T]
|
|
1207
|
+
|
|
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:
|
|
1215
|
+
#
|
|
1216
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1217
|
+
# h.select {|key, value| value < 2 } # => {foo: 0, bar: 1}
|
|
1218
|
+
#
|
|
1219
|
+
# With no block given, returns a new Enumerator.
|
|
1220
|
+
#
|
|
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]
|
|
1225
|
+
|
|
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`.
|
|
1233
|
+
#
|
|
1234
|
+
# Returns `self` if any entries were removed, `nil` otherwise:
|
|
1235
|
+
#
|
|
1236
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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.
|
|
1241
|
+
#
|
|
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?
|
|
1246
|
+
|
|
1247
|
+
# <!--
|
|
1248
|
+
# rdoc-file=hash.c
|
|
1249
|
+
# - flatten(depth = 1) -> new_array
|
|
1250
|
+
# -->
|
|
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]
|
|
1270
|
+
#
|
|
1271
|
+
# With negative integer `depth`, flattens all levels:
|
|
1272
|
+
#
|
|
1273
|
+
# h.flatten(-1) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
|
|
1274
|
+
#
|
|
1275
|
+
# With `depth` zero, returns the equivalent of #to_a:
|
|
1276
|
+
#
|
|
1277
|
+
# h.flatten(0) # => [[:foo, {:bar=>1, :baz=>2}], [:bat, [:bam, [:bap, [:bah]]]]]
|
|
1278
|
+
#
|
|
1279
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1280
|
+
#
|
|
1281
|
+
def flatten: (?1 level) -> Array[K | V]
|
|
1282
|
+
| (0 level) -> Array[[K, V]]
|
|
1283
|
+
| (int level) -> Array[untyped]
|
|
1284
|
+
|
|
1285
|
+
# <!-- rdoc-file=hash.c -->
|
|
1286
|
+
# Returns whether `key` is a key in `self`:
|
|
1287
|
+
#
|
|
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
|
|
1295
|
+
|
|
1296
|
+
# <!--
|
|
1297
|
+
# rdoc-file=hash.c
|
|
1298
|
+
# - has_value?(value) -> true or false
|
|
1299
|
+
# -->
|
|
1300
|
+
# Returns whether `value` is a value in `self`.
|
|
1301
|
+
#
|
|
1302
|
+
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1303
|
+
#
|
|
1304
|
+
def has_value?: (top value) -> bool
|
|
1305
|
+
|
|
1306
|
+
# <!--
|
|
1307
|
+
# rdoc-file=hash.c
|
|
1308
|
+
# - hash -> an_integer
|
|
1309
|
+
# -->
|
|
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):
|
|
1314
|
+
#
|
|
1315
|
+
# h1 = {foo: 0, bar: 1, baz: 2}
|
|
1316
|
+
# h2 = {baz: 2, bar: 1, foo: 0}
|
|
1317
|
+
# h2.hash == h1.hash # => true
|
|
1318
|
+
# h2.eql? h1 # => true
|
|
1319
|
+
#
|
|
1320
|
+
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1321
|
+
#
|
|
1322
|
+
def hash: () -> Integer
|
|
1323
|
+
|
|
1324
|
+
# <!--
|
|
1325
|
+
# rdoc-file=hash.c
|
|
1326
|
+
# - include?(key) -> true or false
|
|
1327
|
+
# -->
|
|
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).
|
|
1335
|
+
#
|
|
1336
|
+
alias include? has_key?
|
|
1337
|
+
|
|
1338
|
+
# <!--
|
|
1339
|
+
# rdoc-file=hash.c
|
|
1340
|
+
# - inspect -> new_string
|
|
1341
|
+
# -->
|
|
1342
|
+
# Returns a new string containing the hash entries:
|
|
1343
|
+
#
|
|
1344
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1345
|
+
# h.inspect # => "{foo: 0, bar: 1, baz: 2}"
|
|
1346
|
+
#
|
|
1347
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1348
|
+
#
|
|
1349
|
+
def inspect: () -> String
|
|
1350
|
+
|
|
1351
|
+
# <!--
|
|
1352
|
+
# rdoc-file=hash.c
|
|
1353
|
+
# - invert -> new_hash
|
|
1354
|
+
# -->
|
|
1355
|
+
# Returns a new hash with each key-value pair inverted:
|
|
1356
|
+
#
|
|
1357
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1358
|
+
# h1 = h.invert
|
|
1359
|
+
# h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
|
|
1360
|
+
#
|
|
1361
|
+
# Overwrites any repeated new keys (see [Entry
|
|
1362
|
+
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
1363
|
+
#
|
|
1364
|
+
# h = {foo: 0, bar: 0, baz: 0}
|
|
1365
|
+
# h.invert # => {0=>:baz}
|
|
1366
|
+
#
|
|
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]
|
|
1371
|
+
|
|
1372
|
+
# <!--
|
|
1373
|
+
# rdoc-file=hash.c
|
|
1374
|
+
# - keep_if {|key, value| ... } -> self
|
|
1375
|
+
# - keep_if -> new_enumerator
|
|
1376
|
+
# -->
|
|
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`:
|
|
1380
|
+
#
|
|
1381
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1382
|
+
# h.keep_if { |key, value| key.start_with?('b') } # => {bar: 1, baz: 2}
|
|
1383
|
+
#
|
|
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
|
|
1390
|
+
|
|
1391
|
+
# <!--
|
|
1392
|
+
# rdoc-file=hash.c
|
|
1393
|
+
# - key(value) -> key or nil
|
|
1394
|
+
# -->
|
|
1395
|
+
# Returns the key for the first-found entry with the given `value` (see [Entry
|
|
1396
|
+
# Order](rdoc-ref:Hash@Entry+Order)):
|
|
1397
|
+
#
|
|
1398
|
+
# h = {foo: 0, bar: 2, baz: 2}
|
|
1399
|
+
# h.key(0) # => :foo
|
|
1400
|
+
# h.key(2) # => :bar
|
|
1401
|
+
#
|
|
1402
|
+
# Returns `nil` if no such value is found.
|
|
1403
|
+
#
|
|
1404
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1405
|
+
#
|
|
1406
|
+
def key: (_Equals) -> K?
|
|
1407
|
+
|
|
1408
|
+
# <!-- rdoc-file=hash.c -->
|
|
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).
|
|
1416
|
+
#
|
|
1417
|
+
alias key? has_key?
|
|
1418
|
+
|
|
1419
|
+
# <!--
|
|
1420
|
+
# rdoc-file=hash.c
|
|
1421
|
+
# - keys -> new_array
|
|
1422
|
+
# -->
|
|
1423
|
+
# Returns a new array containing all keys in `self`:
|
|
1424
|
+
#
|
|
1425
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1426
|
+
# h.keys # => [:foo, :bar, :baz]
|
|
1427
|
+
#
|
|
1428
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1429
|
+
#
|
|
1430
|
+
def keys: () -> Array[K]
|
|
1431
|
+
|
|
1432
|
+
# <!--
|
|
1433
|
+
# rdoc-file=hash.c
|
|
1434
|
+
# - size -> integer
|
|
1435
|
+
# -->
|
|
1436
|
+
# Returns the count of entries in `self`:
|
|
1437
|
+
#
|
|
1438
|
+
# {foo: 0, bar: 1, baz: 2}.size # => 3
|
|
1439
|
+
#
|
|
1440
|
+
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1441
|
+
#
|
|
1442
|
+
def size: () -> Integer
|
|
1443
|
+
|
|
1444
|
+
# <!-- rdoc-file=hash.c -->
|
|
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).
|
|
1452
|
+
#
|
|
1453
|
+
alias member? has_key?
|
|
1454
|
+
|
|
1455
|
+
# <!--
|
|
1456
|
+
# rdoc-file=hash.c
|
|
1457
|
+
# - merge(*other_hashes) -> new_hash
|
|
1458
|
+
# - merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
|
|
1459
|
+
# -->
|
|
1460
|
+
# Each argument `other_hash` in `other_hashes` must be a hash.
|
|
1461
|
+
#
|
|
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`:
|
|
1465
|
+
#
|
|
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.
|
|
1469
|
+
#
|
|
1470
|
+
# Example:
|
|
1471
|
+
#
|
|
1472
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1473
|
+
# h1 = {bat: 3, bar: 4}
|
|
1474
|
+
# h2 = {bam: 5, bat:6}
|
|
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:
|
|
1480
|
+
#
|
|
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`.
|
|
1484
|
+
#
|
|
1485
|
+
# Example:
|
|
1486
|
+
#
|
|
1487
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1488
|
+
# h1 = {bat: 3, bar: 4}
|
|
1489
|
+
# h2 = {bam: 5, bat:6}
|
|
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}
|
|
1492
|
+
#
|
|
1493
|
+
# With no arguments, returns a copy of `self`; the block, if given, is ignored.
|
|
1494
|
+
#
|
|
1495
|
+
# Related: see [Methods for Assigning](rdoc-ref:Hash@Methods+for+Assigning).
|
|
1496
|
+
#
|
|
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]
|
|
1499
|
+
|
|
1500
|
+
# <!-- rdoc-file=hash.c -->
|
|
1501
|
+
# Updates values and/or adds entries to `self`; returns `self`.
|
|
1502
|
+
#
|
|
1503
|
+
# Each argument `other_hash` in `other_hashes` must be a hash.
|
|
1504
|
+
#
|
|
1505
|
+
# With no block given, for each successive entry `key`/`new_value` in each
|
|
1506
|
+
# successive `other_hash`:
|
|
1507
|
+
#
|
|
1508
|
+
# * If `key` is in `self`, sets <code>self[key] = new_value</code>, whose
|
|
1509
|
+
# position is unchanged:
|
|
1510
|
+
#
|
|
1511
|
+
# h0 = {foo: 0, bar: 1, baz: 2}
|
|
1512
|
+
# h1 = {bar: 3, foo: -1}
|
|
1513
|
+
# h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
|
|
1514
|
+
#
|
|
1515
|
+
# * If `key` is not in `self`, adds the entry at the end of `self`:
|
|
1516
|
+
#
|
|
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}
|
|
1519
|
+
#
|
|
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}
|
|
1532
|
+
#
|
|
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
|
|
1543
|
+
|
|
1544
|
+
# <!--
|
|
1545
|
+
# rdoc-file=hash.c
|
|
1546
|
+
# - rassoc(value) -> new_array or nil
|
|
1547
|
+
# -->
|
|
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
|
+
#
|
|
1554
|
+
# h = {foo: 0, bar: 1, baz: 1}
|
|
1555
|
+
# h.rassoc(1) # => [:bar, 1]
|
|
1556
|
+
#
|
|
1557
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
1558
|
+
#
|
|
1559
|
+
def rassoc: (_Equals value) -> [ K, V ]?
|
|
1560
|
+
|
|
1561
|
+
# <!--
|
|
1562
|
+
# rdoc-file=hash.c
|
|
1563
|
+
# - rehash -> self
|
|
1564
|
+
# -->
|
|
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.
|
|
1567
|
+
#
|
|
1568
|
+
# The hash table becomes invalid if the hash value of a key has changed after
|
|
1569
|
+
# the entry was created. See [Modifying an Active Hash
|
|
1570
|
+
# Key](rdoc-ref:Hash@Modifying+an+Active+Hash+Key).
|
|
1571
|
+
#
|
|
1572
|
+
def rehash: () -> self
|
|
1573
|
+
|
|
1574
|
+
def freeze: () -> self
|
|
1575
|
+
|
|
1576
|
+
# <!--
|
|
1577
|
+
# rdoc-file=hash.c
|
|
1578
|
+
# - reject {|key, value| ... } -> new_hash
|
|
1579
|
+
# - reject -> new_enumerator
|
|
1580
|
+
# -->
|
|
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:
|
|
1584
|
+
#
|
|
1585
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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).
|
|
1592
|
+
#
|
|
1593
|
+
def reject: () -> Enumerator[[ K, V ], Hash[K, V]]
|
|
1594
|
+
| () { (K key, V value) -> boolish } -> Hash[K, V]
|
|
1595
|
+
|
|
1596
|
+
# <!--
|
|
1597
|
+
# rdoc-file=hash.c
|
|
1598
|
+
# - reject! {|key, value| ... } -> self or nil
|
|
1599
|
+
# - reject! -> new_enumerator
|
|
1600
|
+
# -->
|
|
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.
|
|
1603
|
+
#
|
|
1604
|
+
# Return `self` if any entries were removed, `nil` otherwise:
|
|
1605
|
+
#
|
|
1606
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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).
|
|
1613
|
+
#
|
|
1614
|
+
def reject!: () -> Enumerator[[ K, V ], self?]
|
|
1615
|
+
| () { (K key, V value) -> boolish } -> self?
|
|
1616
|
+
|
|
1617
|
+
# <!-- rdoc-file=hash.c -->
|
|
1618
|
+
# Replaces the entire contents of `self` with the contents of `other_hash`;
|
|
1619
|
+
# returns `self`:
|
|
1620
|
+
#
|
|
1621
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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`.
|
|
1626
|
+
#
|
|
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
|
|
1635
|
+
|
|
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:
|
|
1639
|
+
#
|
|
1640
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1641
|
+
# h.select {|key, value| value < 2 } # => {foo: 0, bar: 1}
|
|
1642
|
+
#
|
|
1643
|
+
# With no block given, returns a new Enumerator.
|
|
1644
|
+
#
|
|
1645
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1646
|
+
#
|
|
1647
|
+
alias filter select
|
|
1648
|
+
|
|
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`.
|
|
1652
|
+
#
|
|
1653
|
+
# Returns `self` if any entries were removed, `nil` otherwise:
|
|
1654
|
+
#
|
|
1655
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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.
|
|
1660
|
+
#
|
|
1661
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1662
|
+
#
|
|
1663
|
+
alias filter! select!
|
|
1664
|
+
|
|
1665
|
+
# <!--
|
|
1666
|
+
# rdoc-file=hash.c
|
|
1667
|
+
# - shift -> [key, value] or nil
|
|
1668
|
+
# -->
|
|
1669
|
+
# Removes and returns the first entry of `self` as a 2-element array; see [Entry
|
|
1670
|
+
# Order](rdoc-ref:Hash@Entry+Order):
|
|
1671
|
+
#
|
|
1672
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1673
|
+
# h.shift # => [:foo, 0]
|
|
1674
|
+
# h # => {bar: 1, baz: 2}
|
|
1675
|
+
#
|
|
1676
|
+
# Returns `nil` if `self` is empty.
|
|
1677
|
+
#
|
|
1678
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1679
|
+
#
|
|
1680
|
+
def shift: () -> [ K, V ]?
|
|
1681
|
+
|
|
1682
|
+
# <!-- rdoc-file=hash.c -->
|
|
1683
|
+
# Returns the count of entries in `self`:
|
|
1684
|
+
#
|
|
1685
|
+
# {foo: 0, bar: 1, baz: 2}.size # => 3
|
|
1686
|
+
#
|
|
1687
|
+
# Related: see [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
1688
|
+
#
|
|
1689
|
+
alias length size
|
|
1690
|
+
|
|
1691
|
+
# <!--
|
|
1692
|
+
# rdoc-file=hash.c
|
|
1693
|
+
# - slice(*keys) -> new_hash
|
|
1694
|
+
# -->
|
|
1695
|
+
# Returns a new hash containing the entries from `self` for the given `keys`;
|
|
1696
|
+
# ignores any keys that are not found:
|
|
1697
|
+
#
|
|
1698
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1699
|
+
# h.slice(:baz, :foo, :nosuch) # => {baz: 2, foo: 0}
|
|
1700
|
+
#
|
|
1701
|
+
# Related: see [Methods for Deleting](rdoc-ref:Hash@Methods+for+Deleting).
|
|
1702
|
+
#
|
|
1703
|
+
def slice: [K2 < _Key] (*K2 keys) -> Hash[K2, V]
|
|
1704
|
+
|
|
1705
|
+
# <!-- rdoc-file=hash.c -->
|
|
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)):
|
|
1713
|
+
#
|
|
1714
|
+
# h = {foo: 0, bar: 1}
|
|
1715
|
+
# h[:foo] = 2 # => 2
|
|
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)):
|
|
1721
|
+
#
|
|
1722
|
+
# h = {foo: 0, bar: 1}
|
|
1723
|
+
# h[:baz] = 2 # => 2
|
|
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).
|
|
1729
|
+
#
|
|
1730
|
+
alias store []=
|
|
1731
|
+
|
|
1732
|
+
# <!--
|
|
1733
|
+
# rdoc-file=hash.c
|
|
1734
|
+
# - to_a -> new_array
|
|
1735
|
+
# -->
|
|
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
|
+
#
|
|
1739
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1740
|
+
# h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
|
|
1741
|
+
#
|
|
1742
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1743
|
+
#
|
|
1744
|
+
def to_a: () -> Array[[ K, V ]]
|
|
1745
|
+
|
|
1746
|
+
# <!--
|
|
1747
|
+
# rdoc-file=hash.c
|
|
1748
|
+
# - to_h {|key, value| ... } -> new_hash
|
|
1749
|
+
# - to_h -> self or new_hash
|
|
1750
|
+
# -->
|
|
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:
|
|
1755
|
+
#
|
|
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
|
|
1762
|
+
# `self`.
|
|
1763
|
+
#
|
|
1764
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1765
|
+
#
|
|
1766
|
+
def to_h: () -> Hash[K, V]
|
|
1767
|
+
| [A, B] () { (K key, V value) -> _Pair[A, B] } -> Hash[A, B]
|
|
1768
|
+
|
|
1769
|
+
# <!--
|
|
1770
|
+
# rdoc-file=hash.c
|
|
1771
|
+
# - to_hash -> self
|
|
1772
|
+
# -->
|
|
1773
|
+
# Returns `self`.
|
|
1774
|
+
#
|
|
1775
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1776
|
+
#
|
|
1777
|
+
def to_hash: () -> self
|
|
1778
|
+
|
|
1779
|
+
# <!--
|
|
1780
|
+
# rdoc-file=hash.c
|
|
1781
|
+
# - to_proc -> proc
|
|
1782
|
+
# -->
|
|
1783
|
+
# Returns a Proc object that maps a key to its value:
|
|
1784
|
+
#
|
|
1785
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1786
|
+
# proc = h.to_proc
|
|
1787
|
+
# proc.class # => Proc
|
|
1788
|
+
# proc.call(:foo) # => 0
|
|
1789
|
+
# proc.call(:bar) # => 1
|
|
1790
|
+
# proc.call(:nosuch) # => nil
|
|
1791
|
+
#
|
|
1792
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1793
|
+
#
|
|
1794
|
+
def to_proc: () -> ^(K) -> V?
|
|
1795
|
+
|
|
1796
|
+
# <!-- rdoc-file=hash.c -->
|
|
1797
|
+
# Returns a new string containing the hash entries:
|
|
1798
|
+
#
|
|
1799
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1800
|
+
# h.inspect # => "{foo: 0, bar: 1, baz: 2}"
|
|
1801
|
+
#
|
|
1802
|
+
# Related: see [Methods for Converting](rdoc-ref:Hash@Methods+for+Converting).
|
|
1803
|
+
#
|
|
1804
|
+
alias to_s inspect
|
|
1805
|
+
|
|
1806
|
+
# <!--
|
|
1807
|
+
# rdoc-file=hash.c
|
|
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
|
|
1812
|
+
# -->
|
|
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.
|
|
1819
|
+
#
|
|
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:
|
|
1823
|
+
#
|
|
1824
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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}
|
|
1829
|
+
#
|
|
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`.
|
|
1832
|
+
#
|
|
1833
|
+
# For each key/value pair <code>old_key/old_value</code> in `self`, looks for
|
|
1834
|
+
# key `old_key` in `other_hash`:
|
|
1835
|
+
#
|
|
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:
|
|
1856
|
+
#
|
|
1857
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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).
|
|
1889
|
+
#
|
|
1890
|
+
def transform_keys: () -> Enumerator[K, Hash[untyped, V]]
|
|
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]
|
|
1894
|
+
|
|
1895
|
+
# <!--
|
|
1896
|
+
# rdoc-file=hash.c
|
|
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
|
|
1901
|
+
# -->
|
|
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).
|
|
1984
|
+
#
|
|
1985
|
+
def transform_keys!: () -> Enumerator[K, self]
|
|
1986
|
+
| (hash[K, K] replacements) ?{ (K key) -> K } -> self
|
|
1987
|
+
| () { (K key) -> K } -> self
|
|
1988
|
+
|
|
1989
|
+
# <!--
|
|
1990
|
+
# rdoc-file=hash.c
|
|
1991
|
+
# - transform_values {|value| ... } -> new_hash
|
|
1992
|
+
# - transform_values -> new_enumerator
|
|
1993
|
+
# -->
|
|
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`:
|
|
1997
|
+
#
|
|
1998
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
1999
|
+
# h1 = h.transform_values {|value| value * 100}
|
|
2000
|
+
# h1 # => {foo: 0, bar: 100, baz: 200}
|
|
2001
|
+
#
|
|
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).
|
|
2006
|
+
#
|
|
2007
|
+
def transform_values: () -> Enumerator[V, Hash[K, untyped]]
|
|
2008
|
+
| [V2] () { (V value) -> V2 } -> Hash[K, V2]
|
|
2009
|
+
|
|
2010
|
+
# <!--
|
|
2011
|
+
# rdoc-file=hash.c
|
|
2012
|
+
# - transform_values! {|old_value| ... } -> self
|
|
2013
|
+
# - transform_values! -> new_enumerator
|
|
2014
|
+
# -->
|
|
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>:
|
|
2021
|
+
#
|
|
2022
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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).
|
|
2029
|
+
#
|
|
2030
|
+
def transform_values!: () -> Enumerator[V, self]
|
|
2031
|
+
| () { (V value) -> V } -> self
|
|
2032
|
+
|
|
2033
|
+
# <!--
|
|
2034
|
+
# rdoc-file=hash.c
|
|
2035
|
+
# - update(*other_hashes) -> self
|
|
2036
|
+
# - update(*other_hashes) { |key, old_value, new_value| ... } -> self
|
|
2037
|
+
# -->
|
|
2038
|
+
# Updates values and/or adds entries to `self`; returns `self`.
|
|
2039
|
+
#
|
|
2040
|
+
# Each argument `other_hash` in `other_hashes` must be a hash.
|
|
2041
|
+
#
|
|
2042
|
+
# With no block given, for each successive entry `key`/`new_value` in each
|
|
2043
|
+
# successive `other_hash`:
|
|
2044
|
+
#
|
|
2045
|
+
# * If `key` is in `self`, sets <code>self[key] = new_value</code>, whose
|
|
2046
|
+
# position is unchanged:
|
|
2047
|
+
#
|
|
2048
|
+
# h0 = {foo: 0, bar: 1, baz: 2}
|
|
2049
|
+
# h1 = {bar: 3, foo: -1}
|
|
2050
|
+
# h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
|
|
2051
|
+
#
|
|
2052
|
+
# * If `key` is not in `self`, adds the entry at the end of `self`:
|
|
2053
|
+
#
|
|
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}
|
|
2056
|
+
#
|
|
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).
|
|
2077
|
+
#
|
|
2078
|
+
alias update merge!
|
|
2079
|
+
|
|
2080
|
+
# <!-- rdoc-file=hash.c -->
|
|
2081
|
+
# Returns whether `value` is a value in `self`.
|
|
2082
|
+
#
|
|
2083
|
+
# Related: [Methods for Querying](rdoc-ref:Hash@Methods+for+Querying).
|
|
2084
|
+
#
|
|
2085
|
+
alias value? has_value?
|
|
2086
|
+
|
|
2087
|
+
# <!--
|
|
2088
|
+
# rdoc-file=hash.c
|
|
2089
|
+
# - values -> new_array
|
|
2090
|
+
# -->
|
|
2091
|
+
# Returns a new array containing all values in `self`:
|
|
2092
|
+
#
|
|
2093
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
2094
|
+
# h.values # => [0, 1, 2]
|
|
2095
|
+
#
|
|
2096
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
2097
|
+
#
|
|
2098
|
+
def values: () -> Array[V]
|
|
2099
|
+
|
|
2100
|
+
# <!--
|
|
2101
|
+
# rdoc-file=hash.c
|
|
2102
|
+
# - values_at(*keys) -> new_array
|
|
2103
|
+
# -->
|
|
2104
|
+
# Returns a new array containing values for the given `keys`:
|
|
2105
|
+
#
|
|
2106
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
2107
|
+
# h.values_at(:baz, :foo) # => [2, 0]
|
|
2108
|
+
#
|
|
2109
|
+
# The [hash default](rdoc-ref:Hash@Hash+Default) is returned for each key that
|
|
2110
|
+
# is not found:
|
|
2111
|
+
#
|
|
2112
|
+
# h.values_at(:hello, :foo) # => [nil, 0]
|
|
2113
|
+
#
|
|
2114
|
+
# Related: see [Methods for Fetching](rdoc-ref:Hash@Methods+for+Fetching).
|
|
2115
|
+
#
|
|
2116
|
+
def values_at: (*_Key arg0) -> Array[V?]
|
|
2117
|
+
|
|
2118
|
+
private
|
|
2119
|
+
|
|
2120
|
+
# <!--
|
|
2121
|
+
# rdoc-file=hash.rb
|
|
2122
|
+
# - Hash.new(default_value = nil, capacity: 0) -> new_hash
|
|
2123
|
+
# - Hash.new(capacity: 0) {|self, key| ... } -> new_hash
|
|
2124
|
+
# -->
|
|
2125
|
+
# Returns a new empty Hash object.
|
|
2126
|
+
#
|
|
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).
|
|
2130
|
+
#
|
|
2131
|
+
# By default, a hash has `nil` values for both `default` and `default_proc`:
|
|
2132
|
+
#
|
|
2133
|
+
# h = Hash.new # => {}
|
|
2134
|
+
# h.default # => nil
|
|
2135
|
+
# h.default_proc # => nil
|
|
2136
|
+
#
|
|
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.
|
|
2151
|
+
#
|
|
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.
|
|
2155
|
+
#
|
|
2156
|
+
# See also [Methods for Creating a
|
|
2157
|
+
# Hash](rdoc-ref:Hash@Methods+for+Creating+a+Hash).
|
|
2158
|
+
#
|
|
2159
|
+
def initialize: (?V default_value, ?capacity: int) -> void
|
|
2160
|
+
| (?capacity: int) { (instance hash, _Key key) -> V } -> void
|
|
2161
|
+
|
|
2162
|
+
# <!--
|
|
2163
|
+
# rdoc-file=hash.c
|
|
2164
|
+
# - replace(other_hash) -> self
|
|
2165
|
+
# -->
|
|
2166
|
+
# Replaces the entire contents of `self` with the contents of `other_hash`;
|
|
2167
|
+
# returns `self`:
|
|
2168
|
+
#
|
|
2169
|
+
# h = {foo: 0, bar: 1, baz: 2}
|
|
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).
|
|
2181
|
+
#
|
|
2182
|
+
alias initialize_copy replace
|
|
2183
|
+
end
|