rbs-relaxed 3.9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/dependabot.yml +22 -0
- data/.github/workflows/comments.yml +35 -0
- data/.github/workflows/dependabot.yml +30 -0
- data/.github/workflows/ruby.yml +82 -0
- data/.github/workflows/typecheck.yml +38 -0
- data/.github/workflows/windows.yml +43 -0
- data/.gitignore +23 -0
- data/.rubocop.yml +68 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +1868 -0
- data/COPYING +56 -0
- data/README.md +203 -0
- data/Rakefile +417 -0
- data/Steepfile +44 -0
- data/config.yml +313 -0
- data/core/array.rbs +4062 -0
- data/core/basic_object.rbs +375 -0
- data/core/binding.rbs +150 -0
- data/core/builtin.rbs +277 -0
- data/core/class.rbs +220 -0
- data/core/comparable.rbs +171 -0
- data/core/complex.rbs +786 -0
- data/core/constants.rbs +96 -0
- data/core/data.rbs +415 -0
- data/core/dir.rbs +981 -0
- data/core/encoding.rbs +1371 -0
- data/core/enumerable.rbs +2405 -0
- data/core/enumerator/product.rbs +92 -0
- data/core/enumerator.rbs +630 -0
- data/core/env.rbs +6 -0
- data/core/errno.rbs +673 -0
- data/core/errors.rbs +760 -0
- data/core/exception.rbs +485 -0
- data/core/false_class.rbs +82 -0
- data/core/fiber.rbs +550 -0
- data/core/fiber_error.rbs +11 -0
- data/core/file.rbs +2936 -0
- data/core/file_test.rbs +331 -0
- data/core/float.rbs +1151 -0
- data/core/gc.rbs +644 -0
- data/core/global_variables.rbs +184 -0
- data/core/hash.rbs +1861 -0
- data/core/integer.rbs +1413 -0
- data/core/io/buffer.rbs +984 -0
- data/core/io/wait.rbs +70 -0
- data/core/io.rbs +3406 -0
- data/core/kernel.rbs +3096 -0
- data/core/marshal.rbs +207 -0
- data/core/match_data.rbs +635 -0
- data/core/math.rbs +729 -0
- data/core/method.rbs +386 -0
- data/core/module.rbs +1704 -0
- data/core/nil_class.rbs +209 -0
- data/core/numeric.rbs +818 -0
- data/core/object.rbs +110 -0
- data/core/object_space/weak_key_map.rbs +166 -0
- data/core/object_space.rbs +190 -0
- data/core/proc.rbs +868 -0
- data/core/process.rbs +2296 -0
- data/core/ractor.rbs +1068 -0
- data/core/random.rbs +237 -0
- data/core/range.rbs +1107 -0
- data/core/rational.rbs +531 -0
- data/core/rb_config.rbs +88 -0
- data/core/rbs/unnamed/argf.rbs +1229 -0
- data/core/rbs/unnamed/env_class.rbs +1209 -0
- data/core/rbs/unnamed/random.rbs +293 -0
- data/core/refinement.rbs +59 -0
- data/core/regexp.rbs +1930 -0
- data/core/ruby_vm.rbs +765 -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 +176 -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 +1171 -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 +294 -0
- data/core/set.rbs +621 -0
- data/core/signal.rbs +100 -0
- data/core/string.rbs +3583 -0
- data/core/struct.rbs +667 -0
- data/core/symbol.rbs +475 -0
- data/core/thread.rbs +1765 -0
- data/core/thread_group.rbs +79 -0
- data/core/time.rbs +1762 -0
- data/core/trace_point.rbs +477 -0
- data/core/true_class.rbs +98 -0
- data/core/unbound_method.rbs +329 -0
- data/core/warning.rbs +87 -0
- data/docs/CONTRIBUTING.md +106 -0
- data/docs/architecture.md +110 -0
- data/docs/collection.md +192 -0
- data/docs/data_and_struct.md +86 -0
- data/docs/gem.md +57 -0
- data/docs/rbs_by_example.md +309 -0
- data/docs/repo.md +125 -0
- data/docs/sigs.md +167 -0
- data/docs/stdlib.md +147 -0
- data/docs/syntax.md +910 -0
- data/docs/tools.md +17 -0
- data/exe/rbs +7 -0
- data/ext/rbs_extension/extconf.rb +15 -0
- data/ext/rbs_extension/lexer.c +2728 -0
- data/ext/rbs_extension/lexer.h +179 -0
- data/ext/rbs_extension/lexer.re +147 -0
- data/ext/rbs_extension/lexstate.c +175 -0
- data/ext/rbs_extension/location.c +325 -0
- data/ext/rbs_extension/location.h +85 -0
- data/ext/rbs_extension/main.c +33 -0
- data/ext/rbs_extension/parser.c +2973 -0
- data/ext/rbs_extension/parser.h +18 -0
- data/ext/rbs_extension/parserstate.c +397 -0
- data/ext/rbs_extension/parserstate.h +163 -0
- data/ext/rbs_extension/rbs_extension.h +31 -0
- data/ext/rbs_extension/unescape.c +32 -0
- data/goodcheck.yml +91 -0
- data/include/rbs/constants.h +82 -0
- data/include/rbs/ruby_objs.h +72 -0
- data/include/rbs/util/rbs_constant_pool.h +219 -0
- data/include/rbs.h +7 -0
- data/lib/rbs/ancestor_graph.rb +92 -0
- data/lib/rbs/annotate/annotations.rb +199 -0
- data/lib/rbs/annotate/formatter.rb +92 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +400 -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 +467 -0
- data/lib/rbs/ast/directives.rb +49 -0
- data/lib/rbs/ast/members.rb +451 -0
- data/lib/rbs/ast/type_param.rb +225 -0
- data/lib/rbs/ast/visitor.rb +137 -0
- data/lib/rbs/buffer.rb +67 -0
- data/lib/rbs/builtin_names.rb +58 -0
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +83 -0
- data/lib/rbs/cli/validate.rb +357 -0
- data/lib/rbs/cli.rb +1223 -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 +218 -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 +258 -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 +401 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +620 -0
- data/lib/rbs/definition_builder/method_builder.rb +254 -0
- data/lib/rbs/definition_builder.rb +845 -0
- data/lib/rbs/diff.rb +125 -0
- data/lib/rbs/environment/use_map.rb +77 -0
- data/lib/rbs/environment.rb +829 -0
- data/lib/rbs/environment_loader.rb +173 -0
- data/lib/rbs/environment_walker.rb +155 -0
- data/lib/rbs/errors.rb +645 -0
- data/lib/rbs/factory.rb +18 -0
- data/lib/rbs/file_finder.rb +28 -0
- data/lib/rbs/location_aux.rb +138 -0
- data/lib/rbs/locator.rb +243 -0
- data/lib/rbs/method_type.rb +143 -0
- data/lib/rbs/namespace.rb +125 -0
- data/lib/rbs/parser/lex_result.rb +15 -0
- data/lib/rbs/parser/token.rb +23 -0
- data/lib/rbs/parser_aux.rb +114 -0
- data/lib/rbs/prototype/helpers.rb +140 -0
- data/lib/rbs/prototype/node_usage.rb +99 -0
- data/lib/rbs/prototype/rb.rb +840 -0
- data/lib/rbs/prototype/rbi.rb +641 -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 +667 -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 +91 -0
- data/lib/rbs/sorter.rb +198 -0
- data/lib/rbs/substitution.rb +83 -0
- data/lib/rbs/subtractor.rb +201 -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 +435 -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 +109 -0
- data/lib/rbs/types.rb +1596 -0
- data/lib/rbs/unit_test/convertibles.rb +176 -0
- data/lib/rbs/unit_test/spy.rb +138 -0
- data/lib/rbs/unit_test/type_assertions.rb +347 -0
- data/lib/rbs/unit_test/with_aliases.rb +143 -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/writer.rb +424 -0
- data/lib/rbs.rb +94 -0
- data/lib/rdoc/discover.rb +20 -0
- data/lib/rdoc_plugin/parser.rb +163 -0
- data/rbs-relaxed.gemspec +48 -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 +36 -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 +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/annotation.rbs +27 -0
- data/sig/buffer.rbs +32 -0
- data/sig/builtin_names.rbs +44 -0
- data/sig/cli/colored_io.rbs +15 -0
- data/sig/cli/diff.rbs +21 -0
- data/sig/cli/validate.rbs +43 -0
- data/sig/cli.rbs +87 -0
- data/sig/collection/cleaner.rbs +13 -0
- data/sig/collection/config/lockfile.rbs +74 -0
- data/sig/collection/config/lockfile_generator.rbs +66 -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 +267 -0
- data/sig/definition.rbs +173 -0
- data/sig/definition_builder.rbs +165 -0
- data/sig/diff.rbs +28 -0
- data/sig/directives.rbs +77 -0
- data/sig/environment.rbs +279 -0
- data/sig/environment_loader.rbs +111 -0
- data/sig/environment_walker.rbs +65 -0
- data/sig/errors.rbs +405 -0
- data/sig/factory.rbs +5 -0
- data/sig/file_finder.rbs +28 -0
- data/sig/location.rbs +110 -0
- data/sig/locator.rbs +58 -0
- data/sig/manifest.yaml +7 -0
- data/sig/members.rbs +258 -0
- data/sig/method_builder.rbs +84 -0
- data/sig/method_types.rbs +58 -0
- data/sig/namespace.rbs +146 -0
- data/sig/parser.rbs +100 -0
- data/sig/prototype/helpers.rbs +27 -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 +35 -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/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 +110 -0
- data/sig/typename.rbs +79 -0
- data/sig/types.rbs +579 -0
- data/sig/unit_test/convertibles.rbs +154 -0
- data/sig/unit_test/spy.rbs +30 -0
- data/sig/unit_test/type_assertions.rbs +196 -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/writer.rbs +127 -0
- data/src/constants.c +153 -0
- data/src/ruby_objs.c +795 -0
- data/src/util/rbs_constant_pool.c +342 -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 +1629 -0
- data/stdlib/bigdecimal-math/0/big_math.rbs +119 -0
- data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
- data/stdlib/cgi/0/core.rbs +1285 -0
- data/stdlib/cgi/0/manifest.yaml +3 -0
- data/stdlib/coverage/0/coverage.rbs +263 -0
- data/stdlib/csv/0/csv.rbs +3776 -0
- data/stdlib/csv/0/manifest.yaml +3 -0
- data/stdlib/date/0/date.rbs +1585 -0
- data/stdlib/date/0/date_time.rbs +616 -0
- data/stdlib/date/0/time.rbs +26 -0
- data/stdlib/dbm/0/dbm.rbs +421 -0
- data/stdlib/delegate/0/delegator.rbs +184 -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 +343 -0
- data/stdlib/digest/0/digest.rbs +577 -0
- data/stdlib/erb/0/erb.rbs +532 -0
- data/stdlib/etc/0/etc.rbs +865 -0
- data/stdlib/fileutils/0/fileutils.rbs +1734 -0
- data/stdlib/find/0/find.rbs +49 -0
- data/stdlib/forwardable/0/forwardable.rbs +268 -0
- data/stdlib/io-console/0/io-console.rbs +414 -0
- data/stdlib/ipaddr/0/ipaddr.rbs +428 -0
- data/stdlib/json/0/json.rbs +1916 -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 +5552 -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 +487 -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 +393 -0
- data/stdlib/open3/0/open3.rbs +147 -0
- data/stdlib/openssl/0/manifest.yaml +3 -0
- data/stdlib/openssl/0/openssl.rbs +12113 -0
- data/stdlib/optparse/0/optparse.rbs +1725 -0
- data/stdlib/pathname/0/pathname.rbs +1406 -0
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +300 -0
- data/stdlib/prettyprint/0/prettyprint.rbs +383 -0
- data/stdlib/pstore/0/pstore.rbs +603 -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 +402 -0
- data/stdlib/psych/0/store.rbs +59 -0
- data/stdlib/pty/0/pty.rbs +237 -0
- data/stdlib/rdoc/0/code_object.rbs +51 -0
- data/stdlib/rdoc/0/comment.rbs +59 -0
- data/stdlib/rdoc/0/context.rbs +153 -0
- data/stdlib/rdoc/0/markup.rbs +117 -0
- data/stdlib/rdoc/0/parser.rbs +56 -0
- data/stdlib/rdoc/0/rdoc.rbs +391 -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 +1830 -0
- data/stdlib/ripper/0/ripper.rbs +1648 -0
- data/stdlib/securerandom/0/securerandom.rbs +62 -0
- data/stdlib/shellwords/0/shellwords.rbs +229 -0
- data/stdlib/singleton/0/singleton.rbs +131 -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 +92 -0
- data/stdlib/socket/0/socket.rbs +4157 -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 +79 -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 +567 -0
- data/stdlib/strscan/0/string_scanner.rbs +1627 -0
- data/stdlib/tempfile/0/tempfile.rbs +479 -0
- data/stdlib/time/0/time.rbs +432 -0
- data/stdlib/timeout/0/timeout.rbs +81 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +69 -0
- data/stdlib/tsort/0/cyclic.rbs +5 -0
- data/stdlib/tsort/0/interfaces.rbs +20 -0
- data/stdlib/tsort/0/tsort.rbs +409 -0
- data/stdlib/uri/0/common.rbs +582 -0
- data/stdlib/uri/0/file.rbs +118 -0
- data/stdlib/uri/0/ftp.rbs +13 -0
- data/stdlib/uri/0/generic.rbs +1108 -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 +210 -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 +166 -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 +200 -0
- metadata +532 -0
data/core/array.rbs
ADDED
@@ -0,0 +1,4062 @@
|
|
1
|
+
# <!-- rdoc-file=array.c -->
|
2
|
+
# An Array object is an ordered, integer-indexed collection of objects, called
|
3
|
+
# *elements*; the object represents an [array data
|
4
|
+
# structure](https://en.wikipedia.org/wiki/Array_(data_structure)).
|
5
|
+
#
|
6
|
+
# An element may be any object (even another array); elements may be any mixture
|
7
|
+
# of objects of different types.
|
8
|
+
#
|
9
|
+
# Important data structures that use arrays include:
|
10
|
+
#
|
11
|
+
# * [Coordinate vector](https://en.wikipedia.org/wiki/Coordinate_vector).
|
12
|
+
# * [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics)).
|
13
|
+
# * [Heap](https://en.wikipedia.org/wiki/Heap_(data_structure)).
|
14
|
+
# * [Hash table](https://en.wikipedia.org/wiki/Hash_table).
|
15
|
+
# * [Deque (double-ended
|
16
|
+
# queue)](https://en.wikipedia.org/wiki/Double-ended_queue).
|
17
|
+
# * [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
|
18
|
+
# * [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
|
19
|
+
#
|
20
|
+
# There are also array-like data structures:
|
21
|
+
#
|
22
|
+
# * [Associative array](https://en.wikipedia.org/wiki/Associative_array) (see
|
23
|
+
# Hash).
|
24
|
+
# * [Directory](https://en.wikipedia.org/wiki/Directory_(computing)) (see
|
25
|
+
# Dir).
|
26
|
+
# * [Environment](https://en.wikipedia.org/wiki/Environment_variable) (see
|
27
|
+
# ENV).
|
28
|
+
# * [Set](https://en.wikipedia.org/wiki/Set_(abstract_data_type)) (see Set).
|
29
|
+
# * [String](https://en.wikipedia.org/wiki/String_(computer_science)) (see
|
30
|
+
# String).
|
31
|
+
#
|
32
|
+
# ## Array Indexes
|
33
|
+
#
|
34
|
+
# Array indexing starts at 0, as in C or Java.
|
35
|
+
#
|
36
|
+
# A non-negative index is an offset from the first element:
|
37
|
+
#
|
38
|
+
# * Index 0 indicates the first element.
|
39
|
+
# * Index 1 indicates the second element.
|
40
|
+
# * ...
|
41
|
+
#
|
42
|
+
# A negative index is an offset, backwards, from the end of the array:
|
43
|
+
#
|
44
|
+
# * Index -1 indicates the last element.
|
45
|
+
# * Index -2 indicates the next-to-last element.
|
46
|
+
# * ...
|
47
|
+
#
|
48
|
+
# ### In-Range and Out-of-Range Indexes
|
49
|
+
#
|
50
|
+
# A non-negative index is *in range* if and only if it is smaller than the size
|
51
|
+
# of the array. For a 3-element array:
|
52
|
+
#
|
53
|
+
# * Indexes 0 through 2 are in range.
|
54
|
+
# * Index 3 is out of range.
|
55
|
+
#
|
56
|
+
# A negative index is *in range* if and only if its absolute value is not larger
|
57
|
+
# than the size of the array. For a 3-element array:
|
58
|
+
#
|
59
|
+
# * Indexes -1 through -3 are in range.
|
60
|
+
# * Index -4 is out of range.
|
61
|
+
#
|
62
|
+
# ### Effective Index
|
63
|
+
#
|
64
|
+
# Although the effective index into an array is always an integer, some methods
|
65
|
+
# (both within class Array and elsewhere) accept one or more non-integer
|
66
|
+
# arguments that are [integer-convertible
|
67
|
+
# objects](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
68
|
+
#
|
69
|
+
# ## Creating Arrays
|
70
|
+
#
|
71
|
+
# You can create an Array object explicitly with:
|
72
|
+
#
|
73
|
+
# * An [array literal](rdoc-ref:syntax/literals.rdoc@Array+Literals):
|
74
|
+
#
|
75
|
+
# [1, 'one', :one, [2, 'two', :two]]
|
76
|
+
#
|
77
|
+
# * A [%w or %W string-array
|
78
|
+
# Literal](rdoc-ref:syntax/literals.rdoc@25w+and+-25W-3A+String-Array+Litera
|
79
|
+
# ls):
|
80
|
+
#
|
81
|
+
# %w[foo bar baz] # => ["foo", "bar", "baz"]
|
82
|
+
# %w[1 % *] # => ["1", "%", "*"]
|
83
|
+
#
|
84
|
+
# * A [%i or %I symbol-array
|
85
|
+
# Literal](rdoc-ref:syntax/literals.rdoc@25i+and+-25I-3A+Symbol-Array+Litera
|
86
|
+
# ls):
|
87
|
+
#
|
88
|
+
# %i[foo bar baz] # => [:foo, :bar, :baz]
|
89
|
+
# %i[1 % *] # => [:"1", :%, :*]
|
90
|
+
#
|
91
|
+
# * Method Kernel#Array:
|
92
|
+
#
|
93
|
+
# Array(["a", "b"]) # => ["a", "b"]
|
94
|
+
# Array(1..5) # => [1, 2, 3, 4, 5]
|
95
|
+
# Array(key: :value) # => [[:key, :value]]
|
96
|
+
# Array(nil) # => []
|
97
|
+
# Array(1) # => [1]
|
98
|
+
# Array({:a => "a", :b => "b"}) # => [[:a, "a"], [:b, "b"]]
|
99
|
+
#
|
100
|
+
# * Method Array.new:
|
101
|
+
#
|
102
|
+
# Array.new # => []
|
103
|
+
# Array.new(3) # => [nil, nil, nil]
|
104
|
+
# Array.new(4) {Hash.new} # => [{}, {}, {}, {}]
|
105
|
+
# Array.new(3, true) # => [true, true, true]
|
106
|
+
#
|
107
|
+
# Note that the last example above populates the array with references to
|
108
|
+
# the same object. This is recommended only in cases where that object is a
|
109
|
+
# natively immutable object such as a symbol, a numeric, `nil`, `true`, or
|
110
|
+
# `false`.
|
111
|
+
#
|
112
|
+
# Another way to create an array with various objects, using a block; this
|
113
|
+
# usage is safe for mutable objects such as hashes, strings or other arrays:
|
114
|
+
#
|
115
|
+
# Array.new(4) {|i| i.to_s } # => ["0", "1", "2", "3"]
|
116
|
+
#
|
117
|
+
# Here is a way to create a multi-dimensional array:
|
118
|
+
#
|
119
|
+
# Array.new(3) {Array.new(3)}
|
120
|
+
# # => [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
|
121
|
+
#
|
122
|
+
# A number of Ruby methods, both in the core and in the standard library,
|
123
|
+
# provide instance method `to_a`, which converts an object to an array.
|
124
|
+
#
|
125
|
+
# * ARGF#to_a
|
126
|
+
# * Array#to_a
|
127
|
+
# * Enumerable#to_a
|
128
|
+
# * Hash#to_a
|
129
|
+
# * MatchData#to_a
|
130
|
+
# * NilClass#to_a
|
131
|
+
# * OptionParser#to_a
|
132
|
+
# * Range#to_a
|
133
|
+
# * Set#to_a
|
134
|
+
# * Struct#to_a
|
135
|
+
# * Time#to_a
|
136
|
+
# * Benchmark::Tms#to_a
|
137
|
+
# * CSV::Table#to_a
|
138
|
+
# * Enumerator::Lazy#to_a
|
139
|
+
# * Gem::List#to_a
|
140
|
+
# * Gem::NameTuple#to_a
|
141
|
+
# * Gem::Platform#to_a
|
142
|
+
# * Gem::RequestSet::Lockfile::Tokenizer#to_a
|
143
|
+
# * Gem::SourceList#to_a
|
144
|
+
# * OpenSSL::X509::Extension#to_a
|
145
|
+
# * OpenSSL::X509::Name#to_a
|
146
|
+
# * Racc::ISet#to_a
|
147
|
+
# * Rinda::RingFinger#to_a
|
148
|
+
# * Ripper::Lexer::Elem#to_a
|
149
|
+
# * RubyVM::InstructionSequence#to_a
|
150
|
+
# * YAML::DBM#to_a
|
151
|
+
#
|
152
|
+
# ## Example Usage
|
153
|
+
#
|
154
|
+
# In addition to the methods it mixes in through the Enumerable module, the
|
155
|
+
# `Array` class has proprietary methods for accessing, searching and otherwise
|
156
|
+
# manipulating arrays.
|
157
|
+
#
|
158
|
+
# Some of the more common ones are illustrated below.
|
159
|
+
#
|
160
|
+
# ## Accessing Elements
|
161
|
+
#
|
162
|
+
# Elements in an array can be retrieved using the Array#[] method. It can take
|
163
|
+
# a single integer argument (a numeric index), a pair of arguments (start and
|
164
|
+
# length) or a range. Negative indices start counting from the end, with -1
|
165
|
+
# being the last element.
|
166
|
+
#
|
167
|
+
# arr = [1, 2, 3, 4, 5, 6]
|
168
|
+
# arr[2] #=> 3
|
169
|
+
# arr[100] #=> nil
|
170
|
+
# arr[-3] #=> 4
|
171
|
+
# arr[2, 3] #=> [3, 4, 5]
|
172
|
+
# arr[1..4] #=> [2, 3, 4, 5]
|
173
|
+
# arr[1..-3] #=> [2, 3, 4]
|
174
|
+
#
|
175
|
+
# Another way to access a particular array element is by using the #at method
|
176
|
+
#
|
177
|
+
# arr.at(0) #=> 1
|
178
|
+
#
|
179
|
+
# The #slice method works in an identical manner to Array#[].
|
180
|
+
#
|
181
|
+
# To raise an error for indices outside of the array bounds or else to provide a
|
182
|
+
# default value when that happens, you can use #fetch.
|
183
|
+
#
|
184
|
+
# arr = ['a', 'b', 'c', 'd', 'e', 'f']
|
185
|
+
# arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6
|
186
|
+
# arr.fetch(100, "oops") #=> "oops"
|
187
|
+
#
|
188
|
+
# The special methods #first and #last will return the first and last elements
|
189
|
+
# of an array, respectively.
|
190
|
+
#
|
191
|
+
# arr.first #=> 1
|
192
|
+
# arr.last #=> 6
|
193
|
+
#
|
194
|
+
# To return the first `n` elements of an array, use #take
|
195
|
+
#
|
196
|
+
# arr.take(3) #=> [1, 2, 3]
|
197
|
+
#
|
198
|
+
# #drop does the opposite of #take, by returning the elements after `n` elements
|
199
|
+
# have been dropped:
|
200
|
+
#
|
201
|
+
# arr.drop(3) #=> [4, 5, 6]
|
202
|
+
#
|
203
|
+
# ## Obtaining Information about an `Array`
|
204
|
+
#
|
205
|
+
# Arrays keep track of their own length at all times. To query an array about
|
206
|
+
# the number of elements it contains, use #length, #count or #size.
|
207
|
+
#
|
208
|
+
# browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
|
209
|
+
# browsers.length #=> 5
|
210
|
+
# browsers.count #=> 5
|
211
|
+
#
|
212
|
+
# To check whether an array contains any elements at all
|
213
|
+
#
|
214
|
+
# browsers.empty? #=> false
|
215
|
+
#
|
216
|
+
# To check whether a particular item is included in the array
|
217
|
+
#
|
218
|
+
# browsers.include?('Konqueror') #=> false
|
219
|
+
#
|
220
|
+
# ## Adding Items to Arrays
|
221
|
+
#
|
222
|
+
# Items can be added to the end of an array by using either #push or #<<
|
223
|
+
#
|
224
|
+
# arr = [1, 2, 3, 4]
|
225
|
+
# arr.push(5) #=> [1, 2, 3, 4, 5]
|
226
|
+
# arr << 6 #=> [1, 2, 3, 4, 5, 6]
|
227
|
+
#
|
228
|
+
# #unshift will add a new item to the beginning of an array.
|
229
|
+
#
|
230
|
+
# arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
|
231
|
+
#
|
232
|
+
# With #insert you can add a new element to an array at any position.
|
233
|
+
#
|
234
|
+
# arr.insert(3, 'apple') #=> [0, 1, 2, 'apple', 3, 4, 5, 6]
|
235
|
+
#
|
236
|
+
# Using the #insert method, you can also insert multiple values at once:
|
237
|
+
#
|
238
|
+
# arr.insert(3, 'orange', 'pear', 'grapefruit')
|
239
|
+
# #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
|
240
|
+
#
|
241
|
+
# ## Removing Items from an `Array`
|
242
|
+
#
|
243
|
+
# The method #pop removes the last element in an array and returns it:
|
244
|
+
#
|
245
|
+
# arr = [1, 2, 3, 4, 5, 6]
|
246
|
+
# arr.pop #=> 6
|
247
|
+
# arr #=> [1, 2, 3, 4, 5]
|
248
|
+
#
|
249
|
+
# To retrieve and at the same time remove the first item, use #shift:
|
250
|
+
#
|
251
|
+
# arr.shift #=> 1
|
252
|
+
# arr #=> [2, 3, 4, 5]
|
253
|
+
#
|
254
|
+
# To delete an element at a particular index:
|
255
|
+
#
|
256
|
+
# arr.delete_at(2) #=> 4
|
257
|
+
# arr #=> [2, 3, 5]
|
258
|
+
#
|
259
|
+
# To delete a particular element anywhere in an array, use #delete:
|
260
|
+
#
|
261
|
+
# arr = [1, 2, 2, 3]
|
262
|
+
# arr.delete(2) #=> 2
|
263
|
+
# arr #=> [1,3]
|
264
|
+
#
|
265
|
+
# A useful method if you need to remove `nil` values from an array is #compact:
|
266
|
+
#
|
267
|
+
# arr = ['foo', 0, nil, 'bar', 7, 'baz', nil]
|
268
|
+
# arr.compact #=> ['foo', 0, 'bar', 7, 'baz']
|
269
|
+
# arr #=> ['foo', 0, nil, 'bar', 7, 'baz', nil]
|
270
|
+
# arr.compact! #=> ['foo', 0, 'bar', 7, 'baz']
|
271
|
+
# arr #=> ['foo', 0, 'bar', 7, 'baz']
|
272
|
+
#
|
273
|
+
# Another common need is to remove duplicate elements from an array.
|
274
|
+
#
|
275
|
+
# It has the non-destructive #uniq, and destructive method #uniq!
|
276
|
+
#
|
277
|
+
# arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
|
278
|
+
# arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
|
279
|
+
#
|
280
|
+
# ## Iterating over Arrays
|
281
|
+
#
|
282
|
+
# Like all classes that include the Enumerable module, `Array` has an each
|
283
|
+
# method, which defines what elements should be iterated over and how. In case
|
284
|
+
# of Array's #each, all elements in the `Array` instance are yielded to the
|
285
|
+
# supplied block in sequence.
|
286
|
+
#
|
287
|
+
# Note that this operation leaves the array unchanged.
|
288
|
+
#
|
289
|
+
# arr = [1, 2, 3, 4, 5]
|
290
|
+
# arr.each {|a| print a -= 10, " "}
|
291
|
+
# # prints: -9 -8 -7 -6 -5
|
292
|
+
# #=> [1, 2, 3, 4, 5]
|
293
|
+
#
|
294
|
+
# Another sometimes useful iterator is #reverse_each which will iterate over the
|
295
|
+
# elements in the array in reverse order.
|
296
|
+
#
|
297
|
+
# words = %w[first second third fourth fifth sixth]
|
298
|
+
# str = ""
|
299
|
+
# words.reverse_each {|word| str += "#{word} "}
|
300
|
+
# p str #=> "sixth fifth fourth third second first "
|
301
|
+
#
|
302
|
+
# The #map method can be used to create a new array based on the original array,
|
303
|
+
# but with the values modified by the supplied block:
|
304
|
+
#
|
305
|
+
# arr.map {|a| 2*a} #=> [2, 4, 6, 8, 10]
|
306
|
+
# arr #=> [1, 2, 3, 4, 5]
|
307
|
+
# arr.map! {|a| a**2} #=> [1, 4, 9, 16, 25]
|
308
|
+
# arr #=> [1, 4, 9, 16, 25]
|
309
|
+
#
|
310
|
+
# ## Selecting Items from an `Array`
|
311
|
+
#
|
312
|
+
# Elements can be selected from an array according to criteria defined in a
|
313
|
+
# block. The selection can happen in a destructive or a non-destructive manner.
|
314
|
+
# While the destructive operations will modify the array they were called on,
|
315
|
+
# the non-destructive methods usually return a new array with the selected
|
316
|
+
# elements, but leave the original array unchanged.
|
317
|
+
#
|
318
|
+
# ### Non-destructive Selection
|
319
|
+
#
|
320
|
+
# arr = [1, 2, 3, 4, 5, 6]
|
321
|
+
# arr.select {|a| a > 3} #=> [4, 5, 6]
|
322
|
+
# arr.reject {|a| a < 3} #=> [3, 4, 5, 6]
|
323
|
+
# arr.drop_while {|a| a < 4} #=> [4, 5, 6]
|
324
|
+
# arr #=> [1, 2, 3, 4, 5, 6]
|
325
|
+
#
|
326
|
+
# ### Destructive Selection
|
327
|
+
#
|
328
|
+
# #select! and #reject! are the corresponding destructive methods to #select and
|
329
|
+
# #reject
|
330
|
+
#
|
331
|
+
# Similar to #select vs. #reject, #delete_if and #keep_if have the exact
|
332
|
+
# opposite result when supplied with the same block:
|
333
|
+
#
|
334
|
+
# arr.delete_if {|a| a < 4} #=> [4, 5, 6]
|
335
|
+
# arr #=> [4, 5, 6]
|
336
|
+
#
|
337
|
+
# arr = [1, 2, 3, 4, 5, 6]
|
338
|
+
# arr.keep_if {|a| a < 4} #=> [1, 2, 3]
|
339
|
+
# arr #=> [1, 2, 3]
|
340
|
+
#
|
341
|
+
# ## What's Here
|
342
|
+
#
|
343
|
+
# First, what's elsewhere. Class `Array`:
|
344
|
+
#
|
345
|
+
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
|
346
|
+
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
|
347
|
+
# provides dozens of additional methods.
|
348
|
+
#
|
349
|
+
# Here, class `Array` provides methods that are useful for:
|
350
|
+
#
|
351
|
+
# * [Creating an Array](rdoc-ref:Array@Methods+for+Creating+an+Array)
|
352
|
+
# * [Querying](rdoc-ref:Array@Methods+for+Querying)
|
353
|
+
# * [Comparing](rdoc-ref:Array@Methods+for+Comparing)
|
354
|
+
# * [Fetching](rdoc-ref:Array@Methods+for+Fetching)
|
355
|
+
# * [Assigning](rdoc-ref:Array@Methods+for+Assigning)
|
356
|
+
# * [Deleting](rdoc-ref:Array@Methods+for+Deleting)
|
357
|
+
# * [Combining](rdoc-ref:Array@Methods+for+Combining)
|
358
|
+
# * [Iterating](rdoc-ref:Array@Methods+for+Iterating)
|
359
|
+
# * [Converting](rdoc-ref:Array@Methods+for+Converting)
|
360
|
+
# * [And more....](rdoc-ref:Array@Other+Methods)
|
361
|
+
#
|
362
|
+
# ### Methods for Creating an `Array`
|
363
|
+
#
|
364
|
+
# * ::[]: Returns a new array populated with given objects.
|
365
|
+
# * ::new: Returns a new array.
|
366
|
+
# * ::try_convert: Returns a new array created from a given object.
|
367
|
+
#
|
368
|
+
# See also [Creating Arrays](rdoc-ref:Array@Creating+Arrays).
|
369
|
+
#
|
370
|
+
# ### Methods for Querying
|
371
|
+
#
|
372
|
+
# * #all?: Returns whether all elements meet a given criterion.
|
373
|
+
# * #any?: Returns whether any element meets a given criterion.
|
374
|
+
# * #count: Returns the count of elements that meet a given criterion.
|
375
|
+
# * #empty?: Returns whether there are no elements.
|
376
|
+
# * #find_index (aliased as #index): Returns the index of the first element
|
377
|
+
# that meets a given criterion.
|
378
|
+
# * #hash: Returns the integer hash code.
|
379
|
+
# * #include?: Returns whether any element `==` a given object.
|
380
|
+
# * #length (aliased as #size): Returns the count of elements.
|
381
|
+
# * #none?: Returns whether no element `==` a given object.
|
382
|
+
# * #one?: Returns whether exactly one element `==` a given object.
|
383
|
+
# * #rindex: Returns the index of the last element that meets a given
|
384
|
+
# criterion.
|
385
|
+
#
|
386
|
+
# ### Methods for Comparing
|
387
|
+
#
|
388
|
+
# * #<=>: Returns -1, 0, or 1, as `self` is less than, equal to, or greater
|
389
|
+
# than a given object.
|
390
|
+
# * #==: Returns whether each element in `self` is `==` to the corresponding
|
391
|
+
# element in a given object.
|
392
|
+
# * #eql?: Returns whether each element in `self` is `eql?` to the
|
393
|
+
# corresponding element in a given object.
|
394
|
+
#
|
395
|
+
# ### Methods for Fetching
|
396
|
+
#
|
397
|
+
# These methods do not modify `self`.
|
398
|
+
#
|
399
|
+
# * #[] (aliased as #slice): Returns consecutive elements as determined by a
|
400
|
+
# given argument.
|
401
|
+
# * #assoc: Returns the first element that is an array whose first element
|
402
|
+
# `==` a given object.
|
403
|
+
# * #at: Returns the element at a given offset.
|
404
|
+
# * #bsearch: Returns an element selected via a binary search as determined by
|
405
|
+
# a given block.
|
406
|
+
# * #bsearch_index: Returns the index of an element selected via a binary
|
407
|
+
# search as determined by a given block.
|
408
|
+
# * #compact: Returns an array containing all non-`nil` elements.
|
409
|
+
# * #dig: Returns the object in nested objects that is specified by a given
|
410
|
+
# index and additional arguments.
|
411
|
+
# * #drop: Returns trailing elements as determined by a given index.
|
412
|
+
# * #drop_while: Returns trailing elements as determined by a given block.
|
413
|
+
# * #fetch: Returns the element at a given offset.
|
414
|
+
# * #fetch_values: Returns elements at given offsets.
|
415
|
+
# * #first: Returns one or more leading elements.
|
416
|
+
# * #last: Returns one or more trailing elements.
|
417
|
+
# * #max: Returns one or more maximum-valued elements, as determined by `#<=>`
|
418
|
+
# or a given block.
|
419
|
+
# * #min: Returns one or more minimum-valued elements, as determined by `#<=>`
|
420
|
+
# or a given block.
|
421
|
+
# * #minmax: Returns the minimum-valued and maximum-valued elements, as
|
422
|
+
# determined by `#<=>` or a given block.
|
423
|
+
# * #rassoc: Returns the first element that is an array whose second element
|
424
|
+
# `==` a given object.
|
425
|
+
# * #reject: Returns an array containing elements not rejected by a given
|
426
|
+
# block.
|
427
|
+
# * #reverse: Returns all elements in reverse order.
|
428
|
+
# * #rotate: Returns all elements with some rotated from one end to the other.
|
429
|
+
# * #sample: Returns one or more random elements.
|
430
|
+
# * #select (aliased as #filter): Returns an array containing elements
|
431
|
+
# selected by a given block.
|
432
|
+
# * #shuffle: Returns elements in a random order.
|
433
|
+
# * #sort: Returns all elements in an order determined by `#<=>` or a given
|
434
|
+
# block.
|
435
|
+
# * #take: Returns leading elements as determined by a given index.
|
436
|
+
# * #take_while: Returns leading elements as determined by a given block.
|
437
|
+
# * #uniq: Returns an array containing non-duplicate elements.
|
438
|
+
# * #values_at: Returns the elements at given offsets.
|
439
|
+
#
|
440
|
+
# ### Methods for Assigning
|
441
|
+
#
|
442
|
+
# These methods add, replace, or reorder elements in `self`.
|
443
|
+
#
|
444
|
+
# * #<<: Appends an element.
|
445
|
+
# * #[]=: Assigns specified elements with a given object.
|
446
|
+
# * #concat: Appends all elements from given arrays.
|
447
|
+
# * #fill: Replaces specified elements with specified objects.
|
448
|
+
# * #flatten!: Replaces each nested array in `self` with the elements from
|
449
|
+
# that array.
|
450
|
+
# * #initialize_copy (aliased as #replace): Replaces the content of `self`
|
451
|
+
# with the content of a given array.
|
452
|
+
# * #insert: Inserts given objects at a given offset; does not replace
|
453
|
+
# elements.
|
454
|
+
# * #push (aliased as #append): Appends elements.
|
455
|
+
# * #reverse!: Replaces `self` with its elements reversed.
|
456
|
+
# * #rotate!: Replaces `self` with its elements rotated.
|
457
|
+
# * #shuffle!: Replaces `self` with its elements in random order.
|
458
|
+
# * #sort!: Replaces `self` with its elements sorted, as determined by `#<=>`
|
459
|
+
# or a given block.
|
460
|
+
# * #sort_by!: Replaces `self` with its elements sorted, as determined by a
|
461
|
+
# given block.
|
462
|
+
# * #unshift (aliased as #prepend): Prepends leading elements.
|
463
|
+
#
|
464
|
+
# ### Methods for Deleting
|
465
|
+
#
|
466
|
+
# Each of these methods removes elements from `self`:
|
467
|
+
#
|
468
|
+
# * #clear: Removes all elements.
|
469
|
+
# * #compact!: Removes all `nil` elements.
|
470
|
+
# * #delete: Removes elements equal to a given object.
|
471
|
+
# * #delete_at: Removes the element at a given offset.
|
472
|
+
# * #delete_if: Removes elements specified by a given block.
|
473
|
+
# * #keep_if: Removes elements not specified by a given block.
|
474
|
+
# * #pop: Removes and returns the last element.
|
475
|
+
# * #reject!: Removes elements specified by a given block.
|
476
|
+
# * #select! (aliased as #filter!): Removes elements not specified by a given
|
477
|
+
# block.
|
478
|
+
# * #shift: Removes and returns the first element.
|
479
|
+
# * #slice!: Removes and returns a sequence of elements.
|
480
|
+
# * #uniq!: Removes duplicates.
|
481
|
+
#
|
482
|
+
# ### Methods for Combining
|
483
|
+
#
|
484
|
+
# * #&: Returns an array containing elements found both in `self` and a given
|
485
|
+
# array.
|
486
|
+
# * #+: Returns an array containing all elements of `self` followed by all
|
487
|
+
# elements of a given array.
|
488
|
+
# * #-: Returns an array containing all elements of `self` that are not found
|
489
|
+
# in a given array.
|
490
|
+
# * #|: Returns an array containing all element of `self` and all elements of
|
491
|
+
# a given array, duplicates removed.
|
492
|
+
# * #difference: Returns an array containing all elements of `self` that are
|
493
|
+
# not found in any of the given arrays..
|
494
|
+
# * #intersection: Returns an array containing elements found both in `self`
|
495
|
+
# and in each given array.
|
496
|
+
# * #product: Returns or yields all combinations of elements from `self` and
|
497
|
+
# given arrays.
|
498
|
+
# * #reverse: Returns an array containing all elements of `self` in reverse
|
499
|
+
# order.
|
500
|
+
# * #union: Returns an array containing all elements of `self` and all
|
501
|
+
# elements of given arrays, duplicates removed.
|
502
|
+
#
|
503
|
+
# ### Methods for Iterating
|
504
|
+
#
|
505
|
+
# * #combination: Calls a given block with combinations of elements of `self`;
|
506
|
+
# a combination does not use the same element more than once.
|
507
|
+
# * #cycle: Calls a given block with each element, then does so again, for a
|
508
|
+
# specified number of times, or forever.
|
509
|
+
# * #each: Passes each element to a given block.
|
510
|
+
# * #each_index: Passes each element index to a given block.
|
511
|
+
# * #permutation: Calls a given block with permutations of elements of `self`;
|
512
|
+
# a permutation does not use the same element more than once.
|
513
|
+
# * #repeated_combination: Calls a given block with combinations of elements
|
514
|
+
# of `self`; a combination may use the same element more than once.
|
515
|
+
# * #repeated_permutation: Calls a given block with permutations of elements
|
516
|
+
# of `self`; a permutation may use the same element more than once.
|
517
|
+
# * #reverse_each: Passes each element, in reverse order, to a given block.
|
518
|
+
#
|
519
|
+
# ### Methods for Converting
|
520
|
+
#
|
521
|
+
# * #collect (aliased as #map): Returns an array containing the block
|
522
|
+
# return-value for each element.
|
523
|
+
# * #collect! (aliased as #map!): Replaces each element with a block
|
524
|
+
# return-value.
|
525
|
+
# * #flatten: Returns an array that is a recursive flattening of `self`.
|
526
|
+
# * #inspect (aliased as #to_s): Returns a new String containing the elements.
|
527
|
+
# * #join: Returns a newsString containing the elements joined by the field
|
528
|
+
# separator.
|
529
|
+
# * #to_a: Returns `self` or a new array containing all elements.
|
530
|
+
# * #to_ary: Returns `self`.
|
531
|
+
# * #to_h: Returns a new hash formed from the elements.
|
532
|
+
# * #transpose: Transposes `self`, which must be an array of arrays.
|
533
|
+
# * #zip: Returns a new array of arrays containing `self` and given arrays.
|
534
|
+
#
|
535
|
+
# ### Other Methods
|
536
|
+
#
|
537
|
+
# * #*: Returns one of the following:
|
538
|
+
#
|
539
|
+
# * With integer argument `n`, a new array that is the concatenation of
|
540
|
+
# `n` copies of `self`.
|
541
|
+
# * With string argument `field_separator`, a new string that is
|
542
|
+
# equivalent to `join(field_separator)`.
|
543
|
+
#
|
544
|
+
# * #pack: Packs the elements into a binary sequence.
|
545
|
+
# * #sum: Returns a sum of elements according to either `+` or a given block.
|
546
|
+
#
|
547
|
+
%a{annotate:rdoc:source:from=array.c}
|
548
|
+
class Array[unchecked out Elem] < Object
|
549
|
+
include Enumerable[Elem]
|
550
|
+
|
551
|
+
# <!--
|
552
|
+
# rdoc-file=array.c
|
553
|
+
# - Array.new -> new_empty_array
|
554
|
+
# - Array.new(array) -> new_array
|
555
|
+
# - Array.new(size, default_value = nil) -> new_array
|
556
|
+
# - Array.new(size = 0) {|index| ... } -> new_array
|
557
|
+
# -->
|
558
|
+
# Returns a new array.
|
559
|
+
#
|
560
|
+
# With no block and no argument given, returns a new empty array:
|
561
|
+
#
|
562
|
+
# Array.new # => []
|
563
|
+
#
|
564
|
+
# With no block and array argument given, returns a new array with the same
|
565
|
+
# elements:
|
566
|
+
#
|
567
|
+
# Array.new([:foo, 'bar', 2]) # => [:foo, "bar", 2]
|
568
|
+
#
|
569
|
+
# With no block and integer argument given, returns a new array containing that
|
570
|
+
# many instances of the given `default_value`:
|
571
|
+
#
|
572
|
+
# Array.new(0) # => []
|
573
|
+
# Array.new(3) # => [nil, nil, nil]
|
574
|
+
# Array.new(2, 3) # => [3, 3]
|
575
|
+
#
|
576
|
+
# With a block given, returns an array of the given `size`; calls the block with
|
577
|
+
# each `index` in the range `(0...size)`; the element at that `index` in the
|
578
|
+
# returned array is the blocks return value:
|
579
|
+
#
|
580
|
+
# Array.new(3) {|index| "Element #{index}" } # => ["Element 0", "Element 1", "Element 2"]
|
581
|
+
#
|
582
|
+
# A common pitfall for new Rubyists is providing an expression as
|
583
|
+
# `default_value`:
|
584
|
+
#
|
585
|
+
# array = Array.new(2, {})
|
586
|
+
# array # => [{}, {}]
|
587
|
+
# array[0][:a] = 1
|
588
|
+
# array # => [{a: 1}, {a: 1}], as array[0] and array[1] are same object
|
589
|
+
#
|
590
|
+
# If you want the elements of the array to be distinct, you should pass a block:
|
591
|
+
#
|
592
|
+
# array = Array.new(2) { {} }
|
593
|
+
# array # => [{}, {}]
|
594
|
+
# array[0][:a] = 1
|
595
|
+
# array # => [{a: 1}, {}], as array[0] and array[1] are different objects
|
596
|
+
#
|
597
|
+
# Raises TypeError if the first argument is not either an array or an
|
598
|
+
# [integer-convertible
|
599
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects)).
|
600
|
+
# Raises ArgumentError if the first argument is a negative integer.
|
601
|
+
#
|
602
|
+
# Related: see [Methods for Creating an
|
603
|
+
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
|
604
|
+
#
|
605
|
+
def initialize: () -> void
|
606
|
+
| (::Array[Elem] ary) -> void
|
607
|
+
| (int size, ?Elem val) -> void
|
608
|
+
| (int size) { (::Integer index) -> Elem } -> void
|
609
|
+
|
610
|
+
# <!--
|
611
|
+
# rdoc-file=array.c
|
612
|
+
# - [](*args)
|
613
|
+
# -->
|
614
|
+
# Returns a new array, populated with the given objects:
|
615
|
+
#
|
616
|
+
# Array[1, 'a', /^A/] # => [1, "a", /^A/]
|
617
|
+
# Array[] # => []
|
618
|
+
# Array.[](1, 'a', /^A/) # => [1, "a", /^A/]
|
619
|
+
#
|
620
|
+
# Related: see [Methods for Creating an
|
621
|
+
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
|
622
|
+
#
|
623
|
+
def self.[]: [U] (*U) -> ::Array[U]
|
624
|
+
|
625
|
+
# <!--
|
626
|
+
# rdoc-file=array.c
|
627
|
+
# - Array.try_convert(object) -> object, new_array, or nil
|
628
|
+
# -->
|
629
|
+
# Attempts to return an array, based on the given `object`.
|
630
|
+
#
|
631
|
+
# If `object` is an array, returns `object`.
|
632
|
+
#
|
633
|
+
# Otherwise if `object` responds to `:to_ary`. calls `object.to_ary`: if the
|
634
|
+
# return value is an array or `nil`, returns that value; if not, raises
|
635
|
+
# TypeError.
|
636
|
+
#
|
637
|
+
# Otherwise returns `nil`.
|
638
|
+
#
|
639
|
+
# Related: see [Methods for Creating an
|
640
|
+
# Array](rdoc-ref:Array@Methods+for+Creating+an+Array).
|
641
|
+
#
|
642
|
+
def self.try_convert: [U] (untyped) -> ::Array[U]?
|
643
|
+
|
644
|
+
# <!--
|
645
|
+
# rdoc-file=array.c
|
646
|
+
# - self & other_array -> new_array
|
647
|
+
# -->
|
648
|
+
# Returns a new array containing the *intersection* of `self` and `other_array`;
|
649
|
+
# that is, containing those elements found in both `self` and `other_array`:
|
650
|
+
#
|
651
|
+
# [0, 1, 2, 3] & [1, 2] # => [1, 2]
|
652
|
+
#
|
653
|
+
# Omits duplicates:
|
654
|
+
#
|
655
|
+
# [0, 1, 1, 0] & [0, 1] # => [0, 1]
|
656
|
+
#
|
657
|
+
# Preserves order from `self`:
|
658
|
+
#
|
659
|
+
# [0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
|
660
|
+
#
|
661
|
+
# Identifies common elements using method `#eql?` (as defined in each element of
|
662
|
+
# `self`).
|
663
|
+
#
|
664
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
665
|
+
#
|
666
|
+
def &: (::Array[untyped] | _ToAry[untyped]) -> ::Array[Elem]
|
667
|
+
|
668
|
+
# <!--
|
669
|
+
# rdoc-file=array.c
|
670
|
+
# - self * n -> new_array
|
671
|
+
# - self * string_separator -> new_string
|
672
|
+
# -->
|
673
|
+
# When non-negative integer argument `n` is given, returns a new array built by
|
674
|
+
# concatenating `n` copies of `self`:
|
675
|
+
#
|
676
|
+
# a = ['x', 'y']
|
677
|
+
# a * 3 # => ["x", "y", "x", "y", "x", "y"]
|
678
|
+
#
|
679
|
+
# When string argument `string_separator` is given, equivalent to
|
680
|
+
# `self.join(string_separator)`:
|
681
|
+
#
|
682
|
+
# [0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {foo: 0}"
|
683
|
+
#
|
684
|
+
def *: (string str) -> ::String
|
685
|
+
| (int int) -> ::Array[Elem]
|
686
|
+
|
687
|
+
# <!--
|
688
|
+
# rdoc-file=array.c
|
689
|
+
# - self + other_array -> new_array
|
690
|
+
# -->
|
691
|
+
# Returns a new array containing all elements of `self` followed by all elements
|
692
|
+
# of `other_array`:
|
693
|
+
#
|
694
|
+
# a = [0, 1] + [2, 3]
|
695
|
+
# a # => [0, 1, 2, 3]
|
696
|
+
#
|
697
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
698
|
+
#
|
699
|
+
def +: [U] (_ToAry[U]) -> ::Array[Elem | U]
|
700
|
+
|
701
|
+
# <!--
|
702
|
+
# rdoc-file=array.c
|
703
|
+
# - self - other_array -> new_array
|
704
|
+
# -->
|
705
|
+
# Returns a new array containing only those elements of `self` that are not
|
706
|
+
# found in `other_array`; the order from `self` is preserved:
|
707
|
+
#
|
708
|
+
# [0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3]
|
709
|
+
# [0, 1, 1, 2, 1, 1, 3, 1, 1] - [3, 2, 0, :foo] # => [1, 1, 1, 1, 1, 1]
|
710
|
+
# [0, 1, 2] - [:foo] # => [0, 1, 2]
|
711
|
+
#
|
712
|
+
# Element are compared using method `#eql?` (as defined in each element of
|
713
|
+
# `self`).
|
714
|
+
#
|
715
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
716
|
+
#
|
717
|
+
def -: (_ToAry[untyped]) -> ::Array[Elem]
|
718
|
+
|
719
|
+
# <!--
|
720
|
+
# rdoc-file=array.c
|
721
|
+
# - self << object -> self
|
722
|
+
# -->
|
723
|
+
# Appends `object` as the last element in `self`; returns `self`:
|
724
|
+
#
|
725
|
+
# [:foo, 'bar', 2] << :baz # => [:foo, "bar", 2, :baz]
|
726
|
+
#
|
727
|
+
# Appends `object` as a single element, even if it is another array:
|
728
|
+
#
|
729
|
+
# [:foo, 'bar', 2] << [3, 4] # => [:foo, "bar", 2, [3, 4]]
|
730
|
+
#
|
731
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
732
|
+
#
|
733
|
+
def <<: (Elem) -> self
|
734
|
+
|
735
|
+
# <!--
|
736
|
+
# rdoc-file=array.c
|
737
|
+
# - self <=> other_array -> -1, 0, or 1
|
738
|
+
# -->
|
739
|
+
# Returns -1, 0, or 1 as `self` is determined to be less than, equal to, or
|
740
|
+
# greater than `other_array`.
|
741
|
+
#
|
742
|
+
# Iterates over each index `i` in `(0...self.size)`:
|
743
|
+
#
|
744
|
+
# * Computes `result[i]` as `self[i] <=> other_array[i]`.
|
745
|
+
# * Immediately returns 1 if `result[i]` is 1:
|
746
|
+
#
|
747
|
+
# [0, 1, 2] <=> [0, 0, 2] # => 1
|
748
|
+
#
|
749
|
+
# * Immediately returns -1 if `result[i]` is -1:
|
750
|
+
#
|
751
|
+
# [0, 1, 2] <=> [0, 2, 2] # => -1
|
752
|
+
#
|
753
|
+
# * Continues if `result[i]` is 0.
|
754
|
+
#
|
755
|
+
# When every `result` is 0, returns `self.size <=> other_array.size` (see
|
756
|
+
# Integer#<=>):
|
757
|
+
#
|
758
|
+
# [0, 1, 2] <=> [0, 1] # => 1
|
759
|
+
# [0, 1, 2] <=> [0, 1, 2] # => 0
|
760
|
+
# [0, 1, 2] <=> [0, 1, 2, 3] # => -1
|
761
|
+
#
|
762
|
+
# Note that when `other_array` is larger than `self`, its trailing elements do
|
763
|
+
# not affect the result:
|
764
|
+
#
|
765
|
+
# [0, 1, 2] <=> [0, 1, 2, -3] # => -1
|
766
|
+
# [0, 1, 2] <=> [0, 1, 2, 0] # => -1
|
767
|
+
# [0, 1, 2] <=> [0, 1, 2, 3] # => -1
|
768
|
+
#
|
769
|
+
# Related: see [Methods for Comparing](rdoc-ref:Array@Methods+for+Comparing).
|
770
|
+
#
|
771
|
+
def <=>: (untyped) -> ::Integer?
|
772
|
+
|
773
|
+
# <!--
|
774
|
+
# rdoc-file=array.c
|
775
|
+
# - self == other_array -> true or false
|
776
|
+
# -->
|
777
|
+
# Returns whether both:
|
778
|
+
#
|
779
|
+
# * `self` and `other_array` are the same size.
|
780
|
+
# * Their corresponding elements are the same; that is, for each index `i` in
|
781
|
+
# `(0...self.size)`, `self[i] == other_array[i]`.
|
782
|
+
#
|
783
|
+
# Examples:
|
784
|
+
#
|
785
|
+
# [:foo, 'bar', 2] == [:foo, 'bar', 2] # => true
|
786
|
+
# [:foo, 'bar', 2] == [:foo, 'bar', 2.0] # => true
|
787
|
+
# [:foo, 'bar', 2] == [:foo, 'bar'] # => false # Different sizes.
|
788
|
+
# [:foo, 'bar', 2] == [:foo, 'bar', 3] # => false # Different elements.
|
789
|
+
#
|
790
|
+
# This method is different from method Array#eql?, which compares elements using
|
791
|
+
# `Object#eql?`.
|
792
|
+
#
|
793
|
+
# Related: see [Methods for Comparing](rdoc-ref:Array@Methods+for+Comparing).
|
794
|
+
#
|
795
|
+
def ==: (untyped other) -> bool
|
796
|
+
|
797
|
+
# <!--
|
798
|
+
# rdoc-file=array.c
|
799
|
+
# - self[index] -> object or nil
|
800
|
+
# - self[start, length] -> object or nil
|
801
|
+
# - self[range] -> object or nil
|
802
|
+
# - self[aseq] -> object or nil
|
803
|
+
# - slice(index) -> object or nil
|
804
|
+
# - slice(start, length) -> object or nil
|
805
|
+
# - slice(range) -> object or nil
|
806
|
+
# - slice(aseq) -> object or nil
|
807
|
+
# -->
|
808
|
+
# Returns elements from `self`; does not modify `self`.
|
809
|
+
#
|
810
|
+
# In brief:
|
811
|
+
#
|
812
|
+
# a = [:foo, 'bar', 2]
|
813
|
+
#
|
814
|
+
# # Single argument index: returns one element.
|
815
|
+
# a[0] # => :foo # Zero-based index.
|
816
|
+
# a[-1] # => 2 # Negative index counts backwards from end.
|
817
|
+
#
|
818
|
+
# # Arguments start and length: returns an array.
|
819
|
+
# a[1, 2] # => ["bar", 2]
|
820
|
+
# a[-2, 2] # => ["bar", 2] # Negative start counts backwards from end.
|
821
|
+
#
|
822
|
+
# # Single argument range: returns an array.
|
823
|
+
# a[0..1] # => [:foo, "bar"]
|
824
|
+
# a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end.
|
825
|
+
# a[-2..2] # => ["bar", 2] # Negative range-end counts backwards from end.
|
826
|
+
#
|
827
|
+
# When a single integer argument `index` is given, returns the element at offset
|
828
|
+
# `index`:
|
829
|
+
#
|
830
|
+
# a = [:foo, 'bar', 2]
|
831
|
+
# a[0] # => :foo
|
832
|
+
# a[2] # => 2
|
833
|
+
# a # => [:foo, "bar", 2]
|
834
|
+
#
|
835
|
+
# If `index` is negative, counts backwards from the end of `self`:
|
836
|
+
#
|
837
|
+
# a = [:foo, 'bar', 2]
|
838
|
+
# a[-1] # => 2
|
839
|
+
# a[-2] # => "bar"
|
840
|
+
#
|
841
|
+
# If `index` is out of range, returns `nil`.
|
842
|
+
#
|
843
|
+
# When two Integer arguments `start` and `length` are given, returns a new
|
844
|
+
# `Array` of size `length` containing successive elements beginning at offset
|
845
|
+
# `start`:
|
846
|
+
#
|
847
|
+
# a = [:foo, 'bar', 2]
|
848
|
+
# a[0, 2] # => [:foo, "bar"]
|
849
|
+
# a[1, 2] # => ["bar", 2]
|
850
|
+
#
|
851
|
+
# If `start + length` is greater than `self.length`, returns all elements from
|
852
|
+
# offset `start` to the end:
|
853
|
+
#
|
854
|
+
# a = [:foo, 'bar', 2]
|
855
|
+
# a[0, 4] # => [:foo, "bar", 2]
|
856
|
+
# a[1, 3] # => ["bar", 2]
|
857
|
+
# a[2, 2] # => [2]
|
858
|
+
#
|
859
|
+
# If `start == self.size` and `length >= 0`, returns a new empty `Array`.
|
860
|
+
#
|
861
|
+
# If `length` is negative, returns `nil`.
|
862
|
+
#
|
863
|
+
# When a single Range argument `range` is given, treats `range.min` as `start`
|
864
|
+
# above and `range.size` as `length` above:
|
865
|
+
#
|
866
|
+
# a = [:foo, 'bar', 2]
|
867
|
+
# a[0..1] # => [:foo, "bar"]
|
868
|
+
# a[1..2] # => ["bar", 2]
|
869
|
+
#
|
870
|
+
# Special case: If `range.start == a.size`, returns a new empty `Array`.
|
871
|
+
#
|
872
|
+
# If `range.end` is negative, calculates the end index from the end:
|
873
|
+
#
|
874
|
+
# a = [:foo, 'bar', 2]
|
875
|
+
# a[0..-1] # => [:foo, "bar", 2]
|
876
|
+
# a[0..-2] # => [:foo, "bar"]
|
877
|
+
# a[0..-3] # => [:foo]
|
878
|
+
#
|
879
|
+
# If `range.start` is negative, calculates the start index from the end:
|
880
|
+
#
|
881
|
+
# a = [:foo, 'bar', 2]
|
882
|
+
# a[-1..2] # => [2]
|
883
|
+
# a[-2..2] # => ["bar", 2]
|
884
|
+
# a[-3..2] # => [:foo, "bar", 2]
|
885
|
+
#
|
886
|
+
# If `range.start` is larger than the array size, returns `nil`.
|
887
|
+
#
|
888
|
+
# a = [:foo, 'bar', 2]
|
889
|
+
# a[4..1] # => nil
|
890
|
+
# a[4..0] # => nil
|
891
|
+
# a[4..-1] # => nil
|
892
|
+
#
|
893
|
+
# When a single Enumerator::ArithmeticSequence argument `aseq` is given, returns
|
894
|
+
# an `Array` of elements corresponding to the indexes produced by the sequence.
|
895
|
+
#
|
896
|
+
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
|
897
|
+
# a[(1..).step(2)] # => ["data1", "data2", "data3"]
|
898
|
+
#
|
899
|
+
# Unlike slicing with range, if the start or the end of the arithmetic sequence
|
900
|
+
# is larger than array size, throws RangeError.
|
901
|
+
#
|
902
|
+
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
|
903
|
+
# a[(1..11).step(2)]
|
904
|
+
# # RangeError (((1..11).step(2)) out of range)
|
905
|
+
# a[(7..).step(2)]
|
906
|
+
# # RangeError (((7..).step(2)) out of range)
|
907
|
+
#
|
908
|
+
# If given a single argument, and its type is not one of the listed, tries to
|
909
|
+
# convert it to Integer, and raises if it is impossible:
|
910
|
+
#
|
911
|
+
# a = [:foo, 'bar', 2]
|
912
|
+
# # Raises TypeError (no implicit conversion of Symbol into Integer):
|
913
|
+
# a[:foo]
|
914
|
+
#
|
915
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
916
|
+
#
|
917
|
+
def []: %a{implicitly-returns-nil} (int index) -> Elem
|
918
|
+
| (int start, int length) -> ::Array[Elem]?
|
919
|
+
| (::Range[::Integer?] range) -> ::Array[Elem]?
|
920
|
+
|
921
|
+
# <!--
|
922
|
+
# rdoc-file=array.c
|
923
|
+
# - self[index] = object -> object
|
924
|
+
# - self[start, length] = object -> object
|
925
|
+
# - self[range] = object -> object
|
926
|
+
# -->
|
927
|
+
# Assigns elements in `self`, based on the given `object`; returns `object`.
|
928
|
+
#
|
929
|
+
# In brief:
|
930
|
+
#
|
931
|
+
# a_orig = [:foo, 'bar', 2]
|
932
|
+
#
|
933
|
+
# # With argument index.
|
934
|
+
# a = a_orig.dup
|
935
|
+
# a[0] = 'foo' # => "foo"
|
936
|
+
# a # => ["foo", "bar", 2]
|
937
|
+
# a = a_orig.dup
|
938
|
+
# a[7] = 'foo' # => "foo"
|
939
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
|
940
|
+
#
|
941
|
+
# # With arguments start and length.
|
942
|
+
# a = a_orig.dup
|
943
|
+
# a[0, 2] = 'foo' # => "foo"
|
944
|
+
# a # => ["foo", 2]
|
945
|
+
# a = a_orig.dup
|
946
|
+
# a[6, 50] = 'foo' # => "foo"
|
947
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
|
948
|
+
#
|
949
|
+
# # With argument range.
|
950
|
+
# a = a_orig.dup
|
951
|
+
# a[0..1] = 'foo' # => "foo"
|
952
|
+
# a # => ["foo", 2]
|
953
|
+
# a = a_orig.dup
|
954
|
+
# a[6..50] = 'foo' # => "foo"
|
955
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
|
956
|
+
#
|
957
|
+
# When Integer argument `index` is given, assigns `object` to an element in
|
958
|
+
# `self`.
|
959
|
+
#
|
960
|
+
# If `index` is non-negative, assigns `object` the element at offset `index`:
|
961
|
+
#
|
962
|
+
# a = [:foo, 'bar', 2]
|
963
|
+
# a[0] = 'foo' # => "foo"
|
964
|
+
# a # => ["foo", "bar", 2]
|
965
|
+
#
|
966
|
+
# If `index` is greater than `self.length`, extends the array:
|
967
|
+
#
|
968
|
+
# a = [:foo, 'bar', 2]
|
969
|
+
# a[7] = 'foo' # => "foo"
|
970
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, nil, "foo"]
|
971
|
+
#
|
972
|
+
# If `index` is negative, counts backwards from the end of the array:
|
973
|
+
#
|
974
|
+
# a = [:foo, 'bar', 2]
|
975
|
+
# a[-1] = 'two' # => "two"
|
976
|
+
# a # => [:foo, "bar", "two"]
|
977
|
+
#
|
978
|
+
# When Integer arguments `start` and `length` are given and `object` is not an
|
979
|
+
# `Array`, removes `length - 1` elements beginning at offset `start`, and
|
980
|
+
# assigns `object` at offset `start`:
|
981
|
+
#
|
982
|
+
# a = [:foo, 'bar', 2]
|
983
|
+
# a[0, 2] = 'foo' # => "foo"
|
984
|
+
# a # => ["foo", 2]
|
985
|
+
#
|
986
|
+
# If `start` is negative, counts backwards from the end of the array:
|
987
|
+
#
|
988
|
+
# a = [:foo, 'bar', 2]
|
989
|
+
# a[-2, 2] = 'foo' # => "foo"
|
990
|
+
# a # => [:foo, "foo"]
|
991
|
+
#
|
992
|
+
# If `start` is non-negative and outside the array (` >= self.size`), extends
|
993
|
+
# the array with `nil`, assigns `object` at offset `start`, and ignores
|
994
|
+
# `length`:
|
995
|
+
#
|
996
|
+
# a = [:foo, 'bar', 2]
|
997
|
+
# a[6, 50] = 'foo' # => "foo"
|
998
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
|
999
|
+
#
|
1000
|
+
# If `length` is zero, shifts elements at and following offset `start` and
|
1001
|
+
# assigns `object` at offset `start`:
|
1002
|
+
#
|
1003
|
+
# a = [:foo, 'bar', 2]
|
1004
|
+
# a[1, 0] = 'foo' # => "foo"
|
1005
|
+
# a # => [:foo, "foo", "bar", 2]
|
1006
|
+
#
|
1007
|
+
# If `length` is too large for the existing array, does not extend the array:
|
1008
|
+
#
|
1009
|
+
# a = [:foo, 'bar', 2]
|
1010
|
+
# a[1, 5] = 'foo' # => "foo"
|
1011
|
+
# a # => [:foo, "foo"]
|
1012
|
+
#
|
1013
|
+
# When Range argument `range` is given and `object` is not an `Array`, removes
|
1014
|
+
# `length - 1` elements beginning at offset `start`, and assigns `object` at
|
1015
|
+
# offset `start`:
|
1016
|
+
#
|
1017
|
+
# a = [:foo, 'bar', 2]
|
1018
|
+
# a[0..1] = 'foo' # => "foo"
|
1019
|
+
# a # => ["foo", 2]
|
1020
|
+
#
|
1021
|
+
# if `range.begin` is negative, counts backwards from the end of the array:
|
1022
|
+
#
|
1023
|
+
# a = [:foo, 'bar', 2]
|
1024
|
+
# a[-2..2] = 'foo' # => "foo"
|
1025
|
+
# a # => [:foo, "foo"]
|
1026
|
+
#
|
1027
|
+
# If the array length is less than `range.begin`, extends the array with `nil`,
|
1028
|
+
# assigns `object` at offset `range.begin`, and ignores `length`:
|
1029
|
+
#
|
1030
|
+
# a = [:foo, 'bar', 2]
|
1031
|
+
# a[6..50] = 'foo' # => "foo"
|
1032
|
+
# a # => [:foo, "bar", 2, nil, nil, nil, "foo"]
|
1033
|
+
#
|
1034
|
+
# If `range.end` is zero, shifts elements at and following offset `start` and
|
1035
|
+
# assigns `object` at offset `start`:
|
1036
|
+
#
|
1037
|
+
# a = [:foo, 'bar', 2]
|
1038
|
+
# a[1..0] = 'foo' # => "foo"
|
1039
|
+
# a # => [:foo, "foo", "bar", 2]
|
1040
|
+
#
|
1041
|
+
# If `range.end` is negative, assigns `object` at offset `start`, retains
|
1042
|
+
# `range.end.abs -1` elements past that, and removes those beyond:
|
1043
|
+
#
|
1044
|
+
# a = [:foo, 'bar', 2]
|
1045
|
+
# a[1..-1] = 'foo' # => "foo"
|
1046
|
+
# a # => [:foo, "foo"]
|
1047
|
+
# a = [:foo, 'bar', 2]
|
1048
|
+
# a[1..-2] = 'foo' # => "foo"
|
1049
|
+
# a # => [:foo, "foo", 2]
|
1050
|
+
# a = [:foo, 'bar', 2]
|
1051
|
+
# a[1..-3] = 'foo' # => "foo"
|
1052
|
+
# a # => [:foo, "foo", "bar", 2]
|
1053
|
+
# a = [:foo, 'bar', 2]
|
1054
|
+
#
|
1055
|
+
# If `range.end` is too large for the existing array, replaces array elements,
|
1056
|
+
# but does not extend the array with `nil` values:
|
1057
|
+
#
|
1058
|
+
# a = [:foo, 'bar', 2]
|
1059
|
+
# a[1..5] = 'foo' # => "foo"
|
1060
|
+
# a # => [:foo, "foo"]
|
1061
|
+
#
|
1062
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
1063
|
+
#
|
1064
|
+
def []=: (int index, Elem obj) -> Elem
|
1065
|
+
| (int start, int length, Elem obj) -> Elem
|
1066
|
+
| (int start, int length, ::Array[Elem]) -> ::Array[Elem]
|
1067
|
+
| (int start, int length, nil) -> nil
|
1068
|
+
| (::Range[::Integer?], Elem obj) -> Elem
|
1069
|
+
| (::Range[::Integer?], ::Array[Elem]) -> ::Array[Elem]
|
1070
|
+
| (::Range[::Integer?], nil) -> nil
|
1071
|
+
|
1072
|
+
# <!--
|
1073
|
+
# rdoc-file=array.c
|
1074
|
+
# - all? -> true or false
|
1075
|
+
# - all?(object) -> true or false
|
1076
|
+
# - all? {|element| ... } -> true or false
|
1077
|
+
# -->
|
1078
|
+
# Returns whether for every element of `self`, a given criterion is satisfied.
|
1079
|
+
#
|
1080
|
+
# With no block and no argument, returns whether every element of `self` is
|
1081
|
+
# truthy:
|
1082
|
+
#
|
1083
|
+
# [[], {}, '', 0, 0.0, Object.new].all? # => true # All truthy objects.
|
1084
|
+
# [[], {}, '', 0, 0.0, nil].all? # => false # nil is not truthy.
|
1085
|
+
# [[], {}, '', 0, 0.0, false].all? # => false # false is not truthy.
|
1086
|
+
#
|
1087
|
+
# With argument `object` given, returns whether `object === ele` for every
|
1088
|
+
# element `ele` in `self`:
|
1089
|
+
#
|
1090
|
+
# [0, 0, 0].all?(0) # => true
|
1091
|
+
# [0, 1, 2].all?(1) # => false
|
1092
|
+
# ['food', 'fool', 'foot'].all?(/foo/) # => true
|
1093
|
+
# ['food', 'drink'].all?(/foo/) # => false
|
1094
|
+
#
|
1095
|
+
# With a block given, calls the block with each element in `self`; returns
|
1096
|
+
# whether the block returns only truthy values:
|
1097
|
+
#
|
1098
|
+
# [0, 1, 2].all? { |ele| ele < 3 } # => true
|
1099
|
+
# [0, 1, 2].all? { |ele| ele < 2 } # => false
|
1100
|
+
#
|
1101
|
+
# With both a block and argument `object` given, ignores the block and uses
|
1102
|
+
# `object` as above.
|
1103
|
+
#
|
1104
|
+
# **Special case**: returns `true` if `self` is empty (regardless of any given
|
1105
|
+
# argument or block).
|
1106
|
+
#
|
1107
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
1108
|
+
#
|
1109
|
+
def all?: () -> bool
|
1110
|
+
| (_Pattern[Elem] pattern) -> bool
|
1111
|
+
| () { (Elem obj) -> boolish } -> bool
|
1112
|
+
|
1113
|
+
# <!--
|
1114
|
+
# rdoc-file=array.c
|
1115
|
+
# - any? -> true or false
|
1116
|
+
# - any?(object) -> true or false
|
1117
|
+
# - any? {|element| ... } -> true or false
|
1118
|
+
# -->
|
1119
|
+
# Returns whether for any element of `self`, a given criterion is satisfied.
|
1120
|
+
#
|
1121
|
+
# With no block and no argument, returns whether any element of `self` is
|
1122
|
+
# truthy:
|
1123
|
+
#
|
1124
|
+
# [nil, false, []].any? # => true # Array object is truthy.
|
1125
|
+
# [nil, false, {}].any? # => true # Hash object is truthy.
|
1126
|
+
# [nil, false, ''].any? # => true # String object is truthy.
|
1127
|
+
# [nil, false].any? # => false # Nil and false are not truthy.
|
1128
|
+
#
|
1129
|
+
# With argument `object` given, returns whether `object === ele` for any element
|
1130
|
+
# `ele` in `self`:
|
1131
|
+
#
|
1132
|
+
# [nil, false, 0].any?(0) # => true
|
1133
|
+
# [nil, false, 1].any?(0) # => false
|
1134
|
+
# [nil, false, 'food'].any?(/foo/) # => true
|
1135
|
+
# [nil, false, 'food'].any?(/bar/) # => false
|
1136
|
+
#
|
1137
|
+
# With a block given, calls the block with each element in `self`; returns
|
1138
|
+
# whether the block returns any truthy value:
|
1139
|
+
#
|
1140
|
+
# [0, 1, 2].any? {|ele| ele < 1 } # => true
|
1141
|
+
# [0, 1, 2].any? {|ele| ele < 0 } # => false
|
1142
|
+
#
|
1143
|
+
# With both a block and argument `object` given, ignores the block and uses
|
1144
|
+
# `object` as above.
|
1145
|
+
#
|
1146
|
+
# **Special case**: returns `false` if `self` is empty (regardless of any given
|
1147
|
+
# argument or block).
|
1148
|
+
#
|
1149
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
1150
|
+
#
|
1151
|
+
alias any? all?
|
1152
|
+
|
1153
|
+
# <!-- rdoc-file=array.c -->
|
1154
|
+
# Appends each argument in `objects` to `self`; returns `self`:
|
1155
|
+
#
|
1156
|
+
# a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
|
1157
|
+
# a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
|
1158
|
+
#
|
1159
|
+
# Appends each argument as a single element, even if it is another array:
|
1160
|
+
#
|
1161
|
+
# a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
|
1162
|
+
# a.push([:baz, :bat], [:bam, :bad]) # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
|
1163
|
+
#
|
1164
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
1165
|
+
#
|
1166
|
+
alias append push
|
1167
|
+
|
1168
|
+
# <!--
|
1169
|
+
# rdoc-file=array.c
|
1170
|
+
# - assoc(object) -> found_array or nil
|
1171
|
+
# -->
|
1172
|
+
# Returns the first element `ele` in `self` such that `ele` is an array and
|
1173
|
+
# `ele[0] == object`:
|
1174
|
+
#
|
1175
|
+
# a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
|
1176
|
+
# a.assoc(4) # => [4, 5, 6]
|
1177
|
+
#
|
1178
|
+
# Returns `nil` if no such element is found.
|
1179
|
+
#
|
1180
|
+
# Related: Array#rassoc; see also [Methods for
|
1181
|
+
# Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1182
|
+
#
|
1183
|
+
def assoc: (untyped) -> ::Array[untyped]?
|
1184
|
+
|
1185
|
+
# <!--
|
1186
|
+
# rdoc-file=array.c
|
1187
|
+
# - at(index) -> object or nil
|
1188
|
+
# -->
|
1189
|
+
# Returns the element of `self` specified by the given `index` or `nil` if there
|
1190
|
+
# is no such element; `index` must be an [integer-convertible
|
1191
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
1192
|
+
#
|
1193
|
+
# For non-negative `index`, returns the element of `self` at offset `index`:
|
1194
|
+
#
|
1195
|
+
# a = [:foo, 'bar', 2]
|
1196
|
+
# a.at(0) # => :foo
|
1197
|
+
# a.at(2) # => 2
|
1198
|
+
# a.at(2.0) # => 2
|
1199
|
+
#
|
1200
|
+
# For negative `index`, counts backwards from the end of `self`:
|
1201
|
+
#
|
1202
|
+
# a.at(-2) # => "bar"
|
1203
|
+
#
|
1204
|
+
# Related: Array#[]; see also [Methods for
|
1205
|
+
# Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1206
|
+
#
|
1207
|
+
def at: %a{implicitly-returns-nil} (int index) -> Elem
|
1208
|
+
|
1209
|
+
# <!--
|
1210
|
+
# rdoc-file=array.c
|
1211
|
+
# - bsearch {|element| ... } -> found_element or nil
|
1212
|
+
# - bsearch -> new_enumerator
|
1213
|
+
# -->
|
1214
|
+
# Returns the element from `self` found by a binary search, or `nil` if the
|
1215
|
+
# search found no suitable element.
|
1216
|
+
#
|
1217
|
+
# See [Binary Searching](rdoc-ref:bsearch.rdoc).
|
1218
|
+
#
|
1219
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1220
|
+
#
|
1221
|
+
def bsearch: () -> ::Enumerator[Elem, Elem?]
|
1222
|
+
| () { (Elem) -> (true | false) } -> Elem?
|
1223
|
+
| () { (Elem) -> ::Integer } -> Elem?
|
1224
|
+
|
1225
|
+
# <!--
|
1226
|
+
# rdoc-file=array.c
|
1227
|
+
# - bsearch_index {|element| ... } -> integer or nil
|
1228
|
+
# - bsearch_index -> new_enumerator
|
1229
|
+
# -->
|
1230
|
+
# Returns the integer index of the element from `self` found by a binary search,
|
1231
|
+
# or `nil` if the search found no suitable element.
|
1232
|
+
#
|
1233
|
+
# See [Binary Searching](rdoc-ref:bsearch.rdoc).
|
1234
|
+
#
|
1235
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1236
|
+
#
|
1237
|
+
def bsearch_index: () { (Elem) -> (true | false) } -> ::Integer?
|
1238
|
+
| () { (Elem) -> ::Integer } -> ::Integer?
|
1239
|
+
|
1240
|
+
# <!--
|
1241
|
+
# rdoc-file=array.c
|
1242
|
+
# - clear -> self
|
1243
|
+
# -->
|
1244
|
+
# Removes all elements from `self`; returns `self`:
|
1245
|
+
#
|
1246
|
+
# a = [:foo, 'bar', 2]
|
1247
|
+
# a.clear # => []
|
1248
|
+
#
|
1249
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1250
|
+
#
|
1251
|
+
def clear: () -> self
|
1252
|
+
|
1253
|
+
# <!--
|
1254
|
+
# rdoc-file=array.c
|
1255
|
+
# - collect {|element| ... } -> new_array
|
1256
|
+
# - collect -> new_enumerator
|
1257
|
+
# - map {|element| ... } -> new_array
|
1258
|
+
# - map -> new_enumerator
|
1259
|
+
# -->
|
1260
|
+
# With a block given, calls the block with each element of `self`; returns a new
|
1261
|
+
# array whose elements are the return values from the block:
|
1262
|
+
#
|
1263
|
+
# a = [:foo, 'bar', 2]
|
1264
|
+
# a1 = a.map {|element| element.class }
|
1265
|
+
# a1 # => [Symbol, String, Integer]
|
1266
|
+
#
|
1267
|
+
# With no block given, returns a new Enumerator.
|
1268
|
+
#
|
1269
|
+
# Related: #collect!; see also [Methods for
|
1270
|
+
# Converting](rdoc-ref:Array@Methods+for+Converting).
|
1271
|
+
#
|
1272
|
+
def collect: [U] () { (Elem item) -> U } -> ::Array[U]
|
1273
|
+
| () -> ::Enumerator[Elem, ::Array[untyped]]
|
1274
|
+
|
1275
|
+
# <!--
|
1276
|
+
# rdoc-file=array.c
|
1277
|
+
# - collect! {|element| ... } -> new_array
|
1278
|
+
# - collect! -> new_enumerator
|
1279
|
+
# - map! {|element| ... } -> new_array
|
1280
|
+
# - map! -> new_enumerator
|
1281
|
+
# -->
|
1282
|
+
# With a block given, calls the block with each element of `self` and replaces
|
1283
|
+
# the element with the block's return value; returns `self`:
|
1284
|
+
#
|
1285
|
+
# a = [:foo, 'bar', 2]
|
1286
|
+
# a.map! { |element| element.class } # => [Symbol, String, Integer]
|
1287
|
+
#
|
1288
|
+
# With no block given, returns a new Enumerator.
|
1289
|
+
#
|
1290
|
+
# Related: #collect; see also [Methods for
|
1291
|
+
# Converting](rdoc-ref:Array@Methods+for+Converting).
|
1292
|
+
#
|
1293
|
+
def collect!: () { (Elem item) -> Elem } -> self
|
1294
|
+
| () -> ::Enumerator[Elem, self]
|
1295
|
+
|
1296
|
+
# <!--
|
1297
|
+
# rdoc-file=array.c
|
1298
|
+
# - combination(count) {|element| ... } -> self
|
1299
|
+
# - combination(count) -> new_enumerator
|
1300
|
+
# -->
|
1301
|
+
# When a block and a positive [integer-convertible
|
1302
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects)
|
1303
|
+
# argument `count` (`0 < count <= self.size`) are given, calls the block with
|
1304
|
+
# each combination of `self` of size `count`; returns `self`:
|
1305
|
+
#
|
1306
|
+
# a = %w[a b c] # => ["a", "b", "c"]
|
1307
|
+
# a.combination(2) {|combination| p combination } # => ["a", "b", "c"]
|
1308
|
+
#
|
1309
|
+
# Output:
|
1310
|
+
#
|
1311
|
+
# ["a", "b"]
|
1312
|
+
# ["a", "c"]
|
1313
|
+
# ["b", "c"]
|
1314
|
+
#
|
1315
|
+
# The order of the yielded combinations is not guaranteed.
|
1316
|
+
#
|
1317
|
+
# When `count` is zero, calls the block once with a new empty array:
|
1318
|
+
#
|
1319
|
+
# a.combination(0) {|combination| p combination }
|
1320
|
+
# [].combination(0) {|combination| p combination }
|
1321
|
+
#
|
1322
|
+
# Output:
|
1323
|
+
#
|
1324
|
+
# []
|
1325
|
+
# []
|
1326
|
+
#
|
1327
|
+
# When `count` is negative or larger than `self.size` and `self` is non-empty,
|
1328
|
+
# does not call the block:
|
1329
|
+
#
|
1330
|
+
# a.combination(-1) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"]
|
1331
|
+
# a.combination(4) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"]
|
1332
|
+
#
|
1333
|
+
# With no block given, returns a new Enumerator.
|
1334
|
+
#
|
1335
|
+
# Related: Array#permutation; see also [Methods for
|
1336
|
+
# Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
1337
|
+
#
|
1338
|
+
def combination: (int n) { (::Array[Elem]) -> void } -> self
|
1339
|
+
| (int n) -> ::Enumerator[::Array[Elem], self]
|
1340
|
+
|
1341
|
+
# <!--
|
1342
|
+
# rdoc-file=array.c
|
1343
|
+
# - compact -> new_array
|
1344
|
+
# -->
|
1345
|
+
# Returns a new array containing only the non-`nil` elements from `self`;
|
1346
|
+
# element order is preserved:
|
1347
|
+
#
|
1348
|
+
# a = [nil, 0, nil, false, nil, '', nil, [], nil, {}]
|
1349
|
+
# a.compact # => [0, false, "", [], {}]
|
1350
|
+
#
|
1351
|
+
# Related: Array#compact!; see also [Methods for
|
1352
|
+
# Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1353
|
+
#
|
1354
|
+
def compact: () -> ::Array[Elem]
|
1355
|
+
|
1356
|
+
# <!--
|
1357
|
+
# rdoc-file=array.c
|
1358
|
+
# - compact! -> self or nil
|
1359
|
+
# -->
|
1360
|
+
# Removes all `nil` elements from `self`; Returns `self` if any elements are
|
1361
|
+
# removed, `nil` otherwise:
|
1362
|
+
#
|
1363
|
+
# a = [nil, 0, nil, false, nil, '', nil, [], nil, {}]
|
1364
|
+
# a.compact! # => [0, false, "", [], {}]
|
1365
|
+
# a # => [0, false, "", [], {}]
|
1366
|
+
# a.compact! # => nil
|
1367
|
+
#
|
1368
|
+
# Related: Array#compact; see also [Methods for
|
1369
|
+
# Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1370
|
+
#
|
1371
|
+
def compact!: () -> self?
|
1372
|
+
|
1373
|
+
# <!--
|
1374
|
+
# rdoc-file=array.c
|
1375
|
+
# - concat(*other_arrays) -> self
|
1376
|
+
# -->
|
1377
|
+
# Adds to `self` all elements from each array in `other_arrays`; returns `self`:
|
1378
|
+
#
|
1379
|
+
# a = [0, 1]
|
1380
|
+
# a.concat(['two', 'three'], [:four, :five], a)
|
1381
|
+
# # => [0, 1, "two", "three", :four, :five, 0, 1]
|
1382
|
+
#
|
1383
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
1384
|
+
#
|
1385
|
+
def concat: (*::Array[Elem] arrays) -> self
|
1386
|
+
|
1387
|
+
# <!--
|
1388
|
+
# rdoc-file=array.c
|
1389
|
+
# - count -> integer
|
1390
|
+
# - count(object) -> integer
|
1391
|
+
# - count {|element| ... } -> integer
|
1392
|
+
# -->
|
1393
|
+
# Returns a count of specified elements.
|
1394
|
+
#
|
1395
|
+
# With no argument and no block, returns the count of all elements:
|
1396
|
+
#
|
1397
|
+
# [0, :one, 'two', 3, 3.0].count # => 5
|
1398
|
+
#
|
1399
|
+
# With argument `object` given, returns the count of elements `==` to `object`:
|
1400
|
+
#
|
1401
|
+
# [0, :one, 'two', 3, 3.0].count(3) # => 2
|
1402
|
+
#
|
1403
|
+
# With no argument and a block given, calls the block with each element; returns
|
1404
|
+
# the count of elements for which the block returns a truthy value:
|
1405
|
+
#
|
1406
|
+
# [0, 1, 2, 3].count {|element| element > 1 } # => 2
|
1407
|
+
#
|
1408
|
+
# With argument `object` and a block given, issues a warning, ignores the block,
|
1409
|
+
# and returns the count of elements `==` to `object`.
|
1410
|
+
#
|
1411
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
1412
|
+
#
|
1413
|
+
def count: () -> ::Integer
|
1414
|
+
| (Elem obj) -> ::Integer
|
1415
|
+
| () { (Elem) -> boolish } -> ::Integer
|
1416
|
+
|
1417
|
+
# <!--
|
1418
|
+
# rdoc-file=array.c
|
1419
|
+
# - cycle(count = nil) {|element| ... } -> nil
|
1420
|
+
# - cycle(count = nil) -> new_enumerator
|
1421
|
+
# -->
|
1422
|
+
# With a block given, may call the block, depending on the value of argument
|
1423
|
+
# `count`; `count` must be an [integer-convertible
|
1424
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects), or
|
1425
|
+
# `nil`.
|
1426
|
+
#
|
1427
|
+
# When `count` is positive, calls the block with each element, then does so
|
1428
|
+
# repeatedly, until it has done so `count` times; returns `nil`:
|
1429
|
+
#
|
1430
|
+
# output = []
|
1431
|
+
# [0, 1].cycle(2) {|element| output.push(element) } # => nil
|
1432
|
+
# output # => [0, 1, 0, 1]
|
1433
|
+
#
|
1434
|
+
# When `count` is zero or negative, does not call the block:
|
1435
|
+
#
|
1436
|
+
# [0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil
|
1437
|
+
# [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
|
1438
|
+
#
|
1439
|
+
# When `count` is `nil`, cycles forever:
|
1440
|
+
#
|
1441
|
+
# # Prints 0 and 1 forever.
|
1442
|
+
# [0, 1].cycle {|element| puts element }
|
1443
|
+
# [0, 1].cycle(nil) {|element| puts element }
|
1444
|
+
#
|
1445
|
+
# With no block given, returns a new Enumerator.
|
1446
|
+
#
|
1447
|
+
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
1448
|
+
#
|
1449
|
+
def cycle: (?int? n) { (Elem) -> void } -> nil
|
1450
|
+
| (?int? n) -> ::Enumerator[Elem, nil]
|
1451
|
+
|
1452
|
+
# <!--
|
1453
|
+
# rdoc-file=array.c
|
1454
|
+
# - deconstruct()
|
1455
|
+
# -->
|
1456
|
+
#
|
1457
|
+
def deconstruct: () -> self
|
1458
|
+
|
1459
|
+
# <!--
|
1460
|
+
# rdoc-file=array.c
|
1461
|
+
# - delete(object) -> last_removed_object
|
1462
|
+
# - delete(object) {|element| ... } -> last_removed_object or block_return
|
1463
|
+
# -->
|
1464
|
+
# Removes zero or more elements from `self`.
|
1465
|
+
#
|
1466
|
+
# With no block given, removes from `self` each element `ele` such that `ele ==
|
1467
|
+
# object`; returns the last removed element:
|
1468
|
+
#
|
1469
|
+
# a = [0, 1, 2, 2.0]
|
1470
|
+
# a.delete(2) # => 2.0
|
1471
|
+
# a # => [0, 1]
|
1472
|
+
#
|
1473
|
+
# Returns `nil` if no elements removed:
|
1474
|
+
#
|
1475
|
+
# a.delete(2) # => nil
|
1476
|
+
#
|
1477
|
+
# With a block given, removes from `self` each element `ele` such that `ele ==
|
1478
|
+
# object`.
|
1479
|
+
#
|
1480
|
+
# If any such elements are found, ignores the block and returns the last removed
|
1481
|
+
# element:
|
1482
|
+
#
|
1483
|
+
# a = [0, 1, 2, 2.0]
|
1484
|
+
# a.delete(2) {|element| fail 'Cannot happen' } # => 2.0
|
1485
|
+
# a # => [0, 1]
|
1486
|
+
#
|
1487
|
+
# If no such element is found, returns the block's return value:
|
1488
|
+
#
|
1489
|
+
# a.delete(2) {|element| "Element #{element} not found." }
|
1490
|
+
# # => "Element 2 not found."
|
1491
|
+
#
|
1492
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1493
|
+
#
|
1494
|
+
def delete: (Elem obj) -> Elem?
|
1495
|
+
| [S, T] (S obj) { (S) -> T } -> (Elem | T)
|
1496
|
+
|
1497
|
+
# <!--
|
1498
|
+
# rdoc-file=array.c
|
1499
|
+
# - delete_at(index) -> removed_object or nil
|
1500
|
+
# -->
|
1501
|
+
# Removes the element of `self` at the given `index`, which must be an
|
1502
|
+
# [integer-convertible
|
1503
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
1504
|
+
#
|
1505
|
+
# When `index` is non-negative, deletes the element at offset `index`:
|
1506
|
+
#
|
1507
|
+
# a = [:foo, 'bar', 2]
|
1508
|
+
# a.delete_at(1) # => "bar"
|
1509
|
+
# a # => [:foo, 2]
|
1510
|
+
#
|
1511
|
+
# When `index` is negative, counts backward from the end of the array:
|
1512
|
+
#
|
1513
|
+
# a = [:foo, 'bar', 2]
|
1514
|
+
# a.delete_at(-2) # => "bar"
|
1515
|
+
# a # => [:foo, 2]
|
1516
|
+
#
|
1517
|
+
# When `index` is out of range, returns `nil`.
|
1518
|
+
#
|
1519
|
+
# a = [:foo, 'bar', 2]
|
1520
|
+
# a.delete_at(3) # => nil
|
1521
|
+
# a.delete_at(-4) # => nil
|
1522
|
+
#
|
1523
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1524
|
+
#
|
1525
|
+
def delete_at: %a{implicitly-returns-nil} (int index) -> Elem
|
1526
|
+
|
1527
|
+
# <!--
|
1528
|
+
# rdoc-file=array.c
|
1529
|
+
# - delete_if {|element| ... } -> self
|
1530
|
+
# - delete_if -> new_numerator
|
1531
|
+
# -->
|
1532
|
+
# With a block given, calls the block with each element of `self`; removes the
|
1533
|
+
# element if the block returns a truthy value; returns `self`:
|
1534
|
+
#
|
1535
|
+
# a = [:foo, 'bar', 2, 'bat']
|
1536
|
+
# a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2]
|
1537
|
+
#
|
1538
|
+
# With no block given, returns a new Enumerator.
|
1539
|
+
#
|
1540
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
1541
|
+
#
|
1542
|
+
def delete_if: () { (Elem item) -> boolish } -> self
|
1543
|
+
| () -> ::Enumerator[Elem, self]
|
1544
|
+
|
1545
|
+
# <!--
|
1546
|
+
# rdoc-file=array.c
|
1547
|
+
# - difference(*other_arrays = []) -> new_array
|
1548
|
+
# -->
|
1549
|
+
# Returns a new array containing only those elements from `self` that are not
|
1550
|
+
# found in any of the given `other_arrays`; items are compared using `eql?`;
|
1551
|
+
# order from `self` is preserved:
|
1552
|
+
#
|
1553
|
+
# [0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3]
|
1554
|
+
# [0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2]
|
1555
|
+
# [0, 1, 2].difference([4]) # => [0, 1, 2]
|
1556
|
+
# [0, 1, 2].difference # => [0, 1, 2]
|
1557
|
+
#
|
1558
|
+
# Returns a copy of `self` if no arguments are given.
|
1559
|
+
#
|
1560
|
+
# Related: Array#-; see also [Methods for
|
1561
|
+
# Combining](rdoc-ref:Array@Methods+for+Combining).
|
1562
|
+
#
|
1563
|
+
def difference: (*::Array[untyped] arrays) -> ::Array[Elem]
|
1564
|
+
|
1565
|
+
# <!--
|
1566
|
+
# rdoc-file=array.c
|
1567
|
+
# - array.dig(index, *identifiers) -> object
|
1568
|
+
# -->
|
1569
|
+
# Finds and returns the object in nested object specified by `index` and
|
1570
|
+
# `identifiers`; the nested objects may be instances of various classes. See
|
1571
|
+
# [Dig Methods](rdoc-ref:dig_methods.rdoc).
|
1572
|
+
#
|
1573
|
+
# Examples:
|
1574
|
+
#
|
1575
|
+
# a = [:foo, [:bar, :baz, [:bat, :bam]]]
|
1576
|
+
# a.dig(1) # => [:bar, :baz, [:bat, :bam]]
|
1577
|
+
# a.dig(1, 2) # => [:bat, :bam]
|
1578
|
+
# a.dig(1, 2, 0) # => :bat
|
1579
|
+
# a.dig(1, 2, 3) # => nil
|
1580
|
+
#
|
1581
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1582
|
+
#
|
1583
|
+
def dig: (int idx) -> Elem?
|
1584
|
+
| (int idx, untyped, *untyped) -> untyped
|
1585
|
+
|
1586
|
+
# <!--
|
1587
|
+
# rdoc-file=array.c
|
1588
|
+
# - drop(count) -> new_array
|
1589
|
+
# -->
|
1590
|
+
# Returns a new array containing all but the first `count` element of `self`,
|
1591
|
+
# where `count` is a non-negative integer; does not modify `self`.
|
1592
|
+
#
|
1593
|
+
# Examples:
|
1594
|
+
#
|
1595
|
+
# a = [0, 1, 2, 3, 4, 5]
|
1596
|
+
# a.drop(0) # => [0, 1, 2, 3, 4, 5]
|
1597
|
+
# a.drop(1) # => [1, 2, 3, 4, 5]
|
1598
|
+
# a.drop(2) # => [2, 3, 4, 5]
|
1599
|
+
# a.drop(9) # => []
|
1600
|
+
#
|
1601
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1602
|
+
#
|
1603
|
+
def drop: (int n) -> ::Array[Elem]
|
1604
|
+
|
1605
|
+
# <!--
|
1606
|
+
# rdoc-file=array.c
|
1607
|
+
# - drop_while {|element| ... } -> new_array
|
1608
|
+
# - drop_while -> new_enumerator
|
1609
|
+
# -->
|
1610
|
+
# With a block given, calls the block with each successive element of `self`;
|
1611
|
+
# stops if the block returns `false` or `nil`; returns a new array *omitting*
|
1612
|
+
# those elements for which the block returned a truthy value; does not modify
|
1613
|
+
# `self`:
|
1614
|
+
#
|
1615
|
+
# a = [0, 1, 2, 3, 4, 5]
|
1616
|
+
# a.drop_while {|element| element < 3 } # => [3, 4, 5]
|
1617
|
+
#
|
1618
|
+
# With no block given, returns a new Enumerator.
|
1619
|
+
#
|
1620
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1621
|
+
#
|
1622
|
+
def drop_while: () { (Elem obj) -> boolish } -> ::Array[Elem]
|
1623
|
+
| () -> ::Enumerator[Elem, ::Array[Elem]]
|
1624
|
+
|
1625
|
+
# <!--
|
1626
|
+
# rdoc-file=array.c
|
1627
|
+
# - each {|element| ... } -> self
|
1628
|
+
# - each -> new_enumerator
|
1629
|
+
# -->
|
1630
|
+
# With a block given, iterates over the elements of `self`, passing each element
|
1631
|
+
# to the block; returns `self`:
|
1632
|
+
#
|
1633
|
+
# a = [:foo, 'bar', 2]
|
1634
|
+
# a.each {|element| puts "#{element.class} #{element}" }
|
1635
|
+
#
|
1636
|
+
# Output:
|
1637
|
+
#
|
1638
|
+
# Symbol foo
|
1639
|
+
# String bar
|
1640
|
+
# Integer 2
|
1641
|
+
#
|
1642
|
+
# Allows the array to be modified during iteration:
|
1643
|
+
#
|
1644
|
+
# a = [:foo, 'bar', 2]
|
1645
|
+
# a.each {|element| puts element; a.clear if element.to_s.start_with?('b') }
|
1646
|
+
#
|
1647
|
+
# Output:
|
1648
|
+
#
|
1649
|
+
# foo
|
1650
|
+
# bar
|
1651
|
+
#
|
1652
|
+
# With no block given, returns a new Enumerator.
|
1653
|
+
#
|
1654
|
+
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
1655
|
+
#
|
1656
|
+
def each: () -> ::Enumerator[Elem, self]
|
1657
|
+
| () { (Elem item) -> void } -> self
|
1658
|
+
|
1659
|
+
# <!--
|
1660
|
+
# rdoc-file=array.c
|
1661
|
+
# - each_index {|index| ... } -> self
|
1662
|
+
# - each_index -> new_enumerator
|
1663
|
+
# -->
|
1664
|
+
# With a block given, iterates over the elements of `self`, passing each *array
|
1665
|
+
# index* to the block; returns `self`:
|
1666
|
+
#
|
1667
|
+
# a = [:foo, 'bar', 2]
|
1668
|
+
# a.each_index {|index| puts "#{index} #{a[index]}" }
|
1669
|
+
#
|
1670
|
+
# Output:
|
1671
|
+
#
|
1672
|
+
# 0 foo
|
1673
|
+
# 1 bar
|
1674
|
+
# 2 2
|
1675
|
+
#
|
1676
|
+
# Allows the array to be modified during iteration:
|
1677
|
+
#
|
1678
|
+
# a = [:foo, 'bar', 2]
|
1679
|
+
# a.each_index {|index| puts index; a.clear if index > 0 }
|
1680
|
+
# a # => []
|
1681
|
+
#
|
1682
|
+
# Output:
|
1683
|
+
#
|
1684
|
+
# 0
|
1685
|
+
# 1
|
1686
|
+
#
|
1687
|
+
# With no block given, returns a new Enumerator.
|
1688
|
+
#
|
1689
|
+
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
1690
|
+
#
|
1691
|
+
def each_index: () { (::Integer index) -> void } -> self
|
1692
|
+
| () -> ::Enumerator[::Integer, self]
|
1693
|
+
|
1694
|
+
# <!--
|
1695
|
+
# rdoc-file=array.c
|
1696
|
+
# - array.empty? -> true or false
|
1697
|
+
# -->
|
1698
|
+
# Returns `true` if the count of elements in `self` is zero, `false` otherwise.
|
1699
|
+
#
|
1700
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
1701
|
+
#
|
1702
|
+
def empty?: () -> bool
|
1703
|
+
|
1704
|
+
# <!--
|
1705
|
+
# rdoc-file=array.c
|
1706
|
+
# - eql?(other_array) -> true or false
|
1707
|
+
# -->
|
1708
|
+
# Returns `true` if `self` and `other_array` are the same size, and if, for each
|
1709
|
+
# index `i` in `self`, `self[i].eql?(other_array[i])`:
|
1710
|
+
#
|
1711
|
+
# a0 = [:foo, 'bar', 2]
|
1712
|
+
# a1 = [:foo, 'bar', 2]
|
1713
|
+
# a1.eql?(a0) # => true
|
1714
|
+
#
|
1715
|
+
# Otherwise, returns `false`.
|
1716
|
+
#
|
1717
|
+
# This method is different from method Array#==, which compares using method
|
1718
|
+
# `Object#==`.
|
1719
|
+
#
|
1720
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
1721
|
+
#
|
1722
|
+
def eql?: (untyped other) -> bool
|
1723
|
+
|
1724
|
+
# <!--
|
1725
|
+
# rdoc-file=array.c
|
1726
|
+
# - fetch(index) -> element
|
1727
|
+
# - fetch(index, default_value) -> element or default_value
|
1728
|
+
# - fetch(index) {|index| ... } -> element or block_return_value
|
1729
|
+
# -->
|
1730
|
+
# Returns the element of `self` at offset `index` if `index` is in range;
|
1731
|
+
# `index` must be an [integer-convertible
|
1732
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
|
1733
|
+
#
|
1734
|
+
# With the single argument `index` and no block, returns the element at offset
|
1735
|
+
# `index`:
|
1736
|
+
#
|
1737
|
+
# a = [:foo, 'bar', 2]
|
1738
|
+
# a.fetch(1) # => "bar"
|
1739
|
+
# a.fetch(1.1) # => "bar"
|
1740
|
+
#
|
1741
|
+
# If `index` is negative, counts from the end of the array:
|
1742
|
+
#
|
1743
|
+
# a = [:foo, 'bar', 2]
|
1744
|
+
# a.fetch(-1) # => 2
|
1745
|
+
# a.fetch(-2) # => "bar"
|
1746
|
+
#
|
1747
|
+
# With arguments `index` and `default_value` (which may be any object) and no
|
1748
|
+
# block, returns `default_value` if `index` is out-of-range:
|
1749
|
+
#
|
1750
|
+
# a = [:foo, 'bar', 2]
|
1751
|
+
# a.fetch(1, nil) # => "bar"
|
1752
|
+
# a.fetch(3, :foo) # => :foo
|
1753
|
+
#
|
1754
|
+
# With argument `index` and a block, returns the element at offset `index` if
|
1755
|
+
# index is in range (and the block is not called); otherwise calls the block
|
1756
|
+
# with index and returns its return value:
|
1757
|
+
#
|
1758
|
+
# a = [:foo, 'bar', 2]
|
1759
|
+
# a.fetch(1) {|index| raise 'Cannot happen' } # => "bar"
|
1760
|
+
# a.fetch(50) {|index| "Value for #{index}" } # => "Value for 50"
|
1761
|
+
#
|
1762
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1763
|
+
#
|
1764
|
+
def fetch: (int index) -> Elem
|
1765
|
+
| [T] (int index, T default) -> (Elem | T)
|
1766
|
+
| [T] (int index) { (int index) -> T } -> (Elem | T)
|
1767
|
+
|
1768
|
+
# <!--
|
1769
|
+
# rdoc-file=array.rb
|
1770
|
+
# - fetch_values(*indexes) -> new_array
|
1771
|
+
# - fetch_values(*indexes) { |index| ... } -> new_array
|
1772
|
+
# -->
|
1773
|
+
# With no block given, returns a new array containing the elements of `self` at
|
1774
|
+
# the offsets specified by `indexes`. Each of the `indexes` must be an
|
1775
|
+
# [integer-convertible
|
1776
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects):
|
1777
|
+
#
|
1778
|
+
# a = [:foo, :bar, :baz]
|
1779
|
+
# a.fetch_values(2, 0) # => [:baz, :foo]
|
1780
|
+
# a.fetch_values(2.1, 0) # => [:baz, :foo]
|
1781
|
+
# a.fetch_values # => []
|
1782
|
+
#
|
1783
|
+
# For a negative index, counts backwards from the end of the array:
|
1784
|
+
#
|
1785
|
+
# a.fetch_values(-2, -1) # [:bar, :baz]
|
1786
|
+
#
|
1787
|
+
# When no block is given, raises an exception if any index is out of range.
|
1788
|
+
#
|
1789
|
+
# With a block given, for each index:
|
1790
|
+
#
|
1791
|
+
# * If the index is in range, uses an element of `self` (as above).
|
1792
|
+
# * Otherwise, calls the block with the index and uses the block's return
|
1793
|
+
# value.
|
1794
|
+
#
|
1795
|
+
# Example:
|
1796
|
+
#
|
1797
|
+
# a = [:foo, :bar, :baz]
|
1798
|
+
# a.fetch_values(1, 0, 42, 777) { |index| index.to_s }
|
1799
|
+
# # => [:bar, :foo, "42", "777"]
|
1800
|
+
#
|
1801
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
1802
|
+
#
|
1803
|
+
def fetch_values: (*int indexes) -> self
|
1804
|
+
|
1805
|
+
# <!--
|
1806
|
+
# rdoc-file=array.c
|
1807
|
+
# - fill(object, start = nil, count = nil) -> new_array
|
1808
|
+
# - fill(object, range) -> new_array
|
1809
|
+
# - fill(start = nil, count = nil) {|element| ... } -> new_array
|
1810
|
+
# - fill(range) {|element| ... } -> new_array
|
1811
|
+
# -->
|
1812
|
+
# Replaces selected elements in `self`; may add elements to `self`; always
|
1813
|
+
# returns `self` (never a new array).
|
1814
|
+
#
|
1815
|
+
# In brief:
|
1816
|
+
#
|
1817
|
+
# # Non-negative start.
|
1818
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"]
|
1819
|
+
# ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1820
|
+
#
|
1821
|
+
# # Extends with specified values if necessary.
|
1822
|
+
# ['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"]
|
1823
|
+
# ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"]
|
1824
|
+
#
|
1825
|
+
# # Fills with nils if necessary.
|
1826
|
+
# ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"]
|
1827
|
+
# ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"]
|
1828
|
+
#
|
1829
|
+
# # For negative start, counts backwards from the end.
|
1830
|
+
# ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"]
|
1831
|
+
# ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"]
|
1832
|
+
#
|
1833
|
+
# # Range.
|
1834
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"]
|
1835
|
+
# ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1836
|
+
#
|
1837
|
+
# When arguments `start` and `count` are given, they select the elements of
|
1838
|
+
# `self` to be replaced; each must be an [integer-convertible
|
1839
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects) (or
|
1840
|
+
# `nil`):
|
1841
|
+
#
|
1842
|
+
# * `start` specifies the zero-based offset of the first element to be
|
1843
|
+
# replaced; `nil` means zero.
|
1844
|
+
# * `count` is the number of consecutive elements to be replaced; `nil` means
|
1845
|
+
# "all the rest."
|
1846
|
+
#
|
1847
|
+
# With argument `object` given, that one object is used for all replacements:
|
1848
|
+
#
|
1849
|
+
# o = Object.new # => #<Object:0x0000014e7bff7600>
|
1850
|
+
# a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"]
|
1851
|
+
# a.fill(o, 1, 2)
|
1852
|
+
# # => ["a", #<Object:0x0000014e7bff7600>, #<Object:0x0000014e7bff7600>, "d"]
|
1853
|
+
#
|
1854
|
+
# With a block given, the block is called once for each element to be replaced;
|
1855
|
+
# the value passed to the block is the *index* of the element to be replaced
|
1856
|
+
# (not the element itself); the block's return value replaces the element:
|
1857
|
+
#
|
1858
|
+
# a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"]
|
1859
|
+
# a.fill(1, 2) {|element| element.to_s } # => ["a", "1", "2", "d"]
|
1860
|
+
#
|
1861
|
+
# For arguments `start` and `count`:
|
1862
|
+
#
|
1863
|
+
# * If `start` is non-negative, replaces `count` elements beginning at offset
|
1864
|
+
# `start`:
|
1865
|
+
#
|
1866
|
+
# ['a', 'b', 'c', 'd'].fill('-', 0, 2) # => ["-", "-", "c", "d"]
|
1867
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"]
|
1868
|
+
# ['a', 'b', 'c', 'd'].fill('-', 2, 2) # => ["a", "b", "-", "-"]
|
1869
|
+
#
|
1870
|
+
# ['a', 'b', 'c', 'd'].fill(0, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
|
1871
|
+
# ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1872
|
+
# ['a', 'b', 'c', 'd'].fill(2, 2) {|e| e.to_s } # => ["a", "b", "2", "3"]
|
1873
|
+
#
|
1874
|
+
# Extends `self` if necessary:
|
1875
|
+
#
|
1876
|
+
# ['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"]
|
1877
|
+
# ['a', 'b', 'c', 'd'].fill('-', 4, 2) # => ["a", "b", "c", "d", "-", "-"]
|
1878
|
+
#
|
1879
|
+
# ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"]
|
1880
|
+
# ['a', 'b', 'c', 'd'].fill(4, 2) {|e| e.to_s } # => ["a", "b", "c", "d", "4", "5"]
|
1881
|
+
#
|
1882
|
+
# Fills with `nil` if necessary:
|
1883
|
+
#
|
1884
|
+
# ['a', 'b', 'c', 'd'].fill('-', 5, 2) # => ["a", "b", "c", "d", nil, "-", "-"]
|
1885
|
+
# ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"]
|
1886
|
+
#
|
1887
|
+
# ['a', 'b', 'c', 'd'].fill(5, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, "5", "6"]
|
1888
|
+
# ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"]
|
1889
|
+
#
|
1890
|
+
# Does nothing if `count` is non-positive:
|
1891
|
+
#
|
1892
|
+
# ['a', 'b', 'c', 'd'].fill('-', 2, 0) # => ["a", "b", "c", "d"]
|
1893
|
+
# ['a', 'b', 'c', 'd'].fill('-', 2, -100) # => ["a", "b", "c", "d"]
|
1894
|
+
# ['a', 'b', 'c', 'd'].fill('-', 6, -100) # => ["a", "b", "c", "d"]
|
1895
|
+
#
|
1896
|
+
# ['a', 'b', 'c', 'd'].fill(2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
|
1897
|
+
# ['a', 'b', 'c', 'd'].fill(2, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
|
1898
|
+
# ['a', 'b', 'c', 'd'].fill(6, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
|
1899
|
+
#
|
1900
|
+
# * If `start` is negative, counts backwards from the end of `self`:
|
1901
|
+
#
|
1902
|
+
# ['a', 'b', 'c', 'd'].fill('-', -4, 3) # => ["-", "-", "-", "d"]
|
1903
|
+
# ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"]
|
1904
|
+
#
|
1905
|
+
# ['a', 'b', 'c', 'd'].fill(-4, 3) {|e| e.to_s } # => ["0", "1", "2", "d"]
|
1906
|
+
# ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"]
|
1907
|
+
#
|
1908
|
+
# Extends `self` if necessary:
|
1909
|
+
#
|
1910
|
+
# ['a', 'b', 'c', 'd'].fill('-', -2, 3) # => ["a", "b", "-", "-", "-"]
|
1911
|
+
# ['a', 'b', 'c', 'd'].fill('-', -1, 3) # => ["a", "b", "c", "-", "-", "-"]
|
1912
|
+
#
|
1913
|
+
# ['a', 'b', 'c', 'd'].fill(-2, 3) {|e| e.to_s } # => ["a", "b", "2", "3", "4"]
|
1914
|
+
# ['a', 'b', 'c', 'd'].fill(-1, 3) {|e| e.to_s } # => ["a", "b", "c", "3", "4", "5"]
|
1915
|
+
#
|
1916
|
+
# Starts at the beginning of `self` if `start` is negative and out-of-range:
|
1917
|
+
#
|
1918
|
+
# ['a', 'b', 'c', 'd'].fill('-', -5, 2) # => ["-", "-", "c", "d"]
|
1919
|
+
# ['a', 'b', 'c', 'd'].fill('-', -6, 2) # => ["-", "-", "c", "d"]
|
1920
|
+
#
|
1921
|
+
# ['a', 'b', 'c', 'd'].fill(-5, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
|
1922
|
+
# ['a', 'b', 'c', 'd'].fill(-6, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
|
1923
|
+
#
|
1924
|
+
# Does nothing if `count` is non-positive:
|
1925
|
+
#
|
1926
|
+
# ['a', 'b', 'c', 'd'].fill('-', -2, 0) # => ["a", "b", "c", "d"]
|
1927
|
+
# ['a', 'b', 'c', 'd'].fill('-', -2, -1) # => ["a", "b", "c", "d"]
|
1928
|
+
#
|
1929
|
+
# ['a', 'b', 'c', 'd'].fill(-2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
|
1930
|
+
# ['a', 'b', 'c', 'd'].fill(-2, -1) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
|
1931
|
+
#
|
1932
|
+
# When argument `range` is given, it must be a Range object whose members are
|
1933
|
+
# numeric; its `begin` and `end` values determine the elements of `self` to be
|
1934
|
+
# replaced:
|
1935
|
+
#
|
1936
|
+
# * If both `begin` and `end` are positive, they specify the first and last
|
1937
|
+
# elements to be replaced:
|
1938
|
+
#
|
1939
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"]
|
1940
|
+
# ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1941
|
+
#
|
1942
|
+
# If `end` is smaller than `begin`, replaces no elements:
|
1943
|
+
#
|
1944
|
+
# ['a', 'b', 'c', 'd'].fill('-', 2..1) # => ["a", "b", "c", "d"]
|
1945
|
+
# ['a', 'b', 'c', 'd'].fill(2..1) {|e| e.to_s } # => ["a", "b", "c", "d"]
|
1946
|
+
#
|
1947
|
+
# * If either is negative (or both are negative), counts backwards from the
|
1948
|
+
# end of `self`:
|
1949
|
+
#
|
1950
|
+
# ['a', 'b', 'c', 'd'].fill('-', -3..2) # => ["a", "-", "-", "d"]
|
1951
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1..-2) # => ["a", "-", "-", "d"]
|
1952
|
+
# ['a', 'b', 'c', 'd'].fill('-', -3..-2) # => ["a", "-", "-", "d"]
|
1953
|
+
#
|
1954
|
+
# ['a', 'b', 'c', 'd'].fill(-3..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1955
|
+
# ['a', 'b', 'c', 'd'].fill(1..-2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1956
|
+
# ['a', 'b', 'c', 'd'].fill(-3..-2) {|e| e.to_s } # => ["a", "1", "2", "d"]
|
1957
|
+
#
|
1958
|
+
# * If the `end` value is excluded (see Range#exclude_end?), omits the last
|
1959
|
+
# replacement:
|
1960
|
+
#
|
1961
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1...2) # => ["a", "-", "c", "d"]
|
1962
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1...-2) # => ["a", "-", "c", "d"]
|
1963
|
+
#
|
1964
|
+
# ['a', 'b', 'c', 'd'].fill(1...2) {|e| e.to_s } # => ["a", "1", "c", "d"]
|
1965
|
+
# ['a', 'b', 'c', 'd'].fill(1...-2) {|e| e.to_s } # => ["a", "1", "c", "d"]
|
1966
|
+
#
|
1967
|
+
# * If the range is endless (see [Endless
|
1968
|
+
# Ranges](rdoc-ref:Range@Endless+Ranges)), replaces elements to the end of
|
1969
|
+
# `self`:
|
1970
|
+
#
|
1971
|
+
# ['a', 'b', 'c', 'd'].fill('-', 1..) # => ["a", "-", "-", "-"]
|
1972
|
+
# ['a', 'b', 'c', 'd'].fill(1..) {|e| e.to_s } # => ["a", "1", "2", "3"]
|
1973
|
+
#
|
1974
|
+
# * If the range is beginless (see [Beginless
|
1975
|
+
# Ranges](rdoc-ref:Range@Beginless+Ranges)), replaces elements from the
|
1976
|
+
# beginning of `self`:
|
1977
|
+
#
|
1978
|
+
# ['a', 'b', 'c', 'd'].fill('-', ..2) # => ["-", "-", "-", "d"]
|
1979
|
+
# ['a', 'b', 'c', 'd'].fill(..2) {|e| e.to_s } # => ["0", "1", "2", "d"]
|
1980
|
+
#
|
1981
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
1982
|
+
#
|
1983
|
+
def fill: (Elem obj) -> self
|
1984
|
+
| (Elem obj, int? start, ?int? length) -> self
|
1985
|
+
| (Elem obj, ::Range[::Integer] range) -> self
|
1986
|
+
| (?int? start, ?int? length) { (::Integer index) -> Elem } -> self
|
1987
|
+
| (::Range[::Integer] range) { (::Integer index) -> Elem } -> self
|
1988
|
+
|
1989
|
+
# <!-- rdoc-file=array.c -->
|
1990
|
+
# With a block given, calls the block with each element of `self`; returns a new
|
1991
|
+
# array containing those elements of `self` for which the block returns a truthy
|
1992
|
+
# value:
|
1993
|
+
#
|
1994
|
+
# a = [:foo, 'bar', 2, :bam]
|
1995
|
+
# a.select {|element| element.to_s.start_with?('b') }
|
1996
|
+
# # => ["bar", :bam]
|
1997
|
+
#
|
1998
|
+
# With no block given, returns a new Enumerator.
|
1999
|
+
#
|
2000
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2001
|
+
#
|
2002
|
+
def filter: () { (Elem item) -> boolish } -> ::Array[Elem]
|
2003
|
+
| () -> ::Enumerator[Elem, ::Array[Elem]]
|
2004
|
+
|
2005
|
+
# <!-- rdoc-file=array.c -->
|
2006
|
+
# With a block given, calls the block with each element of `self`; removes from
|
2007
|
+
# `self` those elements for which the block returns `false` or `nil`.
|
2008
|
+
#
|
2009
|
+
# Returns `self` if any elements were removed:
|
2010
|
+
#
|
2011
|
+
# a = [:foo, 'bar', 2, :bam]
|
2012
|
+
# a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
|
2013
|
+
#
|
2014
|
+
# Returns `nil` if no elements were removed.
|
2015
|
+
#
|
2016
|
+
# With no block given, returns a new Enumerator.
|
2017
|
+
#
|
2018
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
2019
|
+
#
|
2020
|
+
def filter!: () { (Elem item) -> boolish } -> self?
|
2021
|
+
| () -> ::Enumerator[Elem, self?]
|
2022
|
+
|
2023
|
+
# <!--
|
2024
|
+
# rdoc-file=array.c
|
2025
|
+
# - find_index(object) -> integer or nil
|
2026
|
+
# - find_index {|element| ... } -> integer or nil
|
2027
|
+
# - find_index -> new_enumerator
|
2028
|
+
# - index(object) -> integer or nil
|
2029
|
+
# - index {|element| ... } -> integer or nil
|
2030
|
+
# - index -> new_enumerator
|
2031
|
+
# -->
|
2032
|
+
# Returns the zero-based integer index of a specified element, or `nil`.
|
2033
|
+
#
|
2034
|
+
# With only argument `object` given, returns the index of the first element
|
2035
|
+
# `element` for which `object == element`:
|
2036
|
+
#
|
2037
|
+
# a = [:foo, 'bar', 2, 'bar']
|
2038
|
+
# a.index('bar') # => 1
|
2039
|
+
#
|
2040
|
+
# Returns `nil` if no such element found.
|
2041
|
+
#
|
2042
|
+
# With only a block given, calls the block with each successive element; returns
|
2043
|
+
# the index of the first element for which the block returns a truthy value:
|
2044
|
+
#
|
2045
|
+
# a = [:foo, 'bar', 2, 'bar']
|
2046
|
+
# a.index {|element| element == 'bar' } # => 1
|
2047
|
+
#
|
2048
|
+
# Returns `nil` if the block never returns a truthy value.
|
2049
|
+
#
|
2050
|
+
# With neither an argument nor a block given, returns a new Enumerator.
|
2051
|
+
#
|
2052
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2053
|
+
#
|
2054
|
+
def find_index: (untyped obj) -> ::Integer?
|
2055
|
+
| () { (Elem item) -> boolish } -> ::Integer?
|
2056
|
+
| () -> ::Enumerator[Elem, ::Integer?]
|
2057
|
+
|
2058
|
+
# <!--
|
2059
|
+
# rdoc-file=array.rb
|
2060
|
+
# - first -> object or nil
|
2061
|
+
# - first(count) -> new_array
|
2062
|
+
# -->
|
2063
|
+
# Returns elements from `self`, or `nil`; does not modify `self`.
|
2064
|
+
#
|
2065
|
+
# With no argument given, returns the first element (if available):
|
2066
|
+
#
|
2067
|
+
# a = [:foo, 'bar', 2]
|
2068
|
+
# a.first # => :foo
|
2069
|
+
# a # => [:foo, "bar", 2]
|
2070
|
+
#
|
2071
|
+
# If `self` is empty, returns `nil`.
|
2072
|
+
#
|
2073
|
+
# [].first # => nil
|
2074
|
+
#
|
2075
|
+
# With a non-negative integer argument `count` given, returns the first `count`
|
2076
|
+
# elements (as available) in a new array:
|
2077
|
+
#
|
2078
|
+
# a.first(0) # => []
|
2079
|
+
# a.first(2) # => [:foo, "bar"]
|
2080
|
+
# a.first(50) # => [:foo, "bar", 2]
|
2081
|
+
#
|
2082
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2083
|
+
#
|
2084
|
+
def first: %a{implicitly-returns-nil} () -> Elem
|
2085
|
+
| (int n) -> ::Array[Elem]
|
2086
|
+
|
2087
|
+
# <!--
|
2088
|
+
# rdoc-file=array.c
|
2089
|
+
# - flatten(depth = nil) -> new_array
|
2090
|
+
# -->
|
2091
|
+
# Returns a new array that is a recursive flattening of `self` to `depth` levels
|
2092
|
+
# of recursion; `depth` must be an [integer-convertible
|
2093
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects) or
|
2094
|
+
# `nil`. At each level of recursion:
|
2095
|
+
#
|
2096
|
+
# * Each element that is an array is "flattened" (that is, replaced by its
|
2097
|
+
# individual array elements).
|
2098
|
+
# * Each element that is not an array is unchanged (even if the element is an
|
2099
|
+
# object that has instance method `flatten`).
|
2100
|
+
#
|
2101
|
+
# With non-negative integer argument `depth`, flattens recursively through
|
2102
|
+
# `depth` levels:
|
2103
|
+
#
|
2104
|
+
# a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ]
|
2105
|
+
# a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
|
2106
|
+
# a.flatten(0) # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
|
2107
|
+
# a.flatten(1 ) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2108
|
+
# a.flatten(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2109
|
+
# a.flatten(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2110
|
+
# a.flatten(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2111
|
+
#
|
2112
|
+
# With `nil` or negative `depth`, flattens all levels.
|
2113
|
+
#
|
2114
|
+
# a.flatten # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2115
|
+
# a.flatten(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2116
|
+
#
|
2117
|
+
# Related: Array#flatten!; see also [Methods for
|
2118
|
+
# Converting](rdoc-ref:Array@Methods+for+Converting).
|
2119
|
+
#
|
2120
|
+
def flatten: (?int level) -> ::Array[untyped]
|
2121
|
+
|
2122
|
+
# <!--
|
2123
|
+
# rdoc-file=array.c
|
2124
|
+
# - flatten!(depth = nil) -> self or nil
|
2125
|
+
# -->
|
2126
|
+
# Returns `self` as a recursively flattening of `self` to `depth` levels of
|
2127
|
+
# recursion; `depth` must be an [integer-convertible
|
2128
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects), or
|
2129
|
+
# `nil`. At each level of recursion:
|
2130
|
+
#
|
2131
|
+
# * Each element that is an array is "flattened" (that is, replaced by its
|
2132
|
+
# individual array elements).
|
2133
|
+
# * Each element that is not an array is unchanged (even if the element is an
|
2134
|
+
# object that has instance method `flatten`).
|
2135
|
+
#
|
2136
|
+
# Returns `nil` if no elements were flattened.
|
2137
|
+
#
|
2138
|
+
# With non-negative integer argument `depth`, flattens recursively through
|
2139
|
+
# `depth` levels:
|
2140
|
+
#
|
2141
|
+
# a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ]
|
2142
|
+
# a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>]
|
2143
|
+
# a.dup.flatten!(1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2144
|
+
# a.dup.flatten!(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2145
|
+
# a.dup.flatten!(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2146
|
+
# a.dup.flatten!(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2147
|
+
#
|
2148
|
+
# With `nil` or negative argument `depth`, flattens all levels:
|
2149
|
+
#
|
2150
|
+
# a.dup.flatten! # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2151
|
+
# a.dup.flatten!(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
|
2152
|
+
#
|
2153
|
+
# Related: Array#flatten; see also [Methods for
|
2154
|
+
# Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2155
|
+
#
|
2156
|
+
def flatten!: (?int level) -> self?
|
2157
|
+
|
2158
|
+
# <!--
|
2159
|
+
# rdoc-file=array.c
|
2160
|
+
# - hash -> integer
|
2161
|
+
# -->
|
2162
|
+
# Returns the integer hash value for `self`.
|
2163
|
+
#
|
2164
|
+
# Two arrays with the same content will have the same hash value (and will
|
2165
|
+
# compare using eql?):
|
2166
|
+
#
|
2167
|
+
# ['a', 'b'].hash == ['a', 'b'].hash # => true
|
2168
|
+
# ['a', 'b'].hash == ['a', 'c'].hash # => false
|
2169
|
+
# ['a', 'b'].hash == ['a'].hash # => false
|
2170
|
+
#
|
2171
|
+
def hash: () -> ::Integer
|
2172
|
+
|
2173
|
+
# <!--
|
2174
|
+
# rdoc-file=array.c
|
2175
|
+
# - include?(object) -> true or false
|
2176
|
+
# -->
|
2177
|
+
# Returns whether for some element `element` in `self`, `object == element`:
|
2178
|
+
#
|
2179
|
+
# [0, 1, 2].include?(2) # => true
|
2180
|
+
# [0, 1, 2].include?(2.0) # => true
|
2181
|
+
# [0, 1, 2].include?(2.1) # => false
|
2182
|
+
#
|
2183
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2184
|
+
#
|
2185
|
+
def include?: (Elem object) -> bool
|
2186
|
+
|
2187
|
+
# <!-- rdoc-file=array.c -->
|
2188
|
+
# Returns the zero-based integer index of a specified element, or `nil`.
|
2189
|
+
#
|
2190
|
+
# With only argument `object` given, returns the index of the first element
|
2191
|
+
# `element` for which `object == element`:
|
2192
|
+
#
|
2193
|
+
# a = [:foo, 'bar', 2, 'bar']
|
2194
|
+
# a.index('bar') # => 1
|
2195
|
+
#
|
2196
|
+
# Returns `nil` if no such element found.
|
2197
|
+
#
|
2198
|
+
# With only a block given, calls the block with each successive element; returns
|
2199
|
+
# the index of the first element for which the block returns a truthy value:
|
2200
|
+
#
|
2201
|
+
# a = [:foo, 'bar', 2, 'bar']
|
2202
|
+
# a.index {|element| element == 'bar' } # => 1
|
2203
|
+
#
|
2204
|
+
# Returns `nil` if the block never returns a truthy value.
|
2205
|
+
#
|
2206
|
+
# With neither an argument nor a block given, returns a new Enumerator.
|
2207
|
+
#
|
2208
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2209
|
+
#
|
2210
|
+
alias index find_index
|
2211
|
+
|
2212
|
+
# <!--
|
2213
|
+
# rdoc-file=array.c
|
2214
|
+
# - insert(index, *objects) -> self
|
2215
|
+
# -->
|
2216
|
+
# Inserts the given `objects` as elements of `self`; returns `self`.
|
2217
|
+
#
|
2218
|
+
# When `index` is non-negative, inserts `objects` *before* the element at offset
|
2219
|
+
# `index`:
|
2220
|
+
#
|
2221
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
2222
|
+
# a.insert(1, :x, :y, :z) # => ["a", :x, :y, :z, "b", "c"]
|
2223
|
+
#
|
2224
|
+
# Extends the array if `index` is beyond the array (`index >= self.size`):
|
2225
|
+
#
|
2226
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
2227
|
+
# a.insert(5, :x, :y, :z) # => ["a", "b", "c", nil, nil, :x, :y, :z]
|
2228
|
+
#
|
2229
|
+
# When `index` is negative, inserts `objects` *after* the element at offset
|
2230
|
+
# `index + self.size`:
|
2231
|
+
#
|
2232
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
2233
|
+
# a.insert(-2, :x, :y, :z) # => ["a", "b", :x, :y, :z, "c"]
|
2234
|
+
#
|
2235
|
+
# With no `objects` given, does nothing:
|
2236
|
+
#
|
2237
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
2238
|
+
# a.insert(1) # => ["a", "b", "c"]
|
2239
|
+
# a.insert(50) # => ["a", "b", "c"]
|
2240
|
+
# a.insert(-50) # => ["a", "b", "c"]
|
2241
|
+
#
|
2242
|
+
# Raises IndexError if `objects` are given and `index` is negative and out of
|
2243
|
+
# range.
|
2244
|
+
#
|
2245
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2246
|
+
#
|
2247
|
+
def insert: (int index, *Elem obj) -> self
|
2248
|
+
|
2249
|
+
# <!--
|
2250
|
+
# rdoc-file=array.c
|
2251
|
+
# - inspect -> new_string
|
2252
|
+
# - to_s -> new_string
|
2253
|
+
# -->
|
2254
|
+
# Returns the new string formed by calling method `#inspect` on each array
|
2255
|
+
# element:
|
2256
|
+
#
|
2257
|
+
# a = [:foo, 'bar', 2]
|
2258
|
+
# a.inspect # => "[:foo, \"bar\", 2]"
|
2259
|
+
#
|
2260
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
2261
|
+
#
|
2262
|
+
def inspect: () -> String
|
2263
|
+
|
2264
|
+
# <!--
|
2265
|
+
# rdoc-file=array.c
|
2266
|
+
# - intersect?(other_array) -> true or false
|
2267
|
+
# -->
|
2268
|
+
# Returns whether `other_array` has at least one element that is `#eql?` to some
|
2269
|
+
# element of `self`:
|
2270
|
+
#
|
2271
|
+
# [1, 2, 3].intersect?([3, 4, 5]) # => true
|
2272
|
+
# [1, 2, 3].intersect?([4, 5, 6]) # => false
|
2273
|
+
#
|
2274
|
+
# Each element must correctly implement method `#hash`.
|
2275
|
+
#
|
2276
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2277
|
+
#
|
2278
|
+
def intersect?: (_ToAry[untyped]) -> bool
|
2279
|
+
|
2280
|
+
# <!--
|
2281
|
+
# rdoc-file=array.c
|
2282
|
+
# - intersection(*other_arrays) -> new_array
|
2283
|
+
# -->
|
2284
|
+
# Returns a new array containing each element in `self` that is `#eql?` to at
|
2285
|
+
# least one element in each of the given `other_arrays`; duplicates are omitted:
|
2286
|
+
#
|
2287
|
+
# [0, 0, 1, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
|
2288
|
+
#
|
2289
|
+
# Each element must correctly implement method `#hash`.
|
2290
|
+
#
|
2291
|
+
# Order from `self` is preserved:
|
2292
|
+
#
|
2293
|
+
# [0, 1, 2].intersection([2, 1, 0]) # => [0, 1, 2]
|
2294
|
+
#
|
2295
|
+
# Returns a copy of `self` if no arguments are given.
|
2296
|
+
#
|
2297
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
2298
|
+
#
|
2299
|
+
def intersection: (*::Array[untyped] | _ToAry[untyped] other_ary) -> ::Array[Elem]
|
2300
|
+
|
2301
|
+
# <!--
|
2302
|
+
# rdoc-file=array.c
|
2303
|
+
# - array.join(separator = $,) -> new_string
|
2304
|
+
# -->
|
2305
|
+
# Returns the new string formed by joining the converted elements of `self`; for
|
2306
|
+
# each element `element`:
|
2307
|
+
#
|
2308
|
+
# * Converts recursively using `element.join(separator)` if `element` is a
|
2309
|
+
# `kind_of?(Array)`.
|
2310
|
+
# * Otherwise, converts using `element.to_s`.
|
2311
|
+
#
|
2312
|
+
# With no argument given, joins using the output field separator, `$,`:
|
2313
|
+
#
|
2314
|
+
# a = [:foo, 'bar', 2]
|
2315
|
+
# $, # => nil
|
2316
|
+
# a.join # => "foobar2"
|
2317
|
+
#
|
2318
|
+
# With string argument `separator` given, joins using that separator:
|
2319
|
+
#
|
2320
|
+
# a = [:foo, 'bar', 2]
|
2321
|
+
# a.join("\n") # => "foo\nbar\n2"
|
2322
|
+
#
|
2323
|
+
# Joins recursively for nested arrays:
|
2324
|
+
#
|
2325
|
+
# a = [:foo, [:bar, [:baz, :bat]]]
|
2326
|
+
# a.join # => "foobarbazbat"
|
2327
|
+
#
|
2328
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
2329
|
+
#
|
2330
|
+
def join: (?string separator) -> String
|
2331
|
+
|
2332
|
+
# <!--
|
2333
|
+
# rdoc-file=array.c
|
2334
|
+
# - keep_if {|element| ... } -> self
|
2335
|
+
# - keep_if -> new_enumerator
|
2336
|
+
# -->
|
2337
|
+
# With a block given, calls the block with each element of `self`; removes the
|
2338
|
+
# element from `self` if the block does not return a truthy value:
|
2339
|
+
#
|
2340
|
+
# a = [:foo, 'bar', 2, :bam]
|
2341
|
+
# a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
|
2342
|
+
#
|
2343
|
+
# With no block given, returns a new Enumerator.
|
2344
|
+
#
|
2345
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
2346
|
+
#
|
2347
|
+
def keep_if: () { (Elem item) -> boolish } -> self
|
2348
|
+
| () -> ::Enumerator[Elem, self]
|
2349
|
+
|
2350
|
+
# <!--
|
2351
|
+
# rdoc-file=array.rb
|
2352
|
+
# - last -> last_object or nil
|
2353
|
+
# - last(count) -> new_array
|
2354
|
+
# -->
|
2355
|
+
# Returns elements from `self`, or `nil`; `self` is not modified.
|
2356
|
+
#
|
2357
|
+
# With no argument given, returns the last element, or `nil` if `self` is empty:
|
2358
|
+
#
|
2359
|
+
# a = [:foo, 'bar', 2]
|
2360
|
+
# a.last # => 2
|
2361
|
+
# a # => [:foo, "bar", 2]
|
2362
|
+
# [].last # => nil
|
2363
|
+
#
|
2364
|
+
# With non-negative integer argument `count` given, returns a new array
|
2365
|
+
# containing the trailing `count` elements of `self`, as available:
|
2366
|
+
#
|
2367
|
+
# a = [:foo, 'bar', 2]
|
2368
|
+
# a.last(2) # => ["bar", 2]
|
2369
|
+
# a.last(50) # => [:foo, "bar", 2]
|
2370
|
+
# a.last(0) # => []
|
2371
|
+
# [].last(3) # => []
|
2372
|
+
#
|
2373
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2374
|
+
#
|
2375
|
+
def last: %a{implicitly-returns-nil} () -> Elem
|
2376
|
+
| (int n) -> ::Array[Elem]
|
2377
|
+
|
2378
|
+
# <!--
|
2379
|
+
# rdoc-file=array.c
|
2380
|
+
# - length -> integer
|
2381
|
+
# - size -> integer
|
2382
|
+
# -->
|
2383
|
+
# Returns the count of elements in `self`:
|
2384
|
+
#
|
2385
|
+
# [0, 1, 2].length # => 3
|
2386
|
+
# [].length # => 0
|
2387
|
+
#
|
2388
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2389
|
+
#
|
2390
|
+
def length: () -> ::Integer
|
2391
|
+
|
2392
|
+
# <!-- rdoc-file=array.c -->
|
2393
|
+
# With a block given, calls the block with each element of `self`; returns a new
|
2394
|
+
# array whose elements are the return values from the block:
|
2395
|
+
#
|
2396
|
+
# a = [:foo, 'bar', 2]
|
2397
|
+
# a1 = a.map {|element| element.class }
|
2398
|
+
# a1 # => [Symbol, String, Integer]
|
2399
|
+
#
|
2400
|
+
# With no block given, returns a new Enumerator.
|
2401
|
+
#
|
2402
|
+
# Related: #collect!; see also [Methods for
|
2403
|
+
# Converting](rdoc-ref:Array@Methods+for+Converting).
|
2404
|
+
#
|
2405
|
+
alias map collect
|
2406
|
+
|
2407
|
+
# <!-- rdoc-file=array.c -->
|
2408
|
+
# With a block given, calls the block with each element of `self` and replaces
|
2409
|
+
# the element with the block's return value; returns `self`:
|
2410
|
+
#
|
2411
|
+
# a = [:foo, 'bar', 2]
|
2412
|
+
# a.map! { |element| element.class } # => [Symbol, String, Integer]
|
2413
|
+
#
|
2414
|
+
# With no block given, returns a new Enumerator.
|
2415
|
+
#
|
2416
|
+
# Related: #collect; see also [Methods for
|
2417
|
+
# Converting](rdoc-ref:Array@Methods+for+Converting).
|
2418
|
+
#
|
2419
|
+
alias map! collect!
|
2420
|
+
|
2421
|
+
# <!--
|
2422
|
+
# rdoc-file=array.c
|
2423
|
+
# - max -> element
|
2424
|
+
# - max(count) -> new_array
|
2425
|
+
# - max {|a, b| ... } -> element
|
2426
|
+
# - max(count) {|a, b| ... } -> new_array
|
2427
|
+
# -->
|
2428
|
+
# Returns one of the following:
|
2429
|
+
#
|
2430
|
+
# * The maximum-valued element from `self`.
|
2431
|
+
# * A new array of maximum-valued elements from `self`.
|
2432
|
+
#
|
2433
|
+
# Does not modify `self`.
|
2434
|
+
#
|
2435
|
+
# With no block given, each element in `self` must respond to method `#<=>` with
|
2436
|
+
# a numeric.
|
2437
|
+
#
|
2438
|
+
# With no argument and no block, returns the element in `self` having the
|
2439
|
+
# maximum value per method `#<=>`:
|
2440
|
+
#
|
2441
|
+
# [1, 0, 3, 2].max # => 3
|
2442
|
+
#
|
2443
|
+
# With non-negative numeric argument `count` and no block, returns a new array
|
2444
|
+
# with at most `count` elements, in descending order, per method `#<=>`:
|
2445
|
+
#
|
2446
|
+
# [1, 0, 3, 2].max(3) # => [3, 2, 1]
|
2447
|
+
# [1, 0, 3, 2].max(3.0) # => [3, 2, 1]
|
2448
|
+
# [1, 0, 3, 2].max(9) # => [3, 2, 1, 0]
|
2449
|
+
# [1, 0, 3, 2].max(0) # => []
|
2450
|
+
#
|
2451
|
+
# With a block given, the block must return a numeric.
|
2452
|
+
#
|
2453
|
+
# With a block and no argument, calls the block `self.size - 1` times to compare
|
2454
|
+
# elements; returns the element having the maximum value per the block:
|
2455
|
+
#
|
2456
|
+
# ['0', '', '000', '00'].max {|a, b| a.size <=> b.size }
|
2457
|
+
# # => "000"
|
2458
|
+
#
|
2459
|
+
# With non-negative numeric argument `count` and a block, returns a new array
|
2460
|
+
# with at most `count` elements, in descending order, per the block:
|
2461
|
+
#
|
2462
|
+
# ['0', '', '000', '00'].max(2) {|a, b| a.size <=> b.size }
|
2463
|
+
# # => ["000", "00"]
|
2464
|
+
#
|
2465
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2466
|
+
#
|
2467
|
+
def max: %a{implicitly-returns-nil} () -> Elem
|
2468
|
+
| %a{implicitly-returns-nil} () { (Elem a, Elem b) -> ::Integer? } -> Elem
|
2469
|
+
| (int n) -> ::Array[Elem]
|
2470
|
+
| (int n) { (Elem a, Elem b) -> ::Integer? } -> ::Array[Elem]
|
2471
|
+
|
2472
|
+
# <!--
|
2473
|
+
# rdoc-file=array.c
|
2474
|
+
# - min -> element
|
2475
|
+
# - min(count) -> new_array
|
2476
|
+
# - min {|a, b| ... } -> element
|
2477
|
+
# - min(count) {|a, b| ... } -> new_array
|
2478
|
+
# -->
|
2479
|
+
# Returns one of the following:
|
2480
|
+
#
|
2481
|
+
# * The minimum-valued element from `self`.
|
2482
|
+
# * A new array of minimum-valued elements from `self`.
|
2483
|
+
#
|
2484
|
+
# Does not modify `self`.
|
2485
|
+
#
|
2486
|
+
# With no block given, each element in `self` must respond to method `#<=>` with
|
2487
|
+
# a numeric.
|
2488
|
+
#
|
2489
|
+
# With no argument and no block, returns the element in `self` having the
|
2490
|
+
# minimum value per method `#<=>`:
|
2491
|
+
#
|
2492
|
+
# [1, 0, 3, 2].min # => 0
|
2493
|
+
#
|
2494
|
+
# With non-negative numeric argument `count` and no block, returns a new array
|
2495
|
+
# with at most `count` elements, in ascending order, per method `#<=>`:
|
2496
|
+
#
|
2497
|
+
# [1, 0, 3, 2].min(3) # => [0, 1, 2]
|
2498
|
+
# [1, 0, 3, 2].min(3.0) # => [0, 1, 2]
|
2499
|
+
# [1, 0, 3, 2].min(9) # => [0, 1, 2, 3]
|
2500
|
+
# [1, 0, 3, 2].min(0) # => []
|
2501
|
+
#
|
2502
|
+
# With a block given, the block must return a numeric.
|
2503
|
+
#
|
2504
|
+
# With a block and no argument, calls the block `self.size - 1` times to compare
|
2505
|
+
# elements; returns the element having the minimum value per the block:
|
2506
|
+
#
|
2507
|
+
# ['0', '', '000', '00'].min {|a, b| a.size <=> b.size }
|
2508
|
+
# # => ""
|
2509
|
+
#
|
2510
|
+
# With non-negative numeric argument `count` and a block, returns a new array
|
2511
|
+
# with at most `count` elements, in ascending order, per the block:
|
2512
|
+
#
|
2513
|
+
# ['0', '', '000', '00'].min(2) {|a, b| a.size <=> b.size }
|
2514
|
+
# # => ["", "0"]
|
2515
|
+
#
|
2516
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2517
|
+
#
|
2518
|
+
alias min max
|
2519
|
+
|
2520
|
+
# <!--
|
2521
|
+
# rdoc-file=array.c
|
2522
|
+
# - minmax -> array
|
2523
|
+
# - minmax {|a, b| ... } -> array
|
2524
|
+
# -->
|
2525
|
+
# Returns a 2-element array containing the minimum-valued and maximum-valued
|
2526
|
+
# elements from `self`; does not modify `self`.
|
2527
|
+
#
|
2528
|
+
# With no block given, the minimum and maximum values are determined using
|
2529
|
+
# method `#<=>`:
|
2530
|
+
#
|
2531
|
+
# [1, 0, 3, 2].minmax # => [0, 3]
|
2532
|
+
#
|
2533
|
+
# With a block given, the block must return a numeric; the block is called
|
2534
|
+
# `self.size - 1` times to compare elements; returns the elements having the
|
2535
|
+
# minimum and maximum values per the block:
|
2536
|
+
#
|
2537
|
+
# ['0', '', '000', '00'].minmax {|a, b| a.size <=> b.size }
|
2538
|
+
# # => ["", "000"]
|
2539
|
+
#
|
2540
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2541
|
+
#
|
2542
|
+
def minmax: () -> [ Elem?, Elem? ]
|
2543
|
+
| () { (Elem a, Elem b) -> ::Integer? } -> [ Elem?, Elem? ]
|
2544
|
+
|
2545
|
+
# <!--
|
2546
|
+
# rdoc-file=array.c
|
2547
|
+
# - none? -> true or false
|
2548
|
+
# - none?(object) -> true or false
|
2549
|
+
# - none? {|element| ... } -> true or false
|
2550
|
+
# -->
|
2551
|
+
# Returns `true` if no element of `self` meets a given criterion, `false`
|
2552
|
+
# otherwise.
|
2553
|
+
#
|
2554
|
+
# With no block given and no argument, returns `true` if `self` has no truthy
|
2555
|
+
# elements, `false` otherwise:
|
2556
|
+
#
|
2557
|
+
# [nil, false].none? # => true
|
2558
|
+
# [nil, 0, false].none? # => false
|
2559
|
+
# [].none? # => true
|
2560
|
+
#
|
2561
|
+
# With argument `object` given, returns `false` if for any element `element`,
|
2562
|
+
# `object === element`; `true` otherwise:
|
2563
|
+
#
|
2564
|
+
# ['food', 'drink'].none?(/bar/) # => true
|
2565
|
+
# ['food', 'drink'].none?(/foo/) # => false
|
2566
|
+
# [].none?(/foo/) # => true
|
2567
|
+
# [0, 1, 2].none?(3) # => true
|
2568
|
+
# [0, 1, 2].none?(1) # => false
|
2569
|
+
#
|
2570
|
+
# With a block given, calls the block with each element in `self`; returns
|
2571
|
+
# `true` if the block returns no truthy value, `false` otherwise:
|
2572
|
+
#
|
2573
|
+
# [0, 1, 2].none? {|element| element > 3 } # => true
|
2574
|
+
# [0, 1, 2].none? {|element| element > 1 } # => false
|
2575
|
+
#
|
2576
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2577
|
+
#
|
2578
|
+
alias none? all?
|
2579
|
+
|
2580
|
+
# <!--
|
2581
|
+
# rdoc-file=array.c
|
2582
|
+
# - one? -> true or false
|
2583
|
+
# - one? {|element| ... } -> true or false
|
2584
|
+
# - one?(object) -> true or false
|
2585
|
+
# -->
|
2586
|
+
# Returns `true` if exactly one element of `self` meets a given criterion.
|
2587
|
+
#
|
2588
|
+
# With no block given and no argument, returns `true` if `self` has exactly one
|
2589
|
+
# truthy element, `false` otherwise:
|
2590
|
+
#
|
2591
|
+
# [nil, 0].one? # => true
|
2592
|
+
# [0, 0].one? # => false
|
2593
|
+
# [nil, nil].one? # => false
|
2594
|
+
# [].one? # => false
|
2595
|
+
#
|
2596
|
+
# With a block given, calls the block with each element in `self`; returns
|
2597
|
+
# `true` if the block a truthy value for exactly one element, `false` otherwise:
|
2598
|
+
#
|
2599
|
+
# [0, 1, 2].one? {|element| element > 0 } # => false
|
2600
|
+
# [0, 1, 2].one? {|element| element > 1 } # => true
|
2601
|
+
# [0, 1, 2].one? {|element| element > 2 } # => false
|
2602
|
+
#
|
2603
|
+
# With argument `object` given, returns `true` if for exactly one element
|
2604
|
+
# `element`, `object === element`; `false` otherwise:
|
2605
|
+
#
|
2606
|
+
# [0, 1, 2].one?(0) # => true
|
2607
|
+
# [0, 0, 1].one?(0) # => false
|
2608
|
+
# [1, 1, 2].one?(0) # => false
|
2609
|
+
# ['food', 'drink'].one?(/bar/) # => false
|
2610
|
+
# ['food', 'drink'].one?(/foo/) # => true
|
2611
|
+
# [].one?(/foo/) # => false
|
2612
|
+
#
|
2613
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
2614
|
+
#
|
2615
|
+
alias one? none?
|
2616
|
+
|
2617
|
+
# <!--
|
2618
|
+
# rdoc-file=pack.rb
|
2619
|
+
# - pack(template, buffer: nil) -> string
|
2620
|
+
# -->
|
2621
|
+
# Formats each element in `self` into a binary string; returns that string. See
|
2622
|
+
# [Packed Data](rdoc-ref:packed_data.rdoc).
|
2623
|
+
#
|
2624
|
+
def pack: (string fmt, ?buffer: String?) -> String
|
2625
|
+
|
2626
|
+
# <!--
|
2627
|
+
# rdoc-file=array.c
|
2628
|
+
# - permutation(count = self.size) {|permutation| ... } -> self
|
2629
|
+
# - permutation(count = self.size) -> new_enumerator
|
2630
|
+
# -->
|
2631
|
+
# Iterates over permutations of the elements of `self`; the order of
|
2632
|
+
# permutations is indeterminate.
|
2633
|
+
#
|
2634
|
+
# With a block and an in-range positive integer argument `count` (`0 < count <=
|
2635
|
+
# self.size`) given, calls the block with each permutation of `self` of size
|
2636
|
+
# `count`; returns `self`:
|
2637
|
+
#
|
2638
|
+
# a = [0, 1, 2]
|
2639
|
+
# perms = []
|
2640
|
+
# a.permutation(1) {|perm| perms.push(perm) }
|
2641
|
+
# perms # => [[0], [1], [2]]
|
2642
|
+
#
|
2643
|
+
# perms = []
|
2644
|
+
# a.permutation(2) {|perm| perms.push(perm) }
|
2645
|
+
# perms # => [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
|
2646
|
+
#
|
2647
|
+
# perms = []
|
2648
|
+
# a.permutation(3) {|perm| perms.push(perm) }
|
2649
|
+
# perms # => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
|
2650
|
+
#
|
2651
|
+
# When `count` is zero, calls the block once with a new empty array:
|
2652
|
+
#
|
2653
|
+
# perms = []
|
2654
|
+
# a.permutation(0) {|perm| perms.push(perm) }
|
2655
|
+
# perms # => [[]]
|
2656
|
+
#
|
2657
|
+
# When `count` is out of range (negative or larger than `self.size`), does not
|
2658
|
+
# call the block:
|
2659
|
+
#
|
2660
|
+
# a.permutation(-1) {|permutation| fail 'Cannot happen' }
|
2661
|
+
# a.permutation(4) {|permutation| fail 'Cannot happen' }
|
2662
|
+
#
|
2663
|
+
# With no block given, returns a new Enumerator.
|
2664
|
+
#
|
2665
|
+
# Related: [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
2666
|
+
#
|
2667
|
+
def permutation: (?int n) -> ::Enumerator[::Array[Elem], ::Array[Elem]]
|
2668
|
+
| (?int n) { (::Array[Elem] p) -> void } -> ::Array[Elem]
|
2669
|
+
|
2670
|
+
# <!--
|
2671
|
+
# rdoc-file=array.c
|
2672
|
+
# - pop -> object or nil
|
2673
|
+
# - pop(count) -> new_array
|
2674
|
+
# -->
|
2675
|
+
# Removes and returns trailing elements of `self`.
|
2676
|
+
#
|
2677
|
+
# With no argument given, removes and returns the last element, if available;
|
2678
|
+
# otherwise returns `nil`:
|
2679
|
+
#
|
2680
|
+
# a = [:foo, 'bar', 2]
|
2681
|
+
# a.pop # => 2
|
2682
|
+
# a # => [:foo, "bar"]
|
2683
|
+
# [].pop # => nil
|
2684
|
+
#
|
2685
|
+
# With non-negative integer argument `count` given, returns a new array
|
2686
|
+
# containing the trailing `count` elements of `self`, as available:
|
2687
|
+
#
|
2688
|
+
# a = [:foo, 'bar', 2]
|
2689
|
+
# a.pop(2) # => ["bar", 2]
|
2690
|
+
# a # => [:foo]
|
2691
|
+
#
|
2692
|
+
# a = [:foo, 'bar', 2]
|
2693
|
+
# a.pop(50) # => [:foo, "bar", 2]
|
2694
|
+
# a # => []
|
2695
|
+
#
|
2696
|
+
# Related: Array#push; see also [Methods for
|
2697
|
+
# Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
2698
|
+
#
|
2699
|
+
def pop: () -> Elem?
|
2700
|
+
| (int n) -> ::Array[Elem]
|
2701
|
+
|
2702
|
+
# <!-- rdoc-file=array.c -->
|
2703
|
+
# Prepends the given `objects` to `self`:
|
2704
|
+
#
|
2705
|
+
# a = [:foo, 'bar', 2]
|
2706
|
+
# a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
|
2707
|
+
#
|
2708
|
+
# Related: Array#shift; see also [Methods for
|
2709
|
+
# Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2710
|
+
#
|
2711
|
+
alias prepend unshift
|
2712
|
+
|
2713
|
+
# <!--
|
2714
|
+
# rdoc-file=array.c
|
2715
|
+
# - product(*other_arrays) -> new_array
|
2716
|
+
# - product(*other_arrays) {|combination| ... } -> self
|
2717
|
+
# -->
|
2718
|
+
# Computes all combinations of elements from all the arrays, including both
|
2719
|
+
# `self` and `other_arrays`:
|
2720
|
+
#
|
2721
|
+
# * The number of combinations is the product of the sizes of all the arrays,
|
2722
|
+
# including both `self` and `other_arrays`.
|
2723
|
+
# * The order of the returned combinations is indeterminate.
|
2724
|
+
#
|
2725
|
+
# With no block given, returns the combinations as an array of arrays:
|
2726
|
+
#
|
2727
|
+
# p = [0, 1].product([2, 3])
|
2728
|
+
# # => [[0, 2], [0, 3], [1, 2], [1, 3]]
|
2729
|
+
# p.size # => 4
|
2730
|
+
# p = [0, 1].product([2, 3], [4, 5])
|
2731
|
+
# # => [[0, 2, 4], [0, 2, 5], [0, 3, 4], [0, 3, 5], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3,...
|
2732
|
+
# p.size # => 8
|
2733
|
+
#
|
2734
|
+
# If `self` or any argument is empty, returns an empty array:
|
2735
|
+
#
|
2736
|
+
# [].product([2, 3], [4, 5]) # => []
|
2737
|
+
# [0, 1].product([2, 3], []) # => []
|
2738
|
+
#
|
2739
|
+
# If no argument is given, returns an array of 1-element arrays, each containing
|
2740
|
+
# an element of `self`:
|
2741
|
+
#
|
2742
|
+
# a.product # => [[0], [1], [2]]
|
2743
|
+
#
|
2744
|
+
# With a block given, calls the block with each combination; returns `self`:
|
2745
|
+
#
|
2746
|
+
# p = []
|
2747
|
+
# [0, 1].product([2, 3]) {|combination| p.push(combination) }
|
2748
|
+
# p # => [[0, 2], [0, 3], [1, 2], [1, 3]]
|
2749
|
+
#
|
2750
|
+
# If `self` or any argument is empty, does not call the block:
|
2751
|
+
#
|
2752
|
+
# [].product([2, 3], [4, 5]) {|combination| fail 'Cannot happen' }
|
2753
|
+
# # => []
|
2754
|
+
# [0, 1].product([2, 3], []) {|combination| fail 'Cannot happen' }
|
2755
|
+
# # => [0, 1]
|
2756
|
+
#
|
2757
|
+
# If no argument is given, calls the block with each element of `self` as a
|
2758
|
+
# 1-element array:
|
2759
|
+
#
|
2760
|
+
# p = []
|
2761
|
+
# [0, 1].product {|combination| p.push(combination) }
|
2762
|
+
# p # => [[0], [1]]
|
2763
|
+
#
|
2764
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
2765
|
+
#
|
2766
|
+
def product: () -> ::Array[[ Elem ]]
|
2767
|
+
| [X] (::Array[X] other_ary) -> ::Array[[ Elem, X ]]
|
2768
|
+
| [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[ Elem, X, Y ]]
|
2769
|
+
| [U] (*::Array[U] other_arys) -> ::Array[::Array[Elem | U]]
|
2770
|
+
|
2771
|
+
# <!--
|
2772
|
+
# rdoc-file=array.c
|
2773
|
+
# - push(*objects) -> self
|
2774
|
+
# - append(*objects) -> self
|
2775
|
+
# -->
|
2776
|
+
# Appends each argument in `objects` to `self`; returns `self`:
|
2777
|
+
#
|
2778
|
+
# a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
|
2779
|
+
# a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
|
2780
|
+
#
|
2781
|
+
# Appends each argument as a single element, even if it is another array:
|
2782
|
+
#
|
2783
|
+
# a = [:foo, 'bar', 2] # => [:foo, "bar", 2]
|
2784
|
+
# a.push([:baz, :bat], [:bam, :bad]) # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
|
2785
|
+
#
|
2786
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2787
|
+
#
|
2788
|
+
def push: (*Elem obj) -> self
|
2789
|
+
|
2790
|
+
# <!--
|
2791
|
+
# rdoc-file=array.c
|
2792
|
+
# - rassoc(object) -> found_array or nil
|
2793
|
+
# -->
|
2794
|
+
# Returns the first element `ele` in `self` such that `ele` is an array and
|
2795
|
+
# `ele[1] == object`:
|
2796
|
+
#
|
2797
|
+
# a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]]
|
2798
|
+
# a.rassoc(4) # => [2, 4]
|
2799
|
+
# a.rassoc(5) # => [4, 5, 6]
|
2800
|
+
#
|
2801
|
+
# Returns `nil` if no such element is found.
|
2802
|
+
#
|
2803
|
+
# Related: Array#assoc; see also [Methods for
|
2804
|
+
# Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2805
|
+
#
|
2806
|
+
alias rassoc assoc
|
2807
|
+
|
2808
|
+
# <!--
|
2809
|
+
# rdoc-file=array.c
|
2810
|
+
# - reject {|element| ... } -> new_array
|
2811
|
+
# - reject -> new_enumerator
|
2812
|
+
# -->
|
2813
|
+
# With a block given, returns a new array whose elements are all those from
|
2814
|
+
# `self` for which the block returns `false` or `nil`:
|
2815
|
+
#
|
2816
|
+
# a = [:foo, 'bar', 2, 'bat']
|
2817
|
+
# a1 = a.reject {|element| element.to_s.start_with?('b') }
|
2818
|
+
# a1 # => [:foo, 2]
|
2819
|
+
#
|
2820
|
+
# With no block given, returns a new Enumerator.
|
2821
|
+
#
|
2822
|
+
# Related: [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
2823
|
+
#
|
2824
|
+
alias reject delete_if
|
2825
|
+
|
2826
|
+
# <!--
|
2827
|
+
# rdoc-file=array.c
|
2828
|
+
# - reject! {|element| ... } -> self or nil
|
2829
|
+
# - reject! -> new_enumerator
|
2830
|
+
# -->
|
2831
|
+
# With a block given, calls the block with each element of `self`; removes each
|
2832
|
+
# element for which the block returns a truthy value.
|
2833
|
+
#
|
2834
|
+
# Returns `self` if any elements removed:
|
2835
|
+
#
|
2836
|
+
# a = [:foo, 'bar', 2, 'bat']
|
2837
|
+
# a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
|
2838
|
+
#
|
2839
|
+
# Returns `nil` if no elements removed.
|
2840
|
+
#
|
2841
|
+
# With no block given, returns a new Enumerator.
|
2842
|
+
#
|
2843
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
2844
|
+
#
|
2845
|
+
def reject!: () { (Elem item) -> boolish } -> self?
|
2846
|
+
| () -> ::Enumerator[Elem, self?]
|
2847
|
+
|
2848
|
+
# <!--
|
2849
|
+
# rdoc-file=array.c
|
2850
|
+
# - repeated_combination(size) {|combination| ... } -> self
|
2851
|
+
# - repeated_combination(size) -> new_enumerator
|
2852
|
+
# -->
|
2853
|
+
# With a block given, calls the block with each repeated combination of length
|
2854
|
+
# `size` of the elements of `self`; each combination is an array; returns
|
2855
|
+
# `self`. The order of the combinations is indeterminate.
|
2856
|
+
#
|
2857
|
+
# If a positive integer argument `size` is given, calls the block with each
|
2858
|
+
# `size`-tuple repeated combination of the elements of `self`. The number of
|
2859
|
+
# combinations is `(size+1)(size+2)/2`.
|
2860
|
+
#
|
2861
|
+
# Examples:
|
2862
|
+
#
|
2863
|
+
# * `size` is 1:
|
2864
|
+
#
|
2865
|
+
# c = []
|
2866
|
+
# [0, 1, 2].repeated_combination(1) {|combination| c.push(combination) }
|
2867
|
+
# c # => [[0], [1], [2]]
|
2868
|
+
#
|
2869
|
+
# * `size` is 2:
|
2870
|
+
#
|
2871
|
+
# c = []
|
2872
|
+
# [0, 1, 2].repeated_combination(2) {|combination| c.push(combination) }
|
2873
|
+
# c # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
|
2874
|
+
#
|
2875
|
+
# If `size` is zero, calls the block once with an empty array.
|
2876
|
+
#
|
2877
|
+
# If `size` is negative, does not call the block:
|
2878
|
+
#
|
2879
|
+
# [0, 1, 2].repeated_combination(-1) {|combination| fail 'Cannot happen' }
|
2880
|
+
#
|
2881
|
+
# With no block given, returns a new Enumerator.
|
2882
|
+
#
|
2883
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
2884
|
+
#
|
2885
|
+
def repeated_combination: (int n) { (::Array[Elem] c) -> void } -> self
|
2886
|
+
| (int n) -> ::Enumerator[::Array[Elem], self]
|
2887
|
+
|
2888
|
+
# <!--
|
2889
|
+
# rdoc-file=array.c
|
2890
|
+
# - repeated_permutation(size) {|permutation| ... } -> self
|
2891
|
+
# - repeated_permutation(size) -> new_enumerator
|
2892
|
+
# -->
|
2893
|
+
# With a block given, calls the block with each repeated permutation of length
|
2894
|
+
# `size` of the elements of `self`; each permutation is an array; returns
|
2895
|
+
# `self`. The order of the permutations is indeterminate.
|
2896
|
+
#
|
2897
|
+
# If a positive integer argument `size` is given, calls the block with each
|
2898
|
+
# `size`-tuple repeated permutation of the elements of `self`. The number of
|
2899
|
+
# permutations is `self.size**size`.
|
2900
|
+
#
|
2901
|
+
# Examples:
|
2902
|
+
#
|
2903
|
+
# * `size` is 1:
|
2904
|
+
#
|
2905
|
+
# p = []
|
2906
|
+
# [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) }
|
2907
|
+
# p # => [[0], [1], [2]]
|
2908
|
+
#
|
2909
|
+
# * `size` is 2:
|
2910
|
+
#
|
2911
|
+
# p = []
|
2912
|
+
# [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) }
|
2913
|
+
# p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
|
2914
|
+
#
|
2915
|
+
# If `size` is zero, calls the block once with an empty array.
|
2916
|
+
#
|
2917
|
+
# If `size` is negative, does not call the block:
|
2918
|
+
#
|
2919
|
+
# [0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
|
2920
|
+
#
|
2921
|
+
# With no block given, returns a new Enumerator.
|
2922
|
+
#
|
2923
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
2924
|
+
#
|
2925
|
+
def repeated_permutation: (int n) { (::Array[Elem] p) -> void } -> self
|
2926
|
+
| (int n) -> ::Enumerator[::Array[Elem], self]
|
2927
|
+
|
2928
|
+
# <!-- rdoc-file=array.c -->
|
2929
|
+
# Replaces the elements of `self` with the elements of `other_array`, which must
|
2930
|
+
# be an [array-convertible
|
2931
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Array-Convertible+Objects); returns
|
2932
|
+
# `self`:
|
2933
|
+
#
|
2934
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
2935
|
+
# a.replace(['d', 'e']) # => ["d", "e"]
|
2936
|
+
#
|
2937
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2938
|
+
#
|
2939
|
+
def replace: (::Array[Elem]) -> self
|
2940
|
+
|
2941
|
+
# <!--
|
2942
|
+
# rdoc-file=array.c
|
2943
|
+
# - reverse -> new_array
|
2944
|
+
# -->
|
2945
|
+
# Returns a new array containing the elements of `self` in reverse order:
|
2946
|
+
#
|
2947
|
+
# [0, 1, 2].reverse # => [2, 1, 0]
|
2948
|
+
#
|
2949
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
2950
|
+
#
|
2951
|
+
def reverse: () -> ::Array[Elem]
|
2952
|
+
|
2953
|
+
# <!--
|
2954
|
+
# rdoc-file=array.c
|
2955
|
+
# - reverse! -> self
|
2956
|
+
# -->
|
2957
|
+
# Reverses the order of the elements of `self`; returns `self`:
|
2958
|
+
#
|
2959
|
+
# a = [0, 1, 2]
|
2960
|
+
# a.reverse! # => [2, 1, 0]
|
2961
|
+
# a # => [2, 1, 0]
|
2962
|
+
#
|
2963
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
2964
|
+
#
|
2965
|
+
def reverse!: () -> ::Array[Elem]
|
2966
|
+
|
2967
|
+
# <!--
|
2968
|
+
# rdoc-file=array.c
|
2969
|
+
# - reverse_each {|element| ... } -> self
|
2970
|
+
# - reverse_each -> Enumerator
|
2971
|
+
# -->
|
2972
|
+
# When a block given, iterates backwards over the elements of `self`, passing,
|
2973
|
+
# in reverse order, each element to the block; returns `self`:
|
2974
|
+
#
|
2975
|
+
# a = []
|
2976
|
+
# [0, 1, 2].reverse_each {|element| a.push(element) }
|
2977
|
+
# a # => [2, 1, 0]
|
2978
|
+
#
|
2979
|
+
# Allows the array to be modified during iteration:
|
2980
|
+
#
|
2981
|
+
# a = ['a', 'b', 'c']
|
2982
|
+
# a.reverse_each {|element| a.clear if element.start_with?('b') }
|
2983
|
+
# a # => []
|
2984
|
+
#
|
2985
|
+
# When no block given, returns a new Enumerator.
|
2986
|
+
#
|
2987
|
+
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
|
2988
|
+
#
|
2989
|
+
def reverse_each: () { (Elem item) -> void } -> self
|
2990
|
+
| () -> ::Enumerator[Elem, self]
|
2991
|
+
|
2992
|
+
# <!--
|
2993
|
+
# rdoc-file=array.c
|
2994
|
+
# - rindex(object) -> integer or nil
|
2995
|
+
# - rindex {|element| ... } -> integer or nil
|
2996
|
+
# - rindex -> new_enumerator
|
2997
|
+
# -->
|
2998
|
+
# Returns the index of the last element for which `object == element`.
|
2999
|
+
#
|
3000
|
+
# With argument `object` given, returns the index of the last such element
|
3001
|
+
# found:
|
3002
|
+
#
|
3003
|
+
# a = [:foo, 'bar', 2, 'bar']
|
3004
|
+
# a.rindex('bar') # => 3
|
3005
|
+
#
|
3006
|
+
# Returns `nil` if no such object found.
|
3007
|
+
#
|
3008
|
+
# With a block given, calls the block with each successive element; returns the
|
3009
|
+
# index of the last element for which the block returns a truthy value:
|
3010
|
+
#
|
3011
|
+
# a = [:foo, 'bar', 2, 'bar']
|
3012
|
+
# a.rindex {|element| element == 'bar' } # => 3
|
3013
|
+
#
|
3014
|
+
# Returns `nil` if the block never returns a truthy value.
|
3015
|
+
#
|
3016
|
+
# When neither an argument nor a block is given, returns a new Enumerator.
|
3017
|
+
#
|
3018
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
3019
|
+
#
|
3020
|
+
def rindex: (untyped obj) -> ::Integer?
|
3021
|
+
| () { (Elem item) -> boolish } -> ::Integer?
|
3022
|
+
| () -> ::Enumerator[Elem, ::Integer?]
|
3023
|
+
|
3024
|
+
# <!--
|
3025
|
+
# rdoc-file=array.c
|
3026
|
+
# - rotate(count = 1) -> new_array
|
3027
|
+
# -->
|
3028
|
+
# Returns a new array formed from `self` with elements rotated from one end to
|
3029
|
+
# the other.
|
3030
|
+
#
|
3031
|
+
# With non-negative numeric `count`, rotates elements from the beginning to the
|
3032
|
+
# end:
|
3033
|
+
#
|
3034
|
+
# [0, 1, 2, 3].rotate(2) # => [2, 3, 0, 1]
|
3035
|
+
# [0, 1, 2, 3].rotate(2.1) # => [2, 3, 0, 1]
|
3036
|
+
#
|
3037
|
+
# If `count` is large, uses `count % array.size` as the count:
|
3038
|
+
#
|
3039
|
+
# [0, 1, 2, 3].rotate(22) # => [2, 3, 0, 1]
|
3040
|
+
#
|
3041
|
+
# With a `count` of zero, rotates no elements:
|
3042
|
+
#
|
3043
|
+
# [0, 1, 2, 3].rotate(0) # => [0, 1, 2, 3]
|
3044
|
+
#
|
3045
|
+
# With negative numeric `count`, rotates in the opposite direction, from the end
|
3046
|
+
# to the beginning:
|
3047
|
+
#
|
3048
|
+
# [0, 1, 2, 3].rotate(-1) # => [3, 0, 1, 2]
|
3049
|
+
#
|
3050
|
+
# If `count` is small (far from zero), uses `count % array.size` as the count:
|
3051
|
+
#
|
3052
|
+
# [0, 1, 2, 3].rotate(-21) # => [3, 0, 1, 2]
|
3053
|
+
#
|
3054
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3055
|
+
#
|
3056
|
+
def rotate: (?int count) -> ::Array[Elem]
|
3057
|
+
|
3058
|
+
# <!--
|
3059
|
+
# rdoc-file=array.c
|
3060
|
+
# - rotate!(count = 1) -> self
|
3061
|
+
# -->
|
3062
|
+
# Rotates `self` in place by moving elements from one end to the other; returns
|
3063
|
+
# `self`.
|
3064
|
+
#
|
3065
|
+
# With non-negative numeric `count`, rotates `count` elements from the beginning
|
3066
|
+
# to the end:
|
3067
|
+
#
|
3068
|
+
# [0, 1, 2, 3].rotate!(2) # => [2, 3, 0, 1]
|
3069
|
+
# [0, 1, 2, 3].rotate!(2.1) # => [2, 3, 0, 1]
|
3070
|
+
#
|
3071
|
+
# If `count` is large, uses `count % array.size` as the count:
|
3072
|
+
#
|
3073
|
+
# [0, 1, 2, 3].rotate!(21) # => [1, 2, 3, 0]
|
3074
|
+
#
|
3075
|
+
# If `count` is zero, rotates no elements:
|
3076
|
+
#
|
3077
|
+
# [0, 1, 2, 3].rotate!(0) # => [0, 1, 2, 3]
|
3078
|
+
#
|
3079
|
+
# With a negative numeric `count`, rotates in the opposite direction, from end
|
3080
|
+
# to beginning:
|
3081
|
+
#
|
3082
|
+
# [0, 1, 2, 3].rotate!(-1) # => [3, 0, 1, 2]
|
3083
|
+
#
|
3084
|
+
# If `count` is small (far from zero), uses `count % array.size` as the count:
|
3085
|
+
#
|
3086
|
+
# [0, 1, 2, 3].rotate!(-21) # => [3, 0, 1, 2]
|
3087
|
+
#
|
3088
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
3089
|
+
#
|
3090
|
+
def rotate!: (?int count) -> self
|
3091
|
+
|
3092
|
+
# <!--
|
3093
|
+
# rdoc-file=array.rb
|
3094
|
+
# - sample(random: Random) -> object
|
3095
|
+
# - sample(count, random: Random) -> new_ary
|
3096
|
+
# -->
|
3097
|
+
# Returns random elements from `self`, as selected by the object given by the
|
3098
|
+
# keyword argument `random`.
|
3099
|
+
#
|
3100
|
+
# With no argument `count` given, returns one random element from `self`:
|
3101
|
+
#
|
3102
|
+
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
3103
|
+
# a.sample # => 3
|
3104
|
+
# a.sample # => 8
|
3105
|
+
#
|
3106
|
+
# Returns `nil` if `self` is empty:
|
3107
|
+
#
|
3108
|
+
# [].sample # => nil
|
3109
|
+
#
|
3110
|
+
# With a non-negative numeric argument `count` given, returns a new array
|
3111
|
+
# containing `count` random elements from `self`:
|
3112
|
+
#
|
3113
|
+
# a.sample(3) # => [8, 9, 2]
|
3114
|
+
# a.sample(6) # => [9, 6, 0, 3, 1, 4]
|
3115
|
+
#
|
3116
|
+
# The order of the result array is unrelated to the order of `self`.
|
3117
|
+
#
|
3118
|
+
# Returns a new empty `Array` if `self` is empty:
|
3119
|
+
#
|
3120
|
+
# [].sample(4) # => []
|
3121
|
+
#
|
3122
|
+
# May return duplicates in `self`:
|
3123
|
+
#
|
3124
|
+
# a = [1, 1, 1, 2, 2, 3]
|
3125
|
+
# a.sample(a.size) # => [1, 1, 3, 2, 1, 2]
|
3126
|
+
#
|
3127
|
+
# Returns no more than `a.size` elements (because no new duplicates are
|
3128
|
+
# introduced):
|
3129
|
+
#
|
3130
|
+
# a.sample(50) # => [6, 4, 1, 8, 5, 9, 0, 2, 3, 7]
|
3131
|
+
#
|
3132
|
+
# The object given with the keyword argument `random` is used as the random
|
3133
|
+
# number generator:
|
3134
|
+
#
|
3135
|
+
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
3136
|
+
# a.sample(random: Random.new(1)) # => 6
|
3137
|
+
# a.sample(4, random: Random.new(1)) # => [6, 10, 9, 2]
|
3138
|
+
#
|
3139
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3140
|
+
#
|
3141
|
+
def sample: %a{implicitly-returns-nil} (?random: _Rand rng) -> Elem
|
3142
|
+
| (int n, ?random: _Rand rng) -> ::Array[Elem]
|
3143
|
+
|
3144
|
+
# <!--
|
3145
|
+
# rdoc-file=array.c
|
3146
|
+
# - select {|element| ... } -> new_array
|
3147
|
+
# - select -> new_enumerator
|
3148
|
+
# - filter {|element| ... } -> new_array
|
3149
|
+
# - filter -> new_enumerator
|
3150
|
+
# -->
|
3151
|
+
# With a block given, calls the block with each element of `self`; returns a new
|
3152
|
+
# array containing those elements of `self` for which the block returns a truthy
|
3153
|
+
# value:
|
3154
|
+
#
|
3155
|
+
# a = [:foo, 'bar', 2, :bam]
|
3156
|
+
# a.select {|element| element.to_s.start_with?('b') }
|
3157
|
+
# # => ["bar", :bam]
|
3158
|
+
#
|
3159
|
+
# With no block given, returns a new Enumerator.
|
3160
|
+
#
|
3161
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3162
|
+
#
|
3163
|
+
def select: () { (Elem item) -> boolish } -> ::Array[Elem]
|
3164
|
+
| () -> ::Enumerator[Elem, ::Array[Elem]]
|
3165
|
+
|
3166
|
+
# <!--
|
3167
|
+
# rdoc-file=array.c
|
3168
|
+
# - select! {|element| ... } -> self or nil
|
3169
|
+
# - select! -> new_enumerator
|
3170
|
+
# - filter! {|element| ... } -> self or nil
|
3171
|
+
# - filter! -> new_enumerator
|
3172
|
+
# -->
|
3173
|
+
# With a block given, calls the block with each element of `self`; removes from
|
3174
|
+
# `self` those elements for which the block returns `false` or `nil`.
|
3175
|
+
#
|
3176
|
+
# Returns `self` if any elements were removed:
|
3177
|
+
#
|
3178
|
+
# a = [:foo, 'bar', 2, :bam]
|
3179
|
+
# a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
|
3180
|
+
#
|
3181
|
+
# Returns `nil` if no elements were removed.
|
3182
|
+
#
|
3183
|
+
# With no block given, returns a new Enumerator.
|
3184
|
+
#
|
3185
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
3186
|
+
#
|
3187
|
+
def select!: () { (Elem item) -> boolish } -> self?
|
3188
|
+
| () -> ::Enumerator[Elem, self?]
|
3189
|
+
|
3190
|
+
# <!--
|
3191
|
+
# rdoc-file=array.c
|
3192
|
+
# - shift -> object or nil
|
3193
|
+
# - shift(count) -> new_array or nil
|
3194
|
+
# -->
|
3195
|
+
# Removes and returns leading elements from `self`.
|
3196
|
+
#
|
3197
|
+
# With no argument, removes and returns one element, if available, or `nil`
|
3198
|
+
# otherwise:
|
3199
|
+
#
|
3200
|
+
# a = [0, 1, 2, 3]
|
3201
|
+
# a.shift # => 0
|
3202
|
+
# a # => [1, 2, 3]
|
3203
|
+
# [].shift # => nil
|
3204
|
+
#
|
3205
|
+
# With non-negative numeric argument `count` given, removes and returns the
|
3206
|
+
# first `count` elements:
|
3207
|
+
#
|
3208
|
+
# a = [0, 1, 2, 3]
|
3209
|
+
# a.shift(2) # => [0, 1]
|
3210
|
+
# a # => [2, 3]
|
3211
|
+
# a.shift(1.1) # => [2]
|
3212
|
+
# a # => [3]
|
3213
|
+
# a.shift(0) # => []
|
3214
|
+
# a # => [3]
|
3215
|
+
#
|
3216
|
+
# If `count` is large, removes and returns all elements:
|
3217
|
+
#
|
3218
|
+
# a = [0, 1, 2, 3]
|
3219
|
+
# a.shift(50) # => [0, 1, 2, 3]
|
3220
|
+
# a # => []
|
3221
|
+
#
|
3222
|
+
# If `self` is empty, returns a new empty array.
|
3223
|
+
#
|
3224
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
3225
|
+
#
|
3226
|
+
def shift: %a{implicitly-returns-nil} () -> Elem
|
3227
|
+
| (int n) -> ::Array[Elem]
|
3228
|
+
|
3229
|
+
# <!--
|
3230
|
+
# rdoc-file=array.rb
|
3231
|
+
# - shuffle(random: Random) -> new_array
|
3232
|
+
# -->
|
3233
|
+
# Returns a new array containing all elements from `self` in a random order, as
|
3234
|
+
# selected by the object given by the keyword argument `random`:
|
3235
|
+
#
|
3236
|
+
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
3237
|
+
# a.shuffle # => [0, 8, 1, 9, 6, 3, 4, 7, 2, 5]
|
3238
|
+
# a.shuffle # => [8, 9, 0, 5, 1, 2, 6, 4, 7, 3]
|
3239
|
+
#
|
3240
|
+
# Duplicate elements are included:
|
3241
|
+
#
|
3242
|
+
# a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
|
3243
|
+
# a.shuffle # => [1, 0, 1, 1, 0, 0, 1, 0, 0, 1]
|
3244
|
+
# a.shuffle # => [1, 1, 0, 0, 0, 1, 1, 0, 0, 1]
|
3245
|
+
#
|
3246
|
+
# The object given with the keyword argument `random` is used as the random
|
3247
|
+
# number generator.
|
3248
|
+
#
|
3249
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3250
|
+
#
|
3251
|
+
def shuffle: (?random: _Rand rng) -> ::Array[Elem]
|
3252
|
+
|
3253
|
+
# <!--
|
3254
|
+
# rdoc-file=array.rb
|
3255
|
+
# - shuffle!(random: Random) -> self
|
3256
|
+
# -->
|
3257
|
+
# Shuffles all elements in `self` into a random order, as selected by the object
|
3258
|
+
# given by the keyword argument `random`. Returns `self`:
|
3259
|
+
#
|
3260
|
+
# a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
3261
|
+
# a.shuffle! # => [5, 3, 8, 7, 6, 1, 9, 4, 2, 0]
|
3262
|
+
# a.shuffle! # => [9, 4, 0, 6, 2, 8, 1, 5, 3, 7]
|
3263
|
+
#
|
3264
|
+
# Duplicate elements are included:
|
3265
|
+
#
|
3266
|
+
# a = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
|
3267
|
+
# a.shuffle! # => [1, 0, 0, 1, 1, 0, 1, 0, 0, 1]
|
3268
|
+
# a.shuffle! # => [0, 1, 0, 1, 1, 0, 1, 0, 1, 0]
|
3269
|
+
#
|
3270
|
+
# The object given with the keyword argument `random` is used as the random
|
3271
|
+
# number generator.
|
3272
|
+
#
|
3273
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
3274
|
+
#
|
3275
|
+
def shuffle!: (?random: _Rand rng) -> self
|
3276
|
+
|
3277
|
+
# <!-- rdoc-file=array.c -->
|
3278
|
+
# Returns the count of elements in `self`:
|
3279
|
+
#
|
3280
|
+
# [0, 1, 2].length # => 3
|
3281
|
+
# [].length # => 0
|
3282
|
+
#
|
3283
|
+
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
|
3284
|
+
#
|
3285
|
+
alias size length
|
3286
|
+
|
3287
|
+
# <!-- rdoc-file=array.c -->
|
3288
|
+
# Returns elements from `self`; does not modify `self`.
|
3289
|
+
#
|
3290
|
+
# In brief:
|
3291
|
+
#
|
3292
|
+
# a = [:foo, 'bar', 2]
|
3293
|
+
#
|
3294
|
+
# # Single argument index: returns one element.
|
3295
|
+
# a[0] # => :foo # Zero-based index.
|
3296
|
+
# a[-1] # => 2 # Negative index counts backwards from end.
|
3297
|
+
#
|
3298
|
+
# # Arguments start and length: returns an array.
|
3299
|
+
# a[1, 2] # => ["bar", 2]
|
3300
|
+
# a[-2, 2] # => ["bar", 2] # Negative start counts backwards from end.
|
3301
|
+
#
|
3302
|
+
# # Single argument range: returns an array.
|
3303
|
+
# a[0..1] # => [:foo, "bar"]
|
3304
|
+
# a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end.
|
3305
|
+
# a[-2..2] # => ["bar", 2] # Negative range-end counts backwards from end.
|
3306
|
+
#
|
3307
|
+
# When a single integer argument `index` is given, returns the element at offset
|
3308
|
+
# `index`:
|
3309
|
+
#
|
3310
|
+
# a = [:foo, 'bar', 2]
|
3311
|
+
# a[0] # => :foo
|
3312
|
+
# a[2] # => 2
|
3313
|
+
# a # => [:foo, "bar", 2]
|
3314
|
+
#
|
3315
|
+
# If `index` is negative, counts backwards from the end of `self`:
|
3316
|
+
#
|
3317
|
+
# a = [:foo, 'bar', 2]
|
3318
|
+
# a[-1] # => 2
|
3319
|
+
# a[-2] # => "bar"
|
3320
|
+
#
|
3321
|
+
# If `index` is out of range, returns `nil`.
|
3322
|
+
#
|
3323
|
+
# When two Integer arguments `start` and `length` are given, returns a new
|
3324
|
+
# `Array` of size `length` containing successive elements beginning at offset
|
3325
|
+
# `start`:
|
3326
|
+
#
|
3327
|
+
# a = [:foo, 'bar', 2]
|
3328
|
+
# a[0, 2] # => [:foo, "bar"]
|
3329
|
+
# a[1, 2] # => ["bar", 2]
|
3330
|
+
#
|
3331
|
+
# If `start + length` is greater than `self.length`, returns all elements from
|
3332
|
+
# offset `start` to the end:
|
3333
|
+
#
|
3334
|
+
# a = [:foo, 'bar', 2]
|
3335
|
+
# a[0, 4] # => [:foo, "bar", 2]
|
3336
|
+
# a[1, 3] # => ["bar", 2]
|
3337
|
+
# a[2, 2] # => [2]
|
3338
|
+
#
|
3339
|
+
# If `start == self.size` and `length >= 0`, returns a new empty `Array`.
|
3340
|
+
#
|
3341
|
+
# If `length` is negative, returns `nil`.
|
3342
|
+
#
|
3343
|
+
# When a single Range argument `range` is given, treats `range.min` as `start`
|
3344
|
+
# above and `range.size` as `length` above:
|
3345
|
+
#
|
3346
|
+
# a = [:foo, 'bar', 2]
|
3347
|
+
# a[0..1] # => [:foo, "bar"]
|
3348
|
+
# a[1..2] # => ["bar", 2]
|
3349
|
+
#
|
3350
|
+
# Special case: If `range.start == a.size`, returns a new empty `Array`.
|
3351
|
+
#
|
3352
|
+
# If `range.end` is negative, calculates the end index from the end:
|
3353
|
+
#
|
3354
|
+
# a = [:foo, 'bar', 2]
|
3355
|
+
# a[0..-1] # => [:foo, "bar", 2]
|
3356
|
+
# a[0..-2] # => [:foo, "bar"]
|
3357
|
+
# a[0..-3] # => [:foo]
|
3358
|
+
#
|
3359
|
+
# If `range.start` is negative, calculates the start index from the end:
|
3360
|
+
#
|
3361
|
+
# a = [:foo, 'bar', 2]
|
3362
|
+
# a[-1..2] # => [2]
|
3363
|
+
# a[-2..2] # => ["bar", 2]
|
3364
|
+
# a[-3..2] # => [:foo, "bar", 2]
|
3365
|
+
#
|
3366
|
+
# If `range.start` is larger than the array size, returns `nil`.
|
3367
|
+
#
|
3368
|
+
# a = [:foo, 'bar', 2]
|
3369
|
+
# a[4..1] # => nil
|
3370
|
+
# a[4..0] # => nil
|
3371
|
+
# a[4..-1] # => nil
|
3372
|
+
#
|
3373
|
+
# When a single Enumerator::ArithmeticSequence argument `aseq` is given, returns
|
3374
|
+
# an `Array` of elements corresponding to the indexes produced by the sequence.
|
3375
|
+
#
|
3376
|
+
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
|
3377
|
+
# a[(1..).step(2)] # => ["data1", "data2", "data3"]
|
3378
|
+
#
|
3379
|
+
# Unlike slicing with range, if the start or the end of the arithmetic sequence
|
3380
|
+
# is larger than array size, throws RangeError.
|
3381
|
+
#
|
3382
|
+
# a = ['--', 'data1', '--', 'data2', '--', 'data3']
|
3383
|
+
# a[(1..11).step(2)]
|
3384
|
+
# # RangeError (((1..11).step(2)) out of range)
|
3385
|
+
# a[(7..).step(2)]
|
3386
|
+
# # RangeError (((7..).step(2)) out of range)
|
3387
|
+
#
|
3388
|
+
# If given a single argument, and its type is not one of the listed, tries to
|
3389
|
+
# convert it to Integer, and raises if it is impossible:
|
3390
|
+
#
|
3391
|
+
# a = [:foo, 'bar', 2]
|
3392
|
+
# # Raises TypeError (no implicit conversion of Symbol into Integer):
|
3393
|
+
# a[:foo]
|
3394
|
+
#
|
3395
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3396
|
+
#
|
3397
|
+
def slice: %a{implicitly-returns-nil} (int index) -> Elem
|
3398
|
+
| (int start, int length) -> ::Array[Elem]?
|
3399
|
+
| (::Range[::Integer] range) -> ::Array[Elem]?
|
3400
|
+
|
3401
|
+
# <!--
|
3402
|
+
# rdoc-file=array.c
|
3403
|
+
# - slice!(index) -> object or nil
|
3404
|
+
# - slice!(start, length) -> new_array or nil
|
3405
|
+
# - slice!(range) -> new_array or nil
|
3406
|
+
# -->
|
3407
|
+
# Removes and returns elements from `self`.
|
3408
|
+
#
|
3409
|
+
# With numeric argument `index` given, removes and returns the element at offset
|
3410
|
+
# `index`:
|
3411
|
+
#
|
3412
|
+
# a = ['a', 'b', 'c', 'd']
|
3413
|
+
# a.slice!(2) # => "c"
|
3414
|
+
# a # => ["a", "b", "d"]
|
3415
|
+
# a.slice!(2.1) # => "d"
|
3416
|
+
# a # => ["a", "b"]
|
3417
|
+
#
|
3418
|
+
# If `index` is negative, counts backwards from the end of `self`:
|
3419
|
+
#
|
3420
|
+
# a = ['a', 'b', 'c', 'd']
|
3421
|
+
# a.slice!(-2) # => "c"
|
3422
|
+
# a # => ["a", "b", "d"]
|
3423
|
+
#
|
3424
|
+
# If `index` is out of range, returns `nil`.
|
3425
|
+
#
|
3426
|
+
# With numeric arguments `start` and `length` given, removes `length` elements
|
3427
|
+
# from `self` beginning at zero-based offset `start`; returns the removed
|
3428
|
+
# objects in a new array:
|
3429
|
+
#
|
3430
|
+
# a = ['a', 'b', 'c', 'd']
|
3431
|
+
# a.slice!(1, 2) # => ["b", "c"]
|
3432
|
+
# a # => ["a", "d"]
|
3433
|
+
# a.slice!(0.1, 1.1) # => ["a"]
|
3434
|
+
# a # => ["d"]
|
3435
|
+
#
|
3436
|
+
# If `start` is negative, counts backwards from the end of `self`:
|
3437
|
+
#
|
3438
|
+
# a = ['a', 'b', 'c', 'd']
|
3439
|
+
# a.slice!(-2, 1) # => ["c"]
|
3440
|
+
# a # => ["a", "b", "d"]
|
3441
|
+
#
|
3442
|
+
# If `start` is out-of-range, returns `nil`:
|
3443
|
+
#
|
3444
|
+
# a = ['a', 'b', 'c', 'd']
|
3445
|
+
# a.slice!(5, 1) # => nil
|
3446
|
+
# a.slice!(-5, 1) # => nil
|
3447
|
+
#
|
3448
|
+
# If `start + length` exceeds the array size, removes and returns all elements
|
3449
|
+
# from offset `start` to the end:
|
3450
|
+
#
|
3451
|
+
# a = ['a', 'b', 'c', 'd']
|
3452
|
+
# a.slice!(2, 50) # => ["c", "d"]
|
3453
|
+
# a # => ["a", "b"]
|
3454
|
+
#
|
3455
|
+
# If `start == a.size` and `length` is non-negative, returns a new empty array.
|
3456
|
+
#
|
3457
|
+
# If `length` is negative, returns `nil`.
|
3458
|
+
#
|
3459
|
+
# With Range argument `range` given, treats `range.min` as `start` (as above)
|
3460
|
+
# and `range.size` as `length` (as above):
|
3461
|
+
#
|
3462
|
+
# a = ['a', 'b', 'c', 'd']
|
3463
|
+
# a.slice!(1..2) # => ["b", "c"]
|
3464
|
+
# a # => ["a", "d"]
|
3465
|
+
#
|
3466
|
+
# If `range.start == a.size`, returns a new empty array:
|
3467
|
+
#
|
3468
|
+
# a = ['a', 'b', 'c', 'd']
|
3469
|
+
# a.slice!(4..5) # => []
|
3470
|
+
#
|
3471
|
+
# If `range.start` is larger than the array size, returns `nil`:
|
3472
|
+
#
|
3473
|
+
# a = ['a', 'b', 'c', 'd']
|
3474
|
+
# a.slice!(5..6) # => nil
|
3475
|
+
#
|
3476
|
+
# If `range.start` is negative, calculates the start index by counting backwards
|
3477
|
+
# from the end of `self`:
|
3478
|
+
#
|
3479
|
+
# a = ['a', 'b', 'c', 'd']
|
3480
|
+
# a.slice!(-2..2) # => ["c"]
|
3481
|
+
#
|
3482
|
+
# If `range.end` is negative, calculates the end index by counting backwards
|
3483
|
+
# from the end of `self`:
|
3484
|
+
#
|
3485
|
+
# a = ['a', 'b', 'c', 'd']
|
3486
|
+
# a.slice!(0..-2) # => ["a", "b", "c"]
|
3487
|
+
#
|
3488
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
3489
|
+
#
|
3490
|
+
def slice!: %a{implicitly-returns-nil} (int index) -> Elem
|
3491
|
+
| (int start, int length) -> ::Array[Elem]?
|
3492
|
+
| (::Range[::Integer] range) -> ::Array[Elem]?
|
3493
|
+
|
3494
|
+
# <!--
|
3495
|
+
# rdoc-file=array.c
|
3496
|
+
# - sort -> new_array
|
3497
|
+
# - sort {|a, b| ... } -> new_array
|
3498
|
+
# -->
|
3499
|
+
# Returns a new array containing the elements of `self`, sorted.
|
3500
|
+
#
|
3501
|
+
# With no block given, compares elements using operator `#<=>` (see Object#<=>):
|
3502
|
+
#
|
3503
|
+
# [0, 2, 3, 1].sort # => [0, 1, 2, 3]
|
3504
|
+
#
|
3505
|
+
# With a block given, calls the block with each combination of pairs of elements
|
3506
|
+
# from `self`; for each pair `a` and `b`, the block should return a numeric:
|
3507
|
+
#
|
3508
|
+
# * Negative when `b` is to follow `a`.
|
3509
|
+
# * Zero when `a` and `b` are equivalent.
|
3510
|
+
# * Positive when `a` is to follow `b`.
|
3511
|
+
#
|
3512
|
+
# Example:
|
3513
|
+
#
|
3514
|
+
# a = [3, 2, 0, 1]
|
3515
|
+
# a.sort {|a, b| a <=> b } # => [0, 1, 2, 3]
|
3516
|
+
# a.sort {|a, b| b <=> a } # => [3, 2, 1, 0]
|
3517
|
+
#
|
3518
|
+
# When the block returns zero, the order for `a` and `b` is indeterminate, and
|
3519
|
+
# may be unstable.
|
3520
|
+
#
|
3521
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3522
|
+
#
|
3523
|
+
def sort: () -> ::Array[Elem]
|
3524
|
+
| () { (Elem a, Elem b) -> ::Integer } -> ::Array[Elem]
|
3525
|
+
|
3526
|
+
# <!--
|
3527
|
+
# rdoc-file=array.c
|
3528
|
+
# - sort! -> self
|
3529
|
+
# - sort! {|a, b| ... } -> self
|
3530
|
+
# -->
|
3531
|
+
# Like Array#sort, but returns `self` with its elements sorted in place.
|
3532
|
+
#
|
3533
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
3534
|
+
#
|
3535
|
+
def sort!: () -> self
|
3536
|
+
| () { (Elem a, Elem b) -> ::Integer } -> self
|
3537
|
+
|
3538
|
+
# <!--
|
3539
|
+
# rdoc-file=array.c
|
3540
|
+
# - sort_by! {|element| ... } -> self
|
3541
|
+
# - sort_by! -> new_enumerator
|
3542
|
+
# -->
|
3543
|
+
# With a block given, sorts the elements of `self` in place; returns self.
|
3544
|
+
#
|
3545
|
+
# Calls the block with each successive element; sorts elements based on the
|
3546
|
+
# values returned from the block:
|
3547
|
+
#
|
3548
|
+
# a = ['aaaa', 'bbb', 'cc', 'd']
|
3549
|
+
# a.sort_by! {|element| element.size }
|
3550
|
+
# a # => ["d", "cc", "bbb", "aaaa"]
|
3551
|
+
#
|
3552
|
+
# For duplicate values returned by the block, the ordering is indeterminate, and
|
3553
|
+
# may be unstable.
|
3554
|
+
#
|
3555
|
+
# With no block given, returns a new Enumerator.
|
3556
|
+
#
|
3557
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
3558
|
+
#
|
3559
|
+
def sort_by!: [U] () { (Elem obj) -> U } -> ::Array[Elem]
|
3560
|
+
| () -> ::Enumerator[Elem, ::Array[Elem]]
|
3561
|
+
|
3562
|
+
# <!--
|
3563
|
+
# rdoc-file=array.c
|
3564
|
+
# - sum(init = 0) -> object
|
3565
|
+
# - sum(init = 0) {|element| ... } -> object
|
3566
|
+
# -->
|
3567
|
+
# With no block given, returns the sum of `init` and all elements of `self`; for
|
3568
|
+
# array `array` and value `init`, equivalent to:
|
3569
|
+
#
|
3570
|
+
# sum = init
|
3571
|
+
# array.each {|element| sum += element }
|
3572
|
+
# sum
|
3573
|
+
#
|
3574
|
+
# For example, `[e0, e1, e2].sum` returns `init + e0 + e1 + e2`.
|
3575
|
+
#
|
3576
|
+
# Examples:
|
3577
|
+
#
|
3578
|
+
# [0, 1, 2, 3].sum # => 6
|
3579
|
+
# [0, 1, 2, 3].sum(100) # => 106
|
3580
|
+
# ['abc', 'def', 'ghi'].sum('jkl') # => "jklabcdefghi"
|
3581
|
+
# [[:foo, :bar], ['foo', 'bar']].sum([2, 3])
|
3582
|
+
# # => [2, 3, :foo, :bar, "foo", "bar"]
|
3583
|
+
#
|
3584
|
+
# The `init` value and elements need not be numeric, but must all be
|
3585
|
+
# `+`-compatible:
|
3586
|
+
#
|
3587
|
+
# # Raises TypeError: Array can't be coerced into Integer.
|
3588
|
+
# [[:foo, :bar], ['foo', 'bar']].sum(2)
|
3589
|
+
#
|
3590
|
+
# With a block given, calls the block with each element of `self`; the block's
|
3591
|
+
# return value (instead of the element itself) is used as the addend:
|
3592
|
+
#
|
3593
|
+
# ['zero', 1, :two].sum('Coerced and concatenated: ') {|element| element.to_s }
|
3594
|
+
# # => "Coerced and concatenated: zero1two"
|
3595
|
+
#
|
3596
|
+
# Notes:
|
3597
|
+
#
|
3598
|
+
# * Array#join and Array#flatten may be faster than Array#sum for an array of
|
3599
|
+
# strings or an array of arrays.
|
3600
|
+
# * Array#sum method may not respect method redefinition of "+" methods such
|
3601
|
+
# as Integer#+.
|
3602
|
+
#
|
3603
|
+
def sum: (?untyped init) -> untyped
|
3604
|
+
| (?untyped init) { (Elem e) -> untyped } -> untyped
|
3605
|
+
|
3606
|
+
# <!--
|
3607
|
+
# rdoc-file=array.c
|
3608
|
+
# - take(count) -> new_array
|
3609
|
+
# -->
|
3610
|
+
# Returns a new array containing the first `count` element of `self` (as
|
3611
|
+
# available); `count` must be a non-negative numeric; does not modify `self`:
|
3612
|
+
#
|
3613
|
+
# a = ['a', 'b', 'c', 'd']
|
3614
|
+
# a.take(2) # => ["a", "b"]
|
3615
|
+
# a.take(2.1) # => ["a", "b"]
|
3616
|
+
# a.take(50) # => ["a", "b", "c", "d"]
|
3617
|
+
# a.take(0) # => []
|
3618
|
+
#
|
3619
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3620
|
+
#
|
3621
|
+
def take: (int n) -> ::Array[Elem]
|
3622
|
+
|
3623
|
+
# <!--
|
3624
|
+
# rdoc-file=array.c
|
3625
|
+
# - take_while {|element| ... } -> new_array
|
3626
|
+
# - take_while -> new_enumerator
|
3627
|
+
# -->
|
3628
|
+
# With a block given, calls the block with each successive element of `self`;
|
3629
|
+
# stops iterating if the block returns `false` or `nil`; returns a new array
|
3630
|
+
# containing those elements for which the block returned a truthy value:
|
3631
|
+
#
|
3632
|
+
# a = [0, 1, 2, 3, 4, 5]
|
3633
|
+
# a.take_while {|element| element < 3 } # => [0, 1, 2]
|
3634
|
+
# a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5]
|
3635
|
+
# a.take_while {|element| false } # => []
|
3636
|
+
#
|
3637
|
+
# With no block given, returns a new Enumerator.
|
3638
|
+
#
|
3639
|
+
# Does not modify `self`.
|
3640
|
+
#
|
3641
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3642
|
+
#
|
3643
|
+
def take_while: () { (Elem obj) -> boolish } -> ::Array[Elem]
|
3644
|
+
| () -> ::Enumerator[Elem, ::Array[Elem]]
|
3645
|
+
|
3646
|
+
# <!--
|
3647
|
+
# rdoc-file=array.c
|
3648
|
+
# - to_a -> self or new_array
|
3649
|
+
# -->
|
3650
|
+
# When `self` is an instance of `Array`, returns `self`.
|
3651
|
+
#
|
3652
|
+
# Otherwise, returns a new array containing the elements of `self`:
|
3653
|
+
#
|
3654
|
+
# class MyArray < Array; end
|
3655
|
+
# my_a = MyArray.new(['foo', 'bar', 'two'])
|
3656
|
+
# a = my_a.to_a
|
3657
|
+
# a # => ["foo", "bar", "two"]
|
3658
|
+
# a.class # => Array # Not MyArray.
|
3659
|
+
#
|
3660
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
3661
|
+
#
|
3662
|
+
def to_a: () -> ::Array[Elem]
|
3663
|
+
|
3664
|
+
# <!--
|
3665
|
+
# rdoc-file=array.c
|
3666
|
+
# - array.to_ary -> self
|
3667
|
+
# -->
|
3668
|
+
# Returns `self`.
|
3669
|
+
#
|
3670
|
+
def to_ary: () -> self
|
3671
|
+
|
3672
|
+
# <!--
|
3673
|
+
# rdoc-file=array.c
|
3674
|
+
# - to_h -> new_hash
|
3675
|
+
# - to_h {|element| ... } -> new_hash
|
3676
|
+
# -->
|
3677
|
+
# Returns a new hash formed from `self`.
|
3678
|
+
#
|
3679
|
+
# With no block given, each element of `self` must be a 2-element sub-array;
|
3680
|
+
# forms each sub-array into a key-value pair in the new hash:
|
3681
|
+
#
|
3682
|
+
# a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']]
|
3683
|
+
# a.to_h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
|
3684
|
+
# [].to_h # => {}
|
3685
|
+
#
|
3686
|
+
# With a block given, the block must return a 2-element array; calls the block
|
3687
|
+
# with each element of `self`; forms each returned array into a key-value pair
|
3688
|
+
# in the returned hash:
|
3689
|
+
#
|
3690
|
+
# a = ['foo', :bar, 1, [2, 3], {baz: 4}]
|
3691
|
+
# a.to_h {|element| [element, element.class] }
|
3692
|
+
# # => {"foo"=>String, :bar=>Symbol, 1=>Integer, [2, 3]=>Array, {:baz=>4}=>Hash}
|
3693
|
+
#
|
3694
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
3695
|
+
#
|
3696
|
+
def to_h: () -> Hash[untyped, untyped]
|
3697
|
+
| [T, S] () { (Elem) -> [ T, S ] } -> Hash[T, S]
|
3698
|
+
|
3699
|
+
# <!-- rdoc-file=array.c -->
|
3700
|
+
# Returns the new string formed by calling method `#inspect` on each array
|
3701
|
+
# element:
|
3702
|
+
#
|
3703
|
+
# a = [:foo, 'bar', 2]
|
3704
|
+
# a.inspect # => "[:foo, \"bar\", 2]"
|
3705
|
+
#
|
3706
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
3707
|
+
#
|
3708
|
+
alias to_s inspect
|
3709
|
+
|
3710
|
+
# <!--
|
3711
|
+
# rdoc-file=array.c
|
3712
|
+
# - transpose -> new_array
|
3713
|
+
# -->
|
3714
|
+
# Returns a new array that is `self` as a [transposed
|
3715
|
+
# matrix](https://en.wikipedia.org/wiki/Transpose):
|
3716
|
+
#
|
3717
|
+
# a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]]
|
3718
|
+
# a.transpose # => [[:a0, :b0, :c0], [:a1, :b1, :c1]]
|
3719
|
+
#
|
3720
|
+
# The elements of `self` must all be the same size.
|
3721
|
+
#
|
3722
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
3723
|
+
#
|
3724
|
+
def transpose: () -> ::Array[::Array[untyped]]
|
3725
|
+
|
3726
|
+
# <!--
|
3727
|
+
# rdoc-file=array.c
|
3728
|
+
# - union(*other_arrays) -> new_array
|
3729
|
+
# -->
|
3730
|
+
# Returns a new array that is the union of the elements of `self` and all given
|
3731
|
+
# arrays `other_arrays`; items are compared using `eql?`:
|
3732
|
+
#
|
3733
|
+
# [0, 1, 2, 3].union([4, 5], [6, 7]) # => [0, 1, 2, 3, 4, 5, 6, 7]
|
3734
|
+
#
|
3735
|
+
# Removes duplicates (preserving the first found):
|
3736
|
+
#
|
3737
|
+
# [0, 1, 1].union([2, 1], [3, 1]) # => [0, 1, 2, 3]
|
3738
|
+
#
|
3739
|
+
# Preserves order (preserving the position of the first found):
|
3740
|
+
#
|
3741
|
+
# [3, 2, 1, 0].union([5, 3], [4, 2]) # => [3, 2, 1, 0, 5, 4]
|
3742
|
+
#
|
3743
|
+
# With no arguments given, returns a copy of `self`.
|
3744
|
+
#
|
3745
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
3746
|
+
#
|
3747
|
+
def union: [T] (*::Array[T] other_arys) -> ::Array[T | Elem]
|
3748
|
+
|
3749
|
+
# <!--
|
3750
|
+
# rdoc-file=array.c
|
3751
|
+
# - uniq -> new_array
|
3752
|
+
# - uniq {|element| ... } -> new_array
|
3753
|
+
# -->
|
3754
|
+
# Returns a new array containing those elements from `self` that are not
|
3755
|
+
# duplicates, the first occurrence always being retained.
|
3756
|
+
#
|
3757
|
+
# With no block given, identifies and omits duplicate elements using method
|
3758
|
+
# `eql?` to compare elements:
|
3759
|
+
#
|
3760
|
+
# a = [0, 0, 1, 1, 2, 2]
|
3761
|
+
# a.uniq # => [0, 1, 2]
|
3762
|
+
#
|
3763
|
+
# With a block given, calls the block for each element; identifies and omits
|
3764
|
+
# "duplicate" elements using method `eql?` to compare *block return values*;
|
3765
|
+
# that is, an element is a duplicate if its block return value is the same as
|
3766
|
+
# that of a previous element:
|
3767
|
+
#
|
3768
|
+
# a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
|
3769
|
+
# a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
|
3770
|
+
#
|
3771
|
+
# Related: [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3772
|
+
#
|
3773
|
+
def uniq: () -> ::Array[Elem]
|
3774
|
+
| () { (Elem item) -> untyped } -> ::Array[Elem]
|
3775
|
+
|
3776
|
+
# <!--
|
3777
|
+
# rdoc-file=array.c
|
3778
|
+
# - uniq! -> self or nil
|
3779
|
+
# - uniq! {|element| ... } -> self or nil
|
3780
|
+
# -->
|
3781
|
+
# Removes duplicate elements from `self`, the first occurrence always being
|
3782
|
+
# retained; returns `self` if any elements removed, `nil` otherwise.
|
3783
|
+
#
|
3784
|
+
# With no block given, identifies and removes elements using method `eql?` to
|
3785
|
+
# compare elements:
|
3786
|
+
#
|
3787
|
+
# a = [0, 0, 1, 1, 2, 2]
|
3788
|
+
# a.uniq! # => [0, 1, 2]
|
3789
|
+
# a.uniq! # => nil
|
3790
|
+
#
|
3791
|
+
# With a block given, calls the block for each element; identifies and omits
|
3792
|
+
# "duplicate" elements using method `eql?` to compare *block return values*;
|
3793
|
+
# that is, an element is a duplicate if its block return value is the same as
|
3794
|
+
# that of a previous element:
|
3795
|
+
#
|
3796
|
+
# a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb']
|
3797
|
+
# a.uniq! {|element| element.size } # => ["a", "aa", "aaa"]
|
3798
|
+
# a.uniq! {|element| element.size } # => nil
|
3799
|
+
#
|
3800
|
+
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
|
3801
|
+
#
|
3802
|
+
def uniq!: () -> self?
|
3803
|
+
| () { (Elem) -> untyped } -> self?
|
3804
|
+
|
3805
|
+
# <!--
|
3806
|
+
# rdoc-file=array.c
|
3807
|
+
# - unshift(*objects) -> self
|
3808
|
+
# - prepend(*objects) -> self
|
3809
|
+
# -->
|
3810
|
+
# Prepends the given `objects` to `self`:
|
3811
|
+
#
|
3812
|
+
# a = [:foo, 'bar', 2]
|
3813
|
+
# a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
|
3814
|
+
#
|
3815
|
+
# Related: Array#shift; see also [Methods for
|
3816
|
+
# Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
3817
|
+
#
|
3818
|
+
def unshift: (*Elem obj) -> self
|
3819
|
+
|
3820
|
+
# <!--
|
3821
|
+
# rdoc-file=array.c
|
3822
|
+
# - values_at(*specifiers) -> new_array
|
3823
|
+
# -->
|
3824
|
+
# Returns elements from `self` in a new array; does not modify `self`.
|
3825
|
+
#
|
3826
|
+
# The objects included in the returned array are the elements of `self` selected
|
3827
|
+
# by the given `specifiers`, each of which must be a numeric index or a Range.
|
3828
|
+
#
|
3829
|
+
# In brief:
|
3830
|
+
#
|
3831
|
+
# a = ['a', 'b', 'c', 'd']
|
3832
|
+
#
|
3833
|
+
# # Index specifiers.
|
3834
|
+
# a.values_at(2, 0, 2, 0) # => ["c", "a", "c", "a"] # May repeat.
|
3835
|
+
# a.values_at(-4, -3, -2, -1) # => ["a", "b", "c", "d"] # Counts backwards if negative.
|
3836
|
+
# a.values_at(-50, 50) # => [nil, nil] # Outside of self.
|
3837
|
+
#
|
3838
|
+
# # Range specifiers.
|
3839
|
+
# a.values_at(1..3) # => ["b", "c", "d"] # From range.begin to range.end.
|
3840
|
+
# a.values_at(1...3) # => ["b", "c"] # End excluded.
|
3841
|
+
# a.values_at(3..1) # => [] # No such elements.
|
3842
|
+
#
|
3843
|
+
# a.values_at(-3..3) # => ["b", "c", "d"] # Negative range.begin counts backwards.
|
3844
|
+
# a.values_at(-50..3) # Raises RangeError.
|
3845
|
+
#
|
3846
|
+
# a.values_at(1..-2) # => ["b", "c"] # Negative range.end counts backwards.
|
3847
|
+
# a.values_at(1..-50) # => [] # No such elements.
|
3848
|
+
#
|
3849
|
+
# # Mixture of specifiers.
|
3850
|
+
# a.values_at(2..3, 3, 0..1, 0) # => ["c", "d", "d", "a", "b", "a"]
|
3851
|
+
#
|
3852
|
+
# With no `specifiers` given, returns a new empty array:
|
3853
|
+
#
|
3854
|
+
# a = ['a', 'b', 'c', 'd']
|
3855
|
+
# a.values_at # => []
|
3856
|
+
#
|
3857
|
+
# For each numeric specifier `index`, includes an element:
|
3858
|
+
#
|
3859
|
+
# * For each non-negative numeric specifier `index` that is in-range (less
|
3860
|
+
# than `self.size`), includes the element at offset `index`:
|
3861
|
+
#
|
3862
|
+
# a.values_at(0, 2) # => ["a", "c"]
|
3863
|
+
# a.values_at(0.1, 2.9) # => ["a", "c"]
|
3864
|
+
#
|
3865
|
+
# * For each negative numeric `index` that is in-range (greater than or equal
|
3866
|
+
# to `- self.size`), counts backwards from the end of `self`:
|
3867
|
+
#
|
3868
|
+
# a.values_at(-1, -4) # => ["d", "a"]
|
3869
|
+
#
|
3870
|
+
# The given indexes may be in any order, and may repeat:
|
3871
|
+
#
|
3872
|
+
# a.values_at(2, 0, 1, 0, 2) # => ["c", "a", "b", "a", "c"]
|
3873
|
+
#
|
3874
|
+
# For each `index` that is out-of-range, includes `nil`:
|
3875
|
+
#
|
3876
|
+
# a.values_at(4, -5) # => [nil, nil]
|
3877
|
+
#
|
3878
|
+
# For each Range specifier `range`, includes elements according to `range.begin`
|
3879
|
+
# and `range.end`:
|
3880
|
+
#
|
3881
|
+
# * If both `range.begin` and `range.end` are non-negative and in-range (less
|
3882
|
+
# than `self.size`), includes elements from index `range.begin` through
|
3883
|
+
# `range.end - 1` (if `range.exclude_end?`), or through `range.end`
|
3884
|
+
# (otherwise):
|
3885
|
+
#
|
3886
|
+
# a.values_at(1..2) # => ["b", "c"]
|
3887
|
+
# a.values_at(1...2) # => ["b"]
|
3888
|
+
#
|
3889
|
+
# * If `range.begin` is negative and in-range (greater than or equal to `-
|
3890
|
+
# self.size`), counts backwards from the end of `self`:
|
3891
|
+
#
|
3892
|
+
# a.values_at(-2..3) # => ["c", "d"]
|
3893
|
+
#
|
3894
|
+
# * If `range.begin` is negative and out-of-range, raises an exception:
|
3895
|
+
#
|
3896
|
+
# a.values_at(-5..3) # Raises RangeError.
|
3897
|
+
#
|
3898
|
+
# * If `range.end` is positive and out-of-range, extends the returned array
|
3899
|
+
# with `nil` elements:
|
3900
|
+
#
|
3901
|
+
# a.values_at(1..5) # => ["b", "c", "d", nil, nil]
|
3902
|
+
#
|
3903
|
+
# * If `range.end` is negative and in-range, counts backwards from the end of
|
3904
|
+
# `self`:
|
3905
|
+
#
|
3906
|
+
# a.values_at(1..-2) # => ["b", "c"]
|
3907
|
+
#
|
3908
|
+
# * If `range.end` is negative and out-of-range, returns an empty array:
|
3909
|
+
#
|
3910
|
+
# a.values_at(1..-5) # => []
|
3911
|
+
#
|
3912
|
+
# The given ranges may be in any order and may repeat:
|
3913
|
+
#
|
3914
|
+
# a.values_at(2..3, 0..1, 2..3) # => ["c", "d", "a", "b", "c", "d"]
|
3915
|
+
#
|
3916
|
+
# The given specifiers may be any mixture of indexes and ranges:
|
3917
|
+
#
|
3918
|
+
# a.values_at(3, 1..2, 0, 2..3) # => ["d", "b", "c", "a", "c", "d"]
|
3919
|
+
#
|
3920
|
+
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
|
3921
|
+
#
|
3922
|
+
def values_at: (*int | ::Range[::Integer] selector) -> ::Array[Elem?]
|
3923
|
+
|
3924
|
+
# <!--
|
3925
|
+
# rdoc-file=array.c
|
3926
|
+
# - zip(*other_arrays) -> new_array
|
3927
|
+
# - zip(*other_arrays) {|sub_array| ... } -> nil
|
3928
|
+
# -->
|
3929
|
+
# With no block given, combines `self` with the collection of `other_arrays`;
|
3930
|
+
# returns a new array of sub-arrays:
|
3931
|
+
#
|
3932
|
+
# [0, 1].zip(['zero', 'one'], [:zero, :one])
|
3933
|
+
# # => [[0, "zero", :zero], [1, "one", :one]]
|
3934
|
+
#
|
3935
|
+
# Returned:
|
3936
|
+
#
|
3937
|
+
# * The outer array is of size `self.size`.
|
3938
|
+
# * Each sub-array is of size `other_arrays.size + 1`.
|
3939
|
+
# * The *nth* sub-array contains (in order):
|
3940
|
+
#
|
3941
|
+
# * The *nth* element of `self`.
|
3942
|
+
# * The *nth* element of each of the other arrays, as available.
|
3943
|
+
#
|
3944
|
+
# Example:
|
3945
|
+
#
|
3946
|
+
# a = [0, 1]
|
3947
|
+
# zipped = a.zip(['zero', 'one'], [:zero, :one])
|
3948
|
+
# # => [[0, "zero", :zero], [1, "one", :one]]
|
3949
|
+
# zipped.size # => 2 # Same size as a.
|
3950
|
+
# zipped.first.size # => 3 # Size of other arrays plus 1.
|
3951
|
+
#
|
3952
|
+
# When the other arrays are all the same size as `self`, the returned sub-arrays
|
3953
|
+
# are a rearrangement containing exactly elements of all the arrays (including
|
3954
|
+
# `self`), with no omissions or additions:
|
3955
|
+
#
|
3956
|
+
# a = [:a0, :a1, :a2, :a3]
|
3957
|
+
# b = [:b0, :b1, :b2, :b3]
|
3958
|
+
# c = [:c0, :c1, :c2, :c3]
|
3959
|
+
# d = a.zip(b, c)
|
3960
|
+
# pp d
|
3961
|
+
# # =>
|
3962
|
+
# [[:a0, :b0, :c0],
|
3963
|
+
# [:a1, :b1, :c1],
|
3964
|
+
# [:a2, :b2, :c2],
|
3965
|
+
# [:a3, :b3, :c3]]
|
3966
|
+
#
|
3967
|
+
# When one of the other arrays is smaller than `self`, pads the corresponding
|
3968
|
+
# sub-array with `nil` elements:
|
3969
|
+
#
|
3970
|
+
# a = [:a0, :a1, :a2, :a3]
|
3971
|
+
# b = [:b0, :b1, :b2]
|
3972
|
+
# c = [:c0, :c1]
|
3973
|
+
# d = a.zip(b, c)
|
3974
|
+
# pp d
|
3975
|
+
# # =>
|
3976
|
+
# [[:a0, :b0, :c0],
|
3977
|
+
# [:a1, :b1, :c1],
|
3978
|
+
# [:a2, :b2, nil],
|
3979
|
+
# [:a3, nil, nil]]
|
3980
|
+
#
|
3981
|
+
# When one of the other arrays is larger than `self`, *ignores* its trailing
|
3982
|
+
# elements:
|
3983
|
+
#
|
3984
|
+
# a = [:a0, :a1, :a2, :a3]
|
3985
|
+
# b = [:b0, :b1, :b2, :b3, :b4]
|
3986
|
+
# c = [:c0, :c1, :c2, :c3, :c4, :c5]
|
3987
|
+
# d = a.zip(b, c)
|
3988
|
+
# pp d
|
3989
|
+
# # =>
|
3990
|
+
# [[:a0, :b0, :c0],
|
3991
|
+
# [:a1, :b1, :c1],
|
3992
|
+
# [:a2, :b2, :c2],
|
3993
|
+
# [:a3, :b3, :c3]]
|
3994
|
+
#
|
3995
|
+
# With a block given, calls the block with each of the other arrays; returns
|
3996
|
+
# `nil`:
|
3997
|
+
#
|
3998
|
+
# d = []
|
3999
|
+
# a = [:a0, :a1, :a2, :a3]
|
4000
|
+
# b = [:b0, :b1, :b2, :b3]
|
4001
|
+
# c = [:c0, :c1, :c2, :c3]
|
4002
|
+
# a.zip(b, c) {|sub_array| d.push(sub_array.reverse) } # => nil
|
4003
|
+
# pp d
|
4004
|
+
# # =>
|
4005
|
+
# [[:c0, :b0, :a0],
|
4006
|
+
# [:c1, :b1, :a1],
|
4007
|
+
# [:c2, :b2, :a2],
|
4008
|
+
# [:c3, :b3, :a3]]
|
4009
|
+
#
|
4010
|
+
# For an **object** in **other_arrays** that is not actually an array, forms the
|
4011
|
+
# the "other array" as `object.to_ary`, if defined, or as `object.each.to_a`
|
4012
|
+
# otherwise.
|
4013
|
+
#
|
4014
|
+
# Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting).
|
4015
|
+
#
|
4016
|
+
def zip: [U] (_Each[U] arg) -> Array[[ Elem, U? ]]
|
4017
|
+
| (_Each[untyped] arg, *_Each[untyped] args) -> Array[Array[untyped]]
|
4018
|
+
| [U] (_Each[U] arg) { ([ Elem, U? ]) -> void } -> nil
|
4019
|
+
| (_Each[untyped] arg, *_Each[untyped] args) { (Array[untyped]) -> void } -> nil
|
4020
|
+
|
4021
|
+
# <!--
|
4022
|
+
# rdoc-file=array.c
|
4023
|
+
# - self | other_array -> new_array
|
4024
|
+
# -->
|
4025
|
+
# Returns the union of `self` and `other_array`; duplicates are removed; order
|
4026
|
+
# is preserved; items are compared using `eql?`:
|
4027
|
+
#
|
4028
|
+
# [0, 1] | [2, 3] # => [0, 1, 2, 3]
|
4029
|
+
# [0, 1, 1] | [2, 2, 3] # => [0, 1, 2, 3]
|
4030
|
+
# [0, 1, 2] | [3, 2, 1, 0] # => [0, 1, 2, 3]
|
4031
|
+
#
|
4032
|
+
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
|
4033
|
+
#
|
4034
|
+
def |: [T] (::Array[T] other_ary) -> ::Array[Elem | T]
|
4035
|
+
|
4036
|
+
private
|
4037
|
+
|
4038
|
+
# <!--
|
4039
|
+
# rdoc-file=array.c
|
4040
|
+
# - initialize_copy(other_array) -> self
|
4041
|
+
# - replace(other_array) -> self
|
4042
|
+
# -->
|
4043
|
+
# Replaces the elements of `self` with the elements of `other_array`, which must
|
4044
|
+
# be an [array-convertible
|
4045
|
+
# object](rdoc-ref:implicit_conversion.rdoc@Array-Convertible+Objects); returns
|
4046
|
+
# `self`:
|
4047
|
+
#
|
4048
|
+
# a = ['a', 'b', 'c'] # => ["a", "b", "c"]
|
4049
|
+
# a.replace(['d', 'e']) # => ["d", "e"]
|
4050
|
+
#
|
4051
|
+
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
|
4052
|
+
#
|
4053
|
+
def initialize_copy: (self other_ary) -> void
|
4054
|
+
end
|
4055
|
+
|
4056
|
+
interface _Rand
|
4057
|
+
def rand: (::Integer max) -> ::Integer
|
4058
|
+
end
|
4059
|
+
|
4060
|
+
interface Array::_Pattern[T]
|
4061
|
+
def ===: (T) -> bool
|
4062
|
+
end
|