rbs 2.8.4 → 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +12 -4
- data/.github/workflows/comments.yml +11 -11
- data/.github/workflows/dependabot.yml +30 -0
- data/.github/workflows/ruby.yml +40 -49
- data/.github/workflows/typecheck.yml +36 -0
- data/.github/workflows/windows.yml +28 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +42 -2
- data/CHANGELOG.md +845 -1
- data/README.md +64 -4
- data/Rakefile +198 -18
- data/Steepfile +11 -11
- data/config.yml +311 -0
- data/core/array.rbs +2189 -1914
- data/core/basic_object.rbs +59 -84
- data/core/binding.rbs +7 -69
- data/core/builtin.rbs +210 -11
- data/core/class.rbs +37 -0
- data/core/comparable.rbs +23 -25
- data/core/complex.rbs +449 -227
- data/core/constants.rbs +29 -21
- data/core/data.rbs +415 -0
- data/core/dir.rbs +698 -415
- data/core/encoding.rbs +468 -843
- data/core/enumerable.rbs +495 -455
- data/core/enumerator/product.rbs +92 -0
- data/core/enumerator.rbs +106 -9
- data/core/env.rbs +1 -1
- data/core/errno.rbs +506 -605
- data/core/errors.rbs +15 -17
- data/core/exception.rbs +361 -145
- data/core/false_class.rbs +39 -26
- data/core/fiber.rbs +121 -14
- data/core/file.rbs +1262 -320
- data/core/file_test.rbs +62 -45
- data/core/float.rbs +187 -208
- data/core/gc.rbs +446 -196
- data/core/global_variables.rbs +29 -29
- data/core/hash.rbs +242 -349
- data/core/integer.rbs +246 -308
- data/core/io/buffer.rbs +373 -122
- data/core/io/wait.rbs +29 -17
- data/core/io.rbs +1881 -1518
- data/core/kernel.rbs +2116 -1538
- data/core/marshal.rbs +24 -14
- data/core/match_data.rbs +413 -166
- data/core/math.rbs +531 -291
- data/core/method.rbs +101 -32
- data/core/module.rbs +228 -64
- data/core/nil_class.rbs +106 -47
- data/core/numeric.rbs +206 -292
- data/core/object.rbs +73 -1168
- data/core/object_space/weak_key_map.rbs +166 -0
- data/core/object_space.rbs +5 -3
- data/core/proc.rbs +280 -39
- data/core/process.rbs +1318 -658
- data/core/ractor.rbs +200 -134
- data/core/random.rbs +21 -4
- data/core/range.rbs +309 -153
- data/core/rational.rbs +4 -12
- data/core/rb_config.rbs +64 -43
- data/core/rbs/unnamed/argf.rbs +411 -147
- data/core/rbs/unnamed/env_class.rbs +137 -253
- data/core/rbs/unnamed/random.rbs +49 -26
- data/core/refinement.rbs +16 -1
- data/core/regexp.rbs +1568 -862
- data/core/ruby_vm.rbs +719 -7
- data/core/rubygems/config_file.rbs +3 -0
- data/core/rubygems/errors.rbs +69 -6
- data/core/rubygems/rubygems.rbs +71 -17
- data/core/rubygems/version.rbs +11 -7
- data/{stdlib/set/0 → core}/set.rbs +80 -91
- data/core/signal.rbs +14 -8
- data/core/string.rbs +1732 -1607
- data/core/struct.rbs +467 -95
- data/core/symbol.rbs +215 -245
- data/core/thread.rbs +133 -89
- data/core/thread_group.rbs +9 -9
- data/core/time.rbs +1141 -841
- data/core/trace_point.rbs +181 -121
- data/core/true_class.rbs +58 -32
- data/core/unbound_method.rbs +103 -30
- data/core/warning.rbs +50 -5
- data/docs/CONTRIBUTING.md +1 -1
- data/docs/architecture.md +110 -0
- data/docs/collection.md +59 -5
- data/docs/data_and_struct.md +86 -0
- data/docs/gem.md +57 -0
- data/docs/rbs_by_example.md +16 -35
- data/docs/repo.md +1 -1
- data/docs/sigs.md +7 -7
- data/docs/stdlib.md +63 -5
- data/docs/syntax.md +255 -61
- data/docs/tools.md +1 -0
- data/ext/rbs_extension/extconf.rb +10 -0
- data/ext/rbs_extension/lexer.c +1741 -1548
- data/ext/rbs_extension/lexer.h +11 -1
- data/ext/rbs_extension/lexer.re +12 -6
- data/ext/rbs_extension/lexstate.c +26 -3
- data/ext/rbs_extension/location.c +119 -111
- data/ext/rbs_extension/location.h +32 -7
- data/ext/rbs_extension/main.c +3 -0
- data/ext/rbs_extension/parser.c +883 -481
- data/ext/rbs_extension/parserstate.c +65 -25
- data/ext/rbs_extension/parserstate.h +13 -3
- data/ext/rbs_extension/rbs_extension.h +1 -10
- data/ext/rbs_extension/unescape.c +7 -47
- data/goodcheck.yml +2 -2
- data/{ext/rbs_extension → include/rbs}/constants.h +26 -15
- data/include/rbs/ruby_objs.h +72 -0
- data/include/rbs.h +7 -0
- data/lib/rbs/annotate/annotations.rb +3 -3
- data/lib/rbs/annotate/formatter.rb +13 -3
- data/lib/rbs/annotate/rdoc_annotator.rb +1 -1
- data/lib/rbs/annotate/rdoc_source.rb +12 -3
- data/lib/rbs/ast/declarations.rb +85 -2
- data/lib/rbs/ast/directives.rb +39 -0
- data/lib/rbs/ast/members.rb +49 -15
- data/lib/rbs/ast/type_param.rb +104 -15
- data/lib/rbs/ast/visitor.rb +137 -0
- data/lib/rbs/buffer.rb +5 -0
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +83 -0
- data/lib/rbs/cli/validate.rb +356 -0
- data/lib/rbs/cli.rb +253 -143
- data/lib/rbs/collection/cleaner.rb +8 -1
- data/lib/rbs/collection/config/lockfile.rb +92 -0
- data/lib/rbs/collection/config/lockfile_generator.rb +154 -65
- data/lib/rbs/collection/config.rb +19 -46
- data/lib/rbs/collection/installer.rb +12 -13
- data/lib/rbs/collection/sources/base.rb +2 -2
- data/lib/rbs/collection/sources/git.rb +146 -69
- data/lib/rbs/collection/sources/local.rb +81 -0
- data/lib/rbs/collection/sources/rubygems.rb +10 -12
- data/lib/rbs/collection/sources/stdlib.rb +14 -13
- data/lib/rbs/collection/sources.rb +15 -2
- data/lib/rbs/collection.rb +2 -1
- data/lib/rbs/definition.rb +13 -16
- data/lib/rbs/definition_builder/ancestor_builder.rb +100 -24
- data/lib/rbs/definition_builder/method_builder.rb +4 -4
- data/lib/rbs/definition_builder.rb +489 -584
- data/lib/rbs/diff.rb +125 -0
- data/lib/rbs/environment/use_map.rb +77 -0
- data/lib/rbs/environment.rb +406 -105
- data/lib/rbs/environment_loader.rb +48 -44
- data/lib/rbs/environment_walker.rb +1 -1
- data/lib/rbs/errors.rb +175 -56
- data/lib/rbs/file_finder.rb +28 -0
- data/lib/rbs/location_aux.rb +8 -7
- data/lib/rbs/locator.rb +37 -15
- data/lib/rbs/method_type.rb +23 -0
- data/lib/rbs/namespace.rb +1 -0
- data/lib/rbs/parser/lex_result.rb +15 -0
- data/lib/rbs/parser/token.rb +23 -0
- data/lib/rbs/parser_aux.rb +22 -13
- data/lib/rbs/prototype/helpers.rb +48 -22
- data/lib/rbs/prototype/node_usage.rb +99 -0
- data/lib/rbs/prototype/rb.rb +125 -31
- data/lib/rbs/prototype/rbi.rb +49 -36
- 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 +273 -159
- data/lib/rbs/resolver/constant_resolver.rb +24 -8
- data/lib/rbs/resolver/type_name_resolver.rb +41 -7
- data/lib/rbs/sorter.rb +153 -123
- data/lib/rbs/substitution.rb +19 -0
- data/lib/rbs/subtractor.rb +201 -0
- data/lib/rbs/test/errors.rb +24 -11
- data/lib/rbs/test/guaranteed.rb +30 -0
- data/lib/rbs/test/hook.rb +45 -40
- data/lib/rbs/test/setup.rb +1 -1
- data/lib/rbs/test/tester.rb +1 -1
- data/lib/rbs/test/type_check.rb +120 -23
- data/lib/rbs/test.rb +6 -3
- data/lib/rbs/type_alias_dependency.rb +13 -3
- data/lib/rbs/type_alias_regularity.rb +21 -14
- data/lib/rbs/type_name.rb +18 -13
- data/lib/rbs/types.rb +352 -18
- data/lib/rbs/unit_test/convertibles.rb +176 -0
- data/lib/rbs/unit_test/spy.rb +136 -0
- data/lib/rbs/unit_test/type_assertions.rb +341 -0
- data/lib/rbs/unit_test/with_aliases.rb +143 -0
- data/lib/rbs/unit_test.rb +6 -0
- data/lib/rbs/validator.rb +55 -30
- data/lib/rbs/variance_calculator.rb +26 -23
- data/lib/rbs/vendorer.rb +3 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +69 -22
- data/lib/rbs.rb +7 -2
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +5 -5
- data/rbs.gemspec +12 -2
- data/schema/decls.json +1 -1
- data/schema/members.json +15 -10
- data/sig/ancestor_builder.rbs +4 -0
- data/sig/ancestor_graph.rbs +22 -2
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- 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 +4 -0
- data/sig/collection/config/lockfile.rbs +74 -0
- data/sig/collection/config/lockfile_generator.rbs +66 -0
- data/sig/collection/config.rbs +5 -48
- data/sig/collection/installer.rbs +1 -1
- data/sig/collection/sources.rbs +105 -33
- data/sig/constant.rbs +1 -1
- data/sig/declarations.rbs +42 -3
- data/sig/definition.rbs +26 -10
- data/sig/definition_builder.rbs +103 -81
- data/sig/diff.rbs +28 -0
- data/sig/directives.rbs +61 -0
- data/sig/environment.rbs +175 -29
- data/sig/environment_loader.rbs +20 -18
- data/sig/errors.rbs +123 -2
- data/sig/file_finder.rbs +28 -0
- data/sig/location.rbs +0 -3
- data/sig/locator.rbs +14 -2
- data/sig/manifest.yaml +0 -1
- data/sig/members.rbs +32 -9
- data/sig/method_types.rbs +10 -4
- data/sig/namespace.rbs +2 -3
- data/sig/parser.rbs +55 -16
- data/sig/prototype/helpers.rbs +4 -0
- data/sig/prototype/node_usage.rbs +20 -0
- data/sig/prototype/rb.rbs +10 -2
- data/sig/prototype/rbi.rbs +2 -0
- data/sig/prototype/runtime.rbs +182 -0
- data/sig/rbs.rbs +1 -1
- data/sig/rdoc/rbs.rbs +4 -0
- data/sig/repository.rbs +7 -5
- data/sig/resolver/constant_resolver.rbs +3 -4
- data/sig/resolver/context.rbs +1 -1
- data/sig/resolver/type_name_resolver.rbs +5 -1
- data/sig/shims/bundler.rbs +38 -0
- data/sig/shims/rubygems.rbs +19 -0
- data/sig/sorter.rbs +23 -5
- data/sig/substitution.rbs +6 -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 +31 -0
- data/sig/type_alias_regularity.rbs +12 -6
- data/sig/type_param.rbs +45 -9
- data/sig/typename.rbs +8 -5
- data/sig/types.rbs +119 -12
- data/sig/unit_test/convertibles.rbs +154 -0
- data/sig/unit_test/spy.rbs +28 -0
- data/sig/unit_test/type_assertions.rbs +194 -0
- data/sig/unit_test/with_aliases.rbs +136 -0
- data/sig/use_map.rbs +35 -0
- data/sig/validator.rbs +12 -5
- data/sig/variance_calculator.rbs +3 -1
- data/sig/vendorer.rbs +1 -1
- data/sig/visitor.rbs +47 -0
- data/sig/writer.rbs +6 -2
- data/src/constants.c +153 -0
- data/src/ruby_objs.c +793 -0
- data/stdlib/base64/0/base64.rbs +298 -45
- data/stdlib/benchmark/0/benchmark.rbs +12 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +62 -198
- data/stdlib/cgi/0/core.rbs +68 -15
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/coverage/0/coverage.rbs +50 -11
- data/stdlib/csv/0/csv.rbs +90 -119
- data/stdlib/csv/0/manifest.yaml +1 -0
- data/stdlib/date/0/date.rbs +806 -735
- data/stdlib/date/0/date_time.rbs +70 -211
- data/stdlib/dbm/0/dbm.rbs +0 -2
- 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 +3 -8
- data/stdlib/digest/0/digest.rbs +48 -35
- data/stdlib/erb/0/erb.rbs +15 -39
- data/stdlib/etc/0/etc.rbs +174 -54
- data/stdlib/fileutils/0/fileutils.rbs +1234 -385
- data/stdlib/forwardable/0/forwardable.rbs +4 -4
- data/stdlib/io-console/0/io-console.rbs +82 -17
- data/stdlib/ipaddr/0/ipaddr.rbs +11 -6
- data/stdlib/json/0/json.rbs +434 -151
- data/stdlib/kconv/0/kconv.rbs +166 -0
- data/stdlib/logger/0/formatter.rbs +0 -2
- data/stdlib/logger/0/log_device.rbs +1 -3
- data/stdlib/logger/0/logger.rbs +465 -328
- data/stdlib/minitest/0/kernel.rbs +2 -2
- data/stdlib/minitest/0/minitest/abstract_reporter.rbs +4 -1
- data/stdlib/minitest/0/minitest/assertion.rbs +1 -0
- data/stdlib/minitest/0/minitest/assertions.rbs +58 -13
- data/stdlib/minitest/0/minitest/backtrace_filter.rbs +7 -0
- data/stdlib/minitest/0/minitest/bench_spec.rbs +8 -8
- data/stdlib/minitest/0/minitest/benchmark.rbs +17 -16
- 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/mock.rbs +9 -5
- data/stdlib/minitest/0/minitest/parallel/executor.rbs +4 -0
- data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +0 -1
- data/stdlib/minitest/0/minitest/pride_io.rbs +8 -0
- data/stdlib/minitest/0/minitest/pride_lol.rbs +2 -0
- data/stdlib/minitest/0/minitest/progress_reporter.rbs +1 -1
- data/stdlib/minitest/0/minitest/reportable.rbs +2 -0
- data/stdlib/minitest/0/minitest/runnable.rbs +33 -1
- data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +1 -1
- data/stdlib/minitest/0/minitest/spec/dsl.rbs +10 -6
- data/stdlib/minitest/0/minitest/spec.rbs +1 -1
- data/stdlib/minitest/0/minitest/statistics_reporter.rbs +5 -0
- data/stdlib/minitest/0/minitest/summary_reporter.rbs +0 -7
- data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +7 -7
- data/stdlib/minitest/0/minitest/test.rbs +7 -14
- data/stdlib/minitest/0/minitest/unexpected_error.rbs +2 -0
- data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
- data/stdlib/minitest/0/minitest/unit.rbs +1 -2
- data/stdlib/minitest/0/minitest.rbs +41 -892
- data/stdlib/monitor/0/monitor.rbs +91 -10
- data/stdlib/mutex_m/0/mutex_m.rbs +0 -2
- data/stdlib/net-http/0/manifest.yaml +1 -1
- data/stdlib/net-http/0/net-http.rbs +3858 -964
- 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 +35 -5
- data/stdlib/objspace/0/objspace.rbs +40 -18
- 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 +1 -0
- data/stdlib/openssl/0/openssl.rbs +681 -316
- data/stdlib/optparse/0/optparse.rbs +100 -65
- data/stdlib/pathname/0/pathname.rbs +24 -15
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +300 -0
- data/stdlib/prettyprint/0/prettyprint.rbs +2 -6
- data/stdlib/pstore/0/pstore.rbs +370 -156
- data/stdlib/psych/0/core_ext.rbs +12 -0
- data/stdlib/{yaml → psych}/0/dbm.rbs +3 -3
- data/stdlib/psych/0/manifest.yaml +3 -0
- data/stdlib/psych/0/psych.rbs +402 -0
- data/stdlib/{yaml → psych}/0/store.rbs +2 -2
- data/stdlib/pty/0/pty.rbs +63 -11
- 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 +13 -380
- 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/resolv.rbs +16 -79
- data/stdlib/ripper/0/ripper.rbs +1648 -0
- data/stdlib/securerandom/0/securerandom.rbs +7 -2
- data/stdlib/shellwords/0/shellwords.rbs +11 -12
- data/stdlib/singleton/0/singleton.rbs +0 -3
- data/stdlib/socket/0/addrinfo.rbs +13 -18
- data/stdlib/socket/0/basic_socket.rbs +5 -10
- data/stdlib/socket/0/ip_socket.rbs +0 -2
- data/stdlib/socket/0/socket.rbs +77 -46
- data/stdlib/socket/0/tcp_server.rbs +0 -5
- data/stdlib/socket/0/tcp_socket.rbs +36 -3
- data/stdlib/socket/0/udp_socket.rbs +4 -5
- data/stdlib/socket/0/unix_server.rbs +0 -5
- data/stdlib/socket/0/unix_socket.rbs +2 -4
- data/{core/string_io.rbs → stdlib/stringio/0/stringio.rbs} +188 -107
- data/stdlib/strscan/0/string_scanner.rbs +1269 -425
- data/stdlib/tempfile/0/tempfile.rbs +224 -61
- data/stdlib/time/0/time.rbs +48 -35
- data/stdlib/timeout/0/timeout.rbs +17 -8
- data/stdlib/tmpdir/0/tmpdir.rbs +10 -3
- data/stdlib/tsort/0/tsort.rbs +0 -4
- data/stdlib/uri/0/common.rbs +271 -144
- data/stdlib/uri/0/file.rbs +5 -0
- data/stdlib/uri/0/ftp.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +26 -22
- data/stdlib/uri/0/http.rbs +4 -4
- data/stdlib/uri/0/ldap.rbs +1 -1
- data/stdlib/uri/0/mailto.rbs +84 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +3 -0
- data/stdlib/yaml/0/manifest.yaml +1 -2
- data/stdlib/yaml/0/yaml.rbs +1 -199
- 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 +1 -3
- data/stdlib/zlib/0/zstream.rbs +200 -0
- data/templates/include/rbs/constants.h.erb +20 -0
- data/templates/include/rbs/ruby_objs.h.erb +10 -0
- data/templates/src/constants.c.erb +36 -0
- data/templates/src/ruby_objs.c.erb +27 -0
- data/templates/template.rb +122 -0
- metadata +136 -36
- data/Gemfile +0 -33
- data/Gemfile.lock +0 -118
- data/core/deprecated.rbs +0 -9
- data/ext/rbs_extension/constants.c +0 -135
- data/ext/rbs_extension/ruby_objs.c +0 -525
- data/ext/rbs_extension/ruby_objs.h +0 -43
- data/lib/rbs/constant_table.rb +0 -167
- data/lib/rbs/parser_compat/lexer_error.rb +0 -6
- data/lib/rbs/parser_compat/located_value.rb +0 -7
- data/lib/rbs/parser_compat/semantics_error.rb +0 -6
- data/lib/rbs/parser_compat/syntax_error.rb +0 -6
- data/lib/rbs/test/spy.rb +0 -6
- data/lib/rbs/type_name_resolver.rb +0 -67
- data/sig/constant_table.rbs +0 -30
- data/sig/shims/abstract_syntax_tree.rbs +0 -25
- data/sig/shims/pp.rbs +0 -3
- data/sig/shims/ripper.rbs +0 -8
- data/sig/shims.rbs +0 -69
- data/sig/type_name_resolver.rbs +0 -26
- data/stdlib/minitest/0/manifest.yaml +0 -2
- data/stdlib/prime/0/integer-extension.rbs +0 -41
- data/stdlib/prime/0/manifest.yaml +0 -2
- data/stdlib/prime/0/prime.rbs +0 -372
data/core/string.rbs
CHANGED
@@ -1,63 +1,59 @@
|
|
1
|
-
# <!-- rdoc-file=string.
|
2
|
-
# A String object has an arbitrary sequence of bytes, typically representing
|
3
|
-
# text or binary data. A String object may be created using String::new or as
|
1
|
+
# <!-- rdoc-file=string.rb -->
|
2
|
+
# A `String` object has an arbitrary sequence of bytes, typically representing
|
3
|
+
# text or binary data. A `String` object may be created using String::new or as
|
4
4
|
# literals.
|
5
5
|
#
|
6
6
|
# String objects differ from Symbol objects in that Symbol objects are designed
|
7
7
|
# to be used as identifiers, instead of text or data.
|
8
8
|
#
|
9
|
-
# You can create a String object explicitly with:
|
10
|
-
#
|
11
|
-
# * A [string literal](doc/syntax/literals_rdoc.html#label-String+Literals).
|
12
|
-
# * A [heredoc
|
13
|
-
# literal](doc/syntax/literals_rdoc.html#label-Here+Document+Literals).
|
9
|
+
# You can create a `String` object explicitly with:
|
14
10
|
#
|
11
|
+
# * A [string literal](rdoc-ref:syntax/literals.rdoc@String+Literals).
|
12
|
+
# * A [heredoc literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals).
|
15
13
|
#
|
16
14
|
# You can convert certain objects to Strings with:
|
17
15
|
#
|
18
|
-
# * Method
|
19
|
-
#
|
16
|
+
# * Method #String.
|
20
17
|
#
|
21
|
-
# Some String methods modify `self`. Typically, a method whose name ends with
|
22
|
-
# `!` modifies `self` and returns `self`; often a similarly named method
|
18
|
+
# Some `String` methods modify `self`. Typically, a method whose name ends with
|
19
|
+
# `!` modifies `self` and returns `self`; often, a similarly named method
|
23
20
|
# (without the `!`) returns a new string.
|
24
21
|
#
|
25
|
-
# In general, if
|
26
|
-
# mutates and the non-bang
|
27
|
-
# mutate, such as String#replace.
|
22
|
+
# In general, if both bang and non-bang versions of a method exist, the bang
|
23
|
+
# method mutates and the non-bang method does not. However, a method without a
|
24
|
+
# bang can also mutate, such as String#replace.
|
28
25
|
#
|
29
26
|
# ## Substitution Methods
|
30
27
|
#
|
31
28
|
# These methods perform substitutions:
|
32
29
|
#
|
33
30
|
# * String#sub: One substitution (or none); returns a new string.
|
34
|
-
# * String#sub!: One substitution (or none); returns `self
|
31
|
+
# * String#sub!: One substitution (or none); returns `self` if any changes,
|
32
|
+
# `nil` otherwise.
|
35
33
|
# * String#gsub: Zero or more substitutions; returns a new string.
|
36
|
-
# * String#gsub!: Zero or more substitutions; returns `self
|
37
|
-
#
|
34
|
+
# * String#gsub!: Zero or more substitutions; returns `self` if any changes,
|
35
|
+
# `nil` otherwise.
|
38
36
|
#
|
39
37
|
# Each of these methods takes:
|
40
38
|
#
|
41
|
-
# * A first argument, `pattern` (
|
39
|
+
# * A first argument, `pattern` (String or Regexp), that specifies the
|
42
40
|
# substring(s) to be replaced.
|
43
41
|
#
|
44
|
-
# * Either of
|
42
|
+
# * Either of the following:
|
45
43
|
#
|
46
|
-
# * A second argument, `replacement` (
|
44
|
+
# * A second argument, `replacement` (String or Hash), that determines the
|
47
45
|
# replacing string.
|
48
46
|
# * A block that will determine the replacing string.
|
49
47
|
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# The examples in this section mostly use methods String#sub and String#gsub;
|
53
|
-
# the principles illustrated apply to all four substitution methods.
|
48
|
+
# The examples in this section mostly use the String#sub and String#gsub
|
49
|
+
# methods; the principles illustrated apply to all four substitution methods.
|
54
50
|
#
|
55
51
|
# **Argument `pattern`**
|
56
52
|
#
|
57
53
|
# Argument `pattern` is commonly a regular expression:
|
58
54
|
#
|
59
55
|
# s = 'hello'
|
60
|
-
# s.sub(/[aeiou]/, '*')
|
56
|
+
# s.sub(/[aeiou]/, '*') # => "h*llo"
|
61
57
|
# s.gsub(/[aeiou]/, '*') # => "h*ll*"
|
62
58
|
# s.gsub(/[aeiou]/, '') # => "hll"
|
63
59
|
# s.sub(/ell/, 'al') # => "halo"
|
@@ -65,60 +61,57 @@
|
|
65
61
|
# 'THX1138'.gsub(/\d+/, '00') # => "THX00"
|
66
62
|
#
|
67
63
|
# When `pattern` is a string, all its characters are treated as ordinary
|
68
|
-
# characters (not as
|
64
|
+
# characters (not as Regexp special characters):
|
69
65
|
#
|
70
66
|
# 'THX1138'.gsub('\d+', '00') # => "THX1138"
|
71
67
|
#
|
72
|
-
#
|
68
|
+
# **`String` `replacement`**
|
73
69
|
#
|
74
|
-
# If `replacement` is a string, that string
|
75
|
-
#
|
70
|
+
# If `replacement` is a string, that string determines the replacing string that
|
71
|
+
# is substituted for the matched text.
|
76
72
|
#
|
77
73
|
# Each of the examples above uses a simple string as the replacing string.
|
78
74
|
#
|
79
|
-
# String `replacement` may contain back-references to the pattern's captures:
|
75
|
+
# `String` `replacement` may contain back-references to the pattern's captures:
|
80
76
|
#
|
81
|
-
# * `\n` (*n* a non-negative integer) refers to `$n`.
|
77
|
+
# * `\n` (*n* is a non-negative integer) refers to `$n`.
|
82
78
|
# * `\k<name>` refers to the named capture `name`.
|
83
79
|
#
|
84
|
-
#
|
85
|
-
# See regexp.rdoc for details.
|
80
|
+
# See Regexp for details.
|
86
81
|
#
|
87
82
|
# Note that within the string `replacement`, a character combination such as
|
88
|
-
# `$&` is treated as ordinary text,
|
89
|
-
#
|
90
|
-
# combinations:
|
83
|
+
# `$&` is treated as ordinary text, not as a special match variable. However,
|
84
|
+
# you may refer to some special match variables using these combinations:
|
91
85
|
#
|
92
86
|
# * `\&` and `\0` correspond to `$&`, which contains the complete matched
|
93
87
|
# text.
|
94
|
-
# * `\'` corresponds to `$'`, which contains string after match.
|
95
|
-
# * `\`` corresponds to `$``, which contains string before match.
|
96
|
-
# *
|
88
|
+
# * `\'` corresponds to `$'`, which contains the string after the match.
|
89
|
+
# * `\`` corresponds to `$``, which contains the string before the match.
|
90
|
+
# * `\+` corresponds to `$+`, which contains the last capture group.
|
97
91
|
#
|
98
|
-
#
|
99
|
-
# See regexp.rdoc for details.
|
92
|
+
# See Regexp for details.
|
100
93
|
#
|
101
94
|
# Note that `\\\` is interpreted as an escape, i.e., a single backslash.
|
102
95
|
#
|
103
96
|
# Note also that a string literal consumes backslashes. See [String
|
104
|
-
# Literals](
|
105
|
-
#
|
97
|
+
# Literals](rdoc-ref:syntax/literals.rdoc@String+Literals) for details about
|
98
|
+
# string literals.
|
106
99
|
#
|
107
100
|
# A back-reference is typically preceded by an additional backslash. For
|
108
101
|
# example, if you want to write a back-reference `\&` in `replacement` with a
|
109
102
|
# double-quoted string literal, you need to write `"..\\\\&.."`.
|
110
103
|
#
|
111
104
|
# If you want to write a non-back-reference string `\&` in `replacement`, you
|
112
|
-
# need first
|
105
|
+
# need to first escape the backslash to prevent this method from interpreting it
|
113
106
|
# as a back-reference, and then you need to escape the backslashes again to
|
114
107
|
# prevent a string literal from consuming them: `"..\\\\\\\\&.."`.
|
115
108
|
#
|
116
|
-
# You may want to use the block form to avoid
|
109
|
+
# You may want to use the block form to avoid excessive backslashes.
|
117
110
|
#
|
118
111
|
# **\Hash `replacement`**
|
119
112
|
#
|
120
|
-
# If argument `replacement` is a hash, and `pattern` matches one of its
|
121
|
-
# the replacing string is the value for that key:
|
113
|
+
# If the argument `replacement` is a hash, and `pattern` matches one of its
|
114
|
+
# keys, the replacing string is the value for that key:
|
122
115
|
#
|
123
116
|
# h = {'foo' => 'bar', 'baz' => 'bat'}
|
124
117
|
# 'food'.sub('foo', h) # => "bard"
|
@@ -133,584 +126,569 @@
|
|
133
126
|
# In the block form, the current match string is passed to the block; the
|
134
127
|
# block's return value becomes the replacing string:
|
135
128
|
#
|
136
|
-
#
|
137
|
-
# '1234'.gsub(/\d/) {|match| s.succ! } # => "ABCD"
|
129
|
+
# s = '@'
|
130
|
+
# '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
|
138
131
|
#
|
139
132
|
# Special match variables such as `$1`, `$2`, `$``, `$&`, and `$'` are set
|
140
133
|
# appropriately.
|
141
134
|
#
|
142
|
-
# ##
|
135
|
+
# ## Whitespace in Strings
|
143
136
|
#
|
144
|
-
#
|
137
|
+
# In the class `String`, *whitespace* is defined as a contiguous sequence of
|
138
|
+
# characters consisting of any mixture of the following:
|
145
139
|
#
|
146
|
-
# *
|
147
|
-
#
|
148
|
-
# *
|
149
|
-
#
|
140
|
+
# * NL (null): `"\x00"`, `"\u0000"`.
|
141
|
+
# * HT (horizontal tab): `"\x09"`, `"\t"`.
|
142
|
+
# * LF (line feed): `"\x0a"`, `"\n"`.
|
143
|
+
# * VT (vertical tab): `"\x0b"`, `"\v"`.
|
144
|
+
# * FF (form feed): `"\x0c"`, `"\f"`.
|
145
|
+
# * CR (carriage return): `"\x0d"`, `"\r"`.
|
146
|
+
# * SP (space): `"\x20"`, `" "`.
|
150
147
|
#
|
148
|
+
# Whitespace is relevant for the following methods:
|
151
149
|
#
|
152
|
-
#
|
150
|
+
# * #lstrip, #lstrip!: Strip leading whitespace.
|
151
|
+
# * #rstrip, #rstrip!: Strip trailing whitespace.
|
152
|
+
# * #strip, #strip!: Strip leading and trailing whitespace.
|
153
153
|
#
|
154
|
-
#
|
155
|
-
# * [Frozen/Unfrozen
|
156
|
-
# Strings](#class-String-label-Methods+for+a+Frozen-2FUnfrozen+String)
|
157
|
-
# * [Querying](#class-String-label-Methods+for+Querying)
|
158
|
-
# * [Comparing](#class-String-label-Methods+for+Comparing)
|
159
|
-
# * [Modifying a String](#class-String-label-Methods+for+Modifying+a+String)
|
160
|
-
# * [Converting to New
|
161
|
-
# String](#class-String-label-Methods+for+Converting+to+New+String)
|
162
|
-
# * [Converting to
|
163
|
-
# Non-String](#class-String-label-Methods+for+Converting+to+Non--5CString)
|
164
|
-
# * [Iterating](#class-String-label-Methods+for+Iterating)
|
154
|
+
# ## `String` Slices
|
165
155
|
#
|
156
|
+
# A *slice* of a string is a substring selected by certain criteria.
|
166
157
|
#
|
167
|
-
#
|
158
|
+
# These instance methods utilize slicing:
|
168
159
|
#
|
169
|
-
#
|
170
|
-
#
|
160
|
+
# * String#[] (aliased as String#slice): Returns a slice copied from `self`.
|
161
|
+
# * String#[]=: Mutates `self` with the slice replaced.
|
162
|
+
# * String#slice!: Mutates `self` with the slice removed and returns the
|
163
|
+
# removed slice.
|
171
164
|
#
|
172
|
-
#
|
173
|
-
#
|
165
|
+
# Each of the above methods takes arguments that determine the slice to be
|
166
|
+
# copied or replaced.
|
174
167
|
#
|
168
|
+
# The arguments have several forms. For a string `string`, the forms are:
|
175
169
|
#
|
170
|
+
# * `string[index]`
|
171
|
+
# * `string[start, length]`
|
172
|
+
# * `string[range]`
|
173
|
+
# * `string[regexp, capture = 0]`
|
174
|
+
# * `string[substring]`
|
176
175
|
#
|
177
|
-
#
|
176
|
+
# **`string[index]`**
|
178
177
|
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
# otherwise.
|
178
|
+
# When a non-negative integer argument `index` is given, the slice is the
|
179
|
+
# 1-character substring found in `self` at character offset `index`:
|
182
180
|
#
|
183
|
-
# [
|
184
|
-
#
|
185
|
-
#
|
181
|
+
# 'bar'[0] # => "b"
|
182
|
+
# 'bar'[2] # => "r"
|
183
|
+
# 'bar'[20] # => nil
|
184
|
+
# 'тест'[2] # => "с"
|
185
|
+
# 'こんにちは'[4] # => "は"
|
186
186
|
#
|
187
|
-
#
|
188
|
-
#
|
187
|
+
# When a negative integer `index` is given, the slice begins at the offset given
|
188
|
+
# by counting backward from the end of `self`:
|
189
189
|
#
|
190
|
+
# 'bar'[-3] # => "b"
|
191
|
+
# 'bar'[-1] # => "r"
|
192
|
+
# 'bar'[-20] # => nil
|
190
193
|
#
|
194
|
+
# **`string[start, length]`**
|
191
195
|
#
|
192
|
-
#
|
196
|
+
# When non-negative integer arguments `start` and `length` are given, the slice
|
197
|
+
# begins at character offset `start`, if it exists, and continues for `length`
|
198
|
+
# characters, if available:
|
193
199
|
#
|
194
|
-
#
|
200
|
+
# 'foo'[0, 2] # => "fo"
|
201
|
+
# 'тест'[1, 2] # => "ес"
|
202
|
+
# 'こんにちは'[2, 2] # => "にち"
|
203
|
+
# # Zero length.
|
204
|
+
# 'foo'[2, 0] # => ""
|
205
|
+
# # Length not entirely available.
|
206
|
+
# 'foo'[1, 200] # => "oo"
|
207
|
+
# # Start out of range.
|
208
|
+
# 'foo'[4, 2] # => nil
|
195
209
|
#
|
196
|
-
#
|
197
|
-
# :
|
210
|
+
# Special case: if `start` equals the length of `self`, the slice is a new empty
|
211
|
+
# string:
|
198
212
|
#
|
199
|
-
# #
|
200
|
-
#
|
213
|
+
# 'foo'[3, 2] # => ""
|
214
|
+
# 'foo'[3, 200] # => ""
|
201
215
|
#
|
202
|
-
#
|
203
|
-
#
|
216
|
+
# When a negative `start` and non-negative `length` are given, the slice begins
|
217
|
+
# by counting backward from the end of `self`, and continues for `length`
|
218
|
+
# characters, if available:
|
204
219
|
#
|
205
|
-
# #
|
206
|
-
#
|
220
|
+
# 'foo'[-2, 2] # => "oo"
|
221
|
+
# 'foo'[-2, 200] # => "oo"
|
222
|
+
# # Start out of range.
|
223
|
+
# 'foo'[-4, 2] # => nil
|
207
224
|
#
|
225
|
+
# When a negative `length` is given, there is no slice:
|
208
226
|
#
|
227
|
+
# 'foo'[1, -1] # => nil
|
228
|
+
# 'foo'[-2, -1] # => nil
|
209
229
|
#
|
210
|
-
#
|
230
|
+
# **`string[range]`**
|
211
231
|
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
# or other object; returns `nil` if no match is found.
|
232
|
+
# When a Range argument `range` is given, it creates a substring of `string`
|
233
|
+
# using the indices in `range`. The slice is then determined as above:
|
215
234
|
#
|
216
|
-
# #
|
217
|
-
#
|
218
|
-
# returns `nil` if none found.
|
235
|
+
# 'foo'[0..1] # => "fo"
|
236
|
+
# 'foo'[0, 2] # => "fo"
|
219
237
|
#
|
220
|
-
# #
|
221
|
-
#
|
222
|
-
# returns `nil` if none found.
|
238
|
+
# 'foo'[2...2] # => ""
|
239
|
+
# 'foo'[2, 0] # => ""
|
223
240
|
#
|
224
|
-
# #
|
225
|
-
#
|
226
|
-
# otherwise.
|
241
|
+
# 'foo'[1..200] # => "oo"
|
242
|
+
# 'foo'[1, 200] # => "oo"
|
227
243
|
#
|
228
|
-
# #
|
229
|
-
#
|
230
|
-
# otherwise.
|
244
|
+
# 'foo'[4..5] # => nil
|
245
|
+
# 'foo'[4, 2] # => nil
|
231
246
|
#
|
232
|
-
# #
|
233
|
-
#
|
234
|
-
# otherwise.
|
247
|
+
# 'foo'[-4..-3] # => nil
|
248
|
+
# 'foo'[-4, 2] # => nil
|
235
249
|
#
|
236
|
-
# #
|
237
|
-
#
|
250
|
+
# 'foo'[3..4] # => ""
|
251
|
+
# 'foo'[3, 2] # => ""
|
238
252
|
#
|
239
|
-
# #
|
240
|
-
#
|
253
|
+
# 'foo'[-2..-1] # => "oo"
|
254
|
+
# 'foo'[-2, 2] # => "oo"
|
241
255
|
#
|
256
|
+
# 'foo'[-2..197] # => "oo"
|
257
|
+
# 'foo'[-2, 200] # => "oo"
|
242
258
|
#
|
259
|
+
# **`string[regexp, capture = 0]`**
|
243
260
|
#
|
244
|
-
#
|
261
|
+
# When the Regexp argument `regexp` is given, and the `capture` argument is `0`,
|
262
|
+
# the slice is the first matching substring found in `self`:
|
245
263
|
#
|
246
|
-
# #
|
247
|
-
#
|
248
|
-
#
|
264
|
+
# 'foo'[/o/] # => "o"
|
265
|
+
# 'foo'[/x/] # => nil
|
266
|
+
# s = 'hello there'
|
267
|
+
# s[/[aeiou](.)\1/] # => "ell"
|
268
|
+
# s[/[aeiou](.)\1/, 0] # => "ell"
|
249
269
|
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
270
|
+
# If the argument `capture` is provided and not `0`, it should be either a
|
271
|
+
# capture group index (integer) or a capture group name (String or Symbol); the
|
272
|
+
# slice is the specified capture (see Regexp@Groups and Captures):
|
253
273
|
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
#
|
274
|
+
# s = 'hello there'
|
275
|
+
# s[/[aeiou](.)\1/, 1] # => "l"
|
276
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
277
|
+
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
257
278
|
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
# otherwise.
|
279
|
+
# If an invalid capture group index is given, there is no slice. If an invalid
|
280
|
+
# capture group name is given, `IndexError` is raised.
|
261
281
|
#
|
282
|
+
# **`string[substring]`**
|
262
283
|
#
|
284
|
+
# When the single `String` argument `substring` is given, it returns the
|
285
|
+
# substring from `self` if found, otherwise `nil`:
|
263
286
|
#
|
264
|
-
#
|
265
|
-
#
|
266
|
-
# #sum
|
267
|
-
# : Returns a basic checksum for the string: the sum of each byte.
|
268
|
-
#
|
269
|
-
# #hash
|
270
|
-
# : Returns the integer hash code.
|
271
|
-
#
|
287
|
+
# 'foo'['oo'] # => "oo"
|
288
|
+
# 'foo'['xx'] # => nil
|
272
289
|
#
|
290
|
+
# ## What's Here
|
273
291
|
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
# [#==, #===](#method-i-3D-3D)
|
277
|
-
# : Returns `true` if a given other string has the same content as `self`.
|
292
|
+
# First, what's elsewhere. Class `String`:
|
278
293
|
#
|
279
|
-
#
|
280
|
-
#
|
294
|
+
# * Inherits from the [Object class](rdoc-ref:Object@What-27s+Here).
|
295
|
+
# * Includes the [Comparable module](rdoc-ref:Comparable@What-27s+Here).
|
281
296
|
#
|
282
|
-
#
|
283
|
-
# : Returns -1, 0, or 1 as a given other string is smaller than, equal to,
|
284
|
-
# or larger than `self`.
|
297
|
+
# Here, class `String` provides methods that are useful for:
|
285
298
|
#
|
286
|
-
#
|
287
|
-
#
|
288
|
-
#
|
299
|
+
# * [Creating a String](rdoc-ref:String@Methods+for+Creating+a+String)
|
300
|
+
# * [Frozen/Unfrozen
|
301
|
+
# Strings](rdoc-ref:String@Methods+for+a+Frozen-2FUnfrozen+String)
|
302
|
+
# * [Querying](rdoc-ref:String@Methods+for+Querying)
|
303
|
+
# * [Comparing](rdoc-ref:String@Methods+for+Comparing)
|
304
|
+
# * [Modifying a String](rdoc-ref:String@Methods+for+Modifying+a+String)
|
305
|
+
# * [Converting to New
|
306
|
+
# String](rdoc-ref:String@Methods+for+Converting+to+New+String)
|
307
|
+
# * [Converting to
|
308
|
+
# Non-String](rdoc-ref:String@Methods+for+Converting+to+Non-String)
|
309
|
+
# * [Iterating](rdoc-ref:String@Methods+for+Iterating)
|
289
310
|
#
|
290
|
-
#
|
291
|
-
# : Returns `true` if the string is equal to a given string after Unicode
|
292
|
-
# case folding; `false` otherwise.
|
311
|
+
# ### Methods for Creating a `String`
|
293
312
|
#
|
313
|
+
# * ::new: Returns a new string.
|
314
|
+
# * ::try_convert: Returns a new string created from a given object.
|
294
315
|
#
|
316
|
+
# ### Methods for a Frozen/Unfrozen String
|
295
317
|
#
|
296
|
-
#
|
318
|
+
# * #+@: Returns a string that is not frozen: `self` if not frozen; `self.dup`
|
319
|
+
# otherwise.
|
320
|
+
# * #-@ (aliased as #dedup): Returns a string that is frozen: `self` if
|
321
|
+
# already frozen; `self.freeze` otherwise.
|
322
|
+
# * #freeze: Freezes `self` if not already frozen; returns `self`.
|
297
323
|
#
|
298
|
-
#
|
324
|
+
# ### Methods for Querying
|
299
325
|
#
|
300
|
-
# *
|
326
|
+
# *Counts*
|
301
327
|
#
|
302
|
-
#
|
303
|
-
#
|
328
|
+
# * #length (aliased as #size): Returns the count of characters (not bytes).
|
329
|
+
# * #empty?: Returns `true` if `self.length` is zero; `false` otherwise.
|
330
|
+
# * #bytesize: Returns the count of bytes.
|
331
|
+
# * #count: Returns the count of substrings matching given strings.
|
304
332
|
#
|
305
|
-
#
|
306
|
-
# : Returns `self` concatenated with a given string or integer.
|
333
|
+
# *Substrings*
|
307
334
|
#
|
335
|
+
# * #=~: Returns the index of the first substring that matches a given Regexp
|
336
|
+
# or other object; returns `nil` if no match is found.
|
337
|
+
# * #index: Returns the index of the *first* occurrence of a given substring;
|
338
|
+
# returns `nil` if none found.
|
339
|
+
# * #rindex: Returns the index of the *last* occurrence of a given substring;
|
340
|
+
# returns `nil` if none found.
|
341
|
+
# * #include?: Returns `true` if the string contains a given substring;
|
342
|
+
# `false` otherwise.
|
343
|
+
# * #match: Returns a MatchData object if the string matches a given Regexp;
|
344
|
+
# `nil` otherwise.
|
345
|
+
# * #match?: Returns `true` if the string matches a given Regexp; `false`
|
346
|
+
# otherwise.
|
347
|
+
# * #start_with?: Returns `true` if the string begins with any of the given
|
348
|
+
# substrings.
|
349
|
+
# * #end_with?: Returns `true` if the string ends with any of the given
|
350
|
+
# substrings.
|
308
351
|
#
|
352
|
+
# *Encodings*
|
309
353
|
#
|
310
|
-
# *
|
354
|
+
# * #encoding: Returns the Encoding object that represents the encoding of the
|
355
|
+
# string.
|
356
|
+
# * #unicode_normalized?: Returns `true` if the string is in Unicode
|
357
|
+
# normalized form; `false` otherwise.
|
358
|
+
# * #valid_encoding?: Returns `true` if the string contains only characters
|
359
|
+
# that are valid for its encoding.
|
360
|
+
# * #ascii_only?: Returns `true` if the string has only ASCII characters;
|
361
|
+
# `false` otherwise.
|
311
362
|
#
|
312
|
-
#
|
313
|
-
# : Replaces the first substring that matches a given pattern with a given
|
314
|
-
# replacement string; returns `self` if any changes, `nil` otherwise.
|
363
|
+
# *Other*
|
315
364
|
#
|
316
|
-
#
|
317
|
-
# :
|
318
|
-
# replacement string; returns `self` if any changes, `nil` otherwise.
|
365
|
+
# * #sum: Returns a basic checksum for the string: the sum of each byte.
|
366
|
+
# * #hash: Returns the integer hash code.
|
319
367
|
#
|
320
|
-
#
|
321
|
-
# : Returns `self` modified to become its own successor.
|
368
|
+
# ### Methods for Comparing
|
322
369
|
#
|
323
|
-
#
|
324
|
-
#
|
370
|
+
# * #== (aliased as #===): Returns `true` if a given other string has the same
|
371
|
+
# content as `self`.
|
372
|
+
# * #eql?: Returns `true` if the content is the same as the given other
|
373
|
+
# string.
|
374
|
+
# * #<=>: Returns -1, 0, or 1 as a given other string is smaller than, equal
|
375
|
+
# to, or larger than `self`.
|
376
|
+
# * #casecmp: Ignoring case, returns -1, 0, or 1 as a given other string is
|
377
|
+
# smaller than, equal to, or larger than `self`.
|
378
|
+
# * #casecmp?: Returns `true` if the string is equal to a given string after
|
379
|
+
# Unicode case folding; `false` otherwise.
|
325
380
|
#
|
326
|
-
#
|
327
|
-
# : Returns `self` with its characters in reverse order.
|
381
|
+
# ### Methods for Modifying a `String`
|
328
382
|
#
|
329
|
-
#
|
330
|
-
# : Sets the byte at a given integer offset to a given value; returns the
|
331
|
-
# argument.
|
383
|
+
# Each of these methods modifies `self`.
|
332
384
|
#
|
333
|
-
#
|
334
|
-
# : Replaces specified characters in `self` with specified replacement
|
335
|
-
# characters; returns `self` if any changes, `nil` otherwise.
|
385
|
+
# *Insertion*
|
336
386
|
#
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
387
|
+
# * #insert: Returns `self` with a given string inserted at a specified
|
388
|
+
# offset.
|
389
|
+
# * #<<: Returns `self` concatenated with a given string or integer.
|
390
|
+
# * #append_as_bytes: Returns `self` concatenated with strings without
|
391
|
+
# performing any encoding validation or conversion.
|
341
392
|
#
|
393
|
+
# *Substitution*
|
342
394
|
#
|
395
|
+
# * #sub!: Replaces the first substring that matches a given pattern with a
|
396
|
+
# given replacement string; returns `self` if any changes, `nil` otherwise.
|
397
|
+
# * #gsub!: Replaces each substring that matches a given pattern with a given
|
398
|
+
# replacement string; returns `self` if any changes, `nil` otherwise.
|
399
|
+
# * #succ! (aliased as #next!): Returns `self` modified to become its own
|
400
|
+
# successor.
|
401
|
+
# * #initialize_copy (aliased as #replace): Returns `self` with its entire
|
402
|
+
# content replaced by a given string.
|
403
|
+
# * #reverse!: Returns `self` with its characters in reverse order.
|
404
|
+
# * #setbyte: Sets the byte at a given integer offset to a given value;
|
405
|
+
# returns the argument.
|
406
|
+
# * #tr!: Replaces specified characters in `self` with specified replacement
|
407
|
+
# characters; returns `self` if any changes, `nil` otherwise.
|
408
|
+
# * #tr_s!: Replaces specified characters in `self` with specified replacement
|
409
|
+
# characters, removing duplicates from the substrings that were modified;
|
410
|
+
# returns `self` if any changes, `nil` otherwise.
|
343
411
|
#
|
344
412
|
# *Casing*
|
345
413
|
#
|
346
|
-
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
#
|
353
|
-
#
|
354
|
-
# #upcase!
|
355
|
-
# : Upcases all characters; returns `self` if any changes, `nil`
|
356
|
-
# otherwise.
|
357
|
-
#
|
358
|
-
# #swapcase!
|
359
|
-
# : Upcases each downcase character and downcases each upcase character;
|
360
|
-
# returns `self` if any changes, `nil` otherwise.
|
361
|
-
#
|
362
|
-
#
|
414
|
+
# * #capitalize!: Upcases the initial character and downcases all others;
|
415
|
+
# returns `self` if any changes, `nil` otherwise.
|
416
|
+
# * #downcase!: Downcases all characters; returns `self` if any changes, `nil`
|
417
|
+
# otherwise.
|
418
|
+
# * #upcase!: Upcases all characters; returns `self` if any changes, `nil`
|
419
|
+
# otherwise.
|
420
|
+
# * #swapcase!: Upcases each downcase character and downcases each upcase
|
421
|
+
# character; returns `self` if any changes, `nil` otherwise.
|
363
422
|
#
|
364
423
|
# *Encoding*
|
365
424
|
#
|
366
|
-
#
|
367
|
-
#
|
368
|
-
#
|
369
|
-
#
|
370
|
-
#
|
371
|
-
# :
|
372
|
-
#
|
373
|
-
# #scrub!
|
374
|
-
# : Replaces each invalid byte with a given character; returns `self`.
|
375
|
-
#
|
376
|
-
# #force_encoding
|
377
|
-
# : Changes the encoding to a given encoding; returns `self`.
|
378
|
-
#
|
379
|
-
#
|
425
|
+
# * #encode!: Returns `self` with all characters transcoded from one encoding
|
426
|
+
# to another.
|
427
|
+
# * #unicode_normalize!: Unicode-normalizes `self`; returns `self`.
|
428
|
+
# * #scrub!: Replaces each invalid byte with a given character; returns
|
429
|
+
# `self`.
|
430
|
+
# * #force_encoding: Changes the encoding to a given encoding; returns `self`.
|
380
431
|
#
|
381
432
|
# *Deletion*
|
382
433
|
#
|
383
|
-
#
|
384
|
-
#
|
385
|
-
#
|
386
|
-
#
|
387
|
-
#
|
388
|
-
#
|
389
|
-
#
|
390
|
-
#
|
391
|
-
#
|
392
|
-
#
|
393
|
-
#
|
394
|
-
#
|
395
|
-
#
|
396
|
-
#
|
397
|
-
#
|
398
|
-
#
|
399
|
-
#
|
400
|
-
#
|
401
|
-
#
|
402
|
-
#
|
403
|
-
# otherwise.
|
404
|
-
#
|
405
|
-
# #strip!
|
406
|
-
# : Removes leading and trailing whitespace; returns `self` if any
|
407
|
-
# changes, `nil` otherwise.
|
408
|
-
#
|
409
|
-
# #chomp!
|
410
|
-
# : Removes trailing record separator, if found; returns `self` if any
|
411
|
-
# changes, `nil` otherwise.
|
412
|
-
#
|
413
|
-
# #chop!
|
414
|
-
# : Removes trailing whitespace if found, otherwise removes the last
|
415
|
-
# character; returns `self` if any changes, `nil` otherwise.
|
416
|
-
#
|
417
|
-
#
|
418
|
-
#
|
419
|
-
# ### Methods for Converting to New String
|
420
|
-
#
|
421
|
-
# Each of these methods returns a new String based on `self`, often just a
|
434
|
+
# * #clear: Removes all content, so that `self` is empty; returns `self`.
|
435
|
+
# * #slice!, #[]=: Removes a substring determined by a given index,
|
436
|
+
# start/length, range, regexp, or substring.
|
437
|
+
# * #squeeze!: Removes contiguous duplicate characters; returns `self`.
|
438
|
+
# * #delete!: Removes characters as determined by the intersection of
|
439
|
+
# substring arguments.
|
440
|
+
# * #lstrip!: Removes leading whitespace; returns `self` if any changes, `nil`
|
441
|
+
# otherwise.
|
442
|
+
# * #rstrip!: Removes trailing whitespace; returns `self` if any changes,
|
443
|
+
# `nil` otherwise.
|
444
|
+
# * #strip!: Removes leading and trailing whitespace; returns `self` if any
|
445
|
+
# changes, `nil` otherwise.
|
446
|
+
# * #chomp!: Removes the trailing record separator, if found; returns `self`
|
447
|
+
# if any changes, `nil` otherwise.
|
448
|
+
# * #chop!: Removes trailing newline characters if found; otherwise removes
|
449
|
+
# the last character; returns `self` if any changes, `nil` otherwise.
|
450
|
+
#
|
451
|
+
# ### Methods for Converting to New `String`
|
452
|
+
#
|
453
|
+
# Each of these methods returns a new `String` based on `self`, often just a
|
422
454
|
# modified copy of `self`.
|
423
455
|
#
|
424
456
|
# *Extension*
|
425
457
|
#
|
426
|
-
#
|
427
|
-
#
|
428
|
-
#
|
429
|
-
#
|
430
|
-
# :
|
431
|
-
#
|
432
|
-
#
|
433
|
-
# :
|
434
|
-
#
|
435
|
-
# #concat
|
436
|
-
# : Returns the concatenation of `self` with given other strings.
|
437
|
-
#
|
438
|
-
# #prepend
|
439
|
-
# : Returns the concatenation of a given other string with `self`.
|
440
|
-
#
|
441
|
-
# #ljust
|
442
|
-
# : Returns a copy of `self` of a given length, right-padded with a given
|
443
|
-
# other string.
|
444
|
-
#
|
445
|
-
# #rjust
|
446
|
-
# : Returns a copy of `self` of a given length, left-padded with a given
|
447
|
-
# other string.
|
448
|
-
#
|
449
|
-
#
|
458
|
+
# * #*: Returns the concatenation of multiple copies of `self`.
|
459
|
+
# * #+: Returns the concatenation of `self` and a given other string.
|
460
|
+
# * #center: Returns a copy of `self` centered between pad substrings.
|
461
|
+
# * #concat: Returns the concatenation of `self` with given other strings.
|
462
|
+
# * #prepend: Returns the concatenation of a given other string with `self`.
|
463
|
+
# * #ljust: Returns a copy of `self` of a given length, right-padded with a
|
464
|
+
# given other string.
|
465
|
+
# * #rjust: Returns a copy of `self` of a given length, left-padded with a
|
466
|
+
# given other string.
|
450
467
|
#
|
451
468
|
# *Encoding*
|
452
469
|
#
|
453
|
-
#
|
454
|
-
# :
|
455
|
-
#
|
456
|
-
#
|
457
|
-
#
|
458
|
-
#
|
459
|
-
#
|
460
|
-
# #unicode_normalize
|
461
|
-
# : Returns a copy of `self` with each character Unicode-normalized.
|
462
|
-
#
|
463
|
-
# #encode
|
464
|
-
# : Returns a copy of `self` with all characters transcoded from one given
|
465
|
-
# encoding into another.
|
466
|
-
#
|
467
|
-
#
|
470
|
+
# * #b: Returns a copy of `self` with ASCII-8BIT encoding.
|
471
|
+
# * #scrub: Returns a copy of `self` with each invalid byte replaced with a
|
472
|
+
# given character.
|
473
|
+
# * #unicode_normalize: Returns a copy of `self` with each character
|
474
|
+
# Unicode-normalized.
|
475
|
+
# * #encode: Returns a copy of `self` with all characters transcoded from one
|
476
|
+
# encoding to another.
|
468
477
|
#
|
469
478
|
# *Substitution*
|
470
479
|
#
|
471
|
-
#
|
472
|
-
#
|
473
|
-
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
#
|
479
|
-
#
|
480
|
-
#
|
481
|
-
#
|
482
|
-
#
|
483
|
-
#
|
484
|
-
# :
|
485
|
-
#
|
486
|
-
#
|
487
|
-
#
|
488
|
-
#
|
489
|
-
#
|
490
|
-
# #reverse
|
491
|
-
# : Returns a copy of `self` with its characters in reverse order.
|
492
|
-
#
|
493
|
-
# #tr
|
494
|
-
# : Returns a copy of `self` with specified characters replaced with
|
495
|
-
# specified replacement characters.
|
496
|
-
#
|
497
|
-
# #tr_s
|
498
|
-
# : Returns a copy of `self` with specified characters replaced with
|
499
|
-
# specified replacement characters, removing duplicates from the
|
500
|
-
# substrings that were modified.
|
501
|
-
#
|
502
|
-
# #%
|
503
|
-
# : Returns the string resulting from formatting a given object into
|
504
|
-
# `self`
|
505
|
-
#
|
506
|
-
#
|
480
|
+
# * #dump: Returns a copy of `self` with all non-printing characters replaced
|
481
|
+
# by xHH notation and all special characters escaped.
|
482
|
+
# * #undump: Returns a copy of `self` with all `\xNN` notations replaced by
|
483
|
+
# `\uNNNN` notations and all escaped characters unescaped.
|
484
|
+
# * #sub: Returns a copy of `self` with the first substring matching a given
|
485
|
+
# pattern replaced with a given replacement string.
|
486
|
+
# * #gsub: Returns a copy of `self` with each substring that matches a given
|
487
|
+
# pattern replaced with a given replacement string.
|
488
|
+
# * #succ (aliased as #next): Returns the string that is the successor to
|
489
|
+
# `self`.
|
490
|
+
# * #reverse: Returns a copy of `self` with its characters in reverse order.
|
491
|
+
# * #tr: Returns a copy of `self` with specified characters replaced with
|
492
|
+
# specified replacement characters.
|
493
|
+
# * #tr_s: Returns a copy of `self` with specified characters replaced with
|
494
|
+
# specified replacement characters, removing duplicates from the substrings
|
495
|
+
# that were modified.
|
496
|
+
# * #%: Returns the string resulting from formatting a given object into
|
497
|
+
# `self`.
|
507
498
|
#
|
508
499
|
# *Casing*
|
509
500
|
#
|
510
|
-
#
|
511
|
-
#
|
512
|
-
#
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
# #upcase
|
518
|
-
# : Returns a copy of `self` with all characters upcased.
|
519
|
-
#
|
520
|
-
# #swapcase
|
521
|
-
# : Returns a copy of `self` with all upcase characters downcased and all
|
522
|
-
# downcase characters upcased.
|
523
|
-
#
|
524
|
-
#
|
501
|
+
# * #capitalize: Returns a copy of `self` with the first character upcased and
|
502
|
+
# all other characters downcased.
|
503
|
+
# * #downcase: Returns a copy of `self` with all characters downcased.
|
504
|
+
# * #upcase: Returns a copy of `self` with all characters upcased.
|
505
|
+
# * #swapcase: Returns a copy of `self` with all upcase characters downcased
|
506
|
+
# and all downcase characters upcased.
|
525
507
|
#
|
526
508
|
# *Deletion*
|
527
509
|
#
|
528
|
-
#
|
529
|
-
# :
|
530
|
-
#
|
531
|
-
#
|
532
|
-
# :
|
533
|
-
#
|
534
|
-
#
|
535
|
-
# :
|
536
|
-
#
|
537
|
-
#
|
538
|
-
#
|
539
|
-
#
|
540
|
-
#
|
541
|
-
# :
|
542
|
-
#
|
543
|
-
#
|
544
|
-
#
|
545
|
-
#
|
546
|
-
# #chomp
|
547
|
-
# : Returns a copy of `self` with a trailing record separator removed, if
|
548
|
-
# found.
|
549
|
-
#
|
550
|
-
# #chop
|
551
|
-
# : Returns a copy of `self` with trailing whitespace or the last
|
552
|
-
# character removed.
|
553
|
-
#
|
554
|
-
# #squeeze
|
555
|
-
# : Returns a copy of `self` with contiguous duplicate characters removed.
|
556
|
-
#
|
557
|
-
# #[], #slice
|
558
|
-
# : Returns a substring determined by a given index, start/length, or
|
559
|
-
# range, or string.
|
560
|
-
#
|
561
|
-
# #byteslice
|
562
|
-
# : Returns a substring determined by a given index, start/length, or
|
563
|
-
# range.
|
564
|
-
#
|
565
|
-
# #chr
|
566
|
-
# : Returns the first character.
|
567
|
-
#
|
568
|
-
#
|
510
|
+
# * #delete: Returns a copy of `self` with characters removed.
|
511
|
+
# * #delete_prefix: Returns a copy of `self` with a given prefix removed.
|
512
|
+
# * #delete_suffix: Returns a copy of `self` with a given suffix removed.
|
513
|
+
# * #lstrip: Returns a copy of `self` with leading whitespace removed.
|
514
|
+
# * #rstrip: Returns a copy of `self` with trailing whitespace removed.
|
515
|
+
# * #strip: Returns a copy of `self` with leading and trailing whitespace
|
516
|
+
# removed.
|
517
|
+
# * #chomp: Returns a copy of `self` with a trailing record separator removed,
|
518
|
+
# if found.
|
519
|
+
# * #chop: Returns a copy of `self` with trailing newline characters or the
|
520
|
+
# last character removed.
|
521
|
+
# * #squeeze: Returns a copy of `self` with contiguous duplicate characters
|
522
|
+
# removed.
|
523
|
+
# * #[] (aliased as #slice): Returns a substring determined by a given index,
|
524
|
+
# start/length, range, regexp, or string.
|
525
|
+
# * #byteslice: Returns a substring determined by a given index, start/length,
|
526
|
+
# or range.
|
527
|
+
# * #chr: Returns the first character.
|
569
528
|
#
|
570
529
|
# *Duplication*
|
571
530
|
#
|
572
|
-
#
|
573
|
-
#
|
574
|
-
# String; otherwise, returns `self`.
|
575
|
-
#
|
531
|
+
# * #to_s (aliased as #to_str): If `self` is a subclass of `String`, returns
|
532
|
+
# `self` copied into a `String`; otherwise, returns `self`.
|
576
533
|
#
|
534
|
+
# ### Methods for Converting to Non-`String`
|
577
535
|
#
|
578
|
-
#
|
579
|
-
#
|
580
|
-
# Each of these methods converts the contents of `self` to a non-String.
|
536
|
+
# Each of these methods converts the contents of `self` to a non-`String`.
|
581
537
|
#
|
582
538
|
# *Characters, Bytes, and Clusters*
|
583
539
|
#
|
584
|
-
#
|
585
|
-
# :
|
586
|
-
#
|
587
|
-
#
|
588
|
-
# :
|
589
|
-
#
|
590
|
-
# #codepoints
|
591
|
-
# : Returns an array of the integer ordinals in `self`.
|
592
|
-
#
|
593
|
-
# #getbyte
|
594
|
-
# : Returns an integer byte as determined by a given index.
|
595
|
-
#
|
596
|
-
# #grapheme_clusters
|
597
|
-
# : Returns an array of the grapheme clusters in `self`.
|
598
|
-
#
|
599
|
-
#
|
540
|
+
# * #bytes: Returns an array of the bytes in `self`.
|
541
|
+
# * #chars: Returns an array of the characters in `self`.
|
542
|
+
# * #codepoints: Returns an array of the integer ordinals in `self`.
|
543
|
+
# * #getbyte: Returns the integer byte at the given index in `self`.
|
544
|
+
# * #grapheme_clusters: Returns an array of the grapheme clusters in `self`.
|
600
545
|
#
|
601
546
|
# *Splitting*
|
602
547
|
#
|
603
|
-
#
|
604
|
-
#
|
605
|
-
#
|
606
|
-
#
|
607
|
-
#
|
608
|
-
#
|
609
|
-
#
|
610
|
-
#
|
611
|
-
#
|
612
|
-
# : Returns a 3-element array determined by the last substring that
|
613
|
-
# matches a given substring or regexp,
|
614
|
-
#
|
615
|
-
# #split
|
616
|
-
# : Returns an array of substrings determined by a given delimiter --
|
617
|
-
# regexp or string -- or, if a block given, passes those substrings to
|
618
|
-
# the block.
|
619
|
-
#
|
620
|
-
#
|
548
|
+
# * #lines: Returns an array of the lines in `self`, as determined by a given
|
549
|
+
# record separator.
|
550
|
+
# * #partition: Returns a 3-element array determined by the first substring
|
551
|
+
# that matches a given substring or regexp.
|
552
|
+
# * #rpartition: Returns a 3-element array determined by the last substring
|
553
|
+
# that matches a given substring or regexp.
|
554
|
+
# * #split: Returns an array of substrings determined by a given delimiter --
|
555
|
+
# regexp or string -- or, if a block is given, passes those substrings to
|
556
|
+
# the block.
|
621
557
|
#
|
622
558
|
# *Matching*
|
623
559
|
#
|
624
|
-
#
|
625
|
-
#
|
626
|
-
#
|
627
|
-
#
|
628
|
-
#
|
629
|
-
#
|
630
|
-
# given format.
|
631
|
-
#
|
632
|
-
# #unpack1
|
633
|
-
# : Returns the first substring extracted from `self` according to a given
|
634
|
-
# format.
|
635
|
-
#
|
636
|
-
#
|
560
|
+
# * #scan: Returns an array of substrings matching a given regexp or string,
|
561
|
+
# or, if a block is given, passes each matching substring to the block.
|
562
|
+
# * #unpack: Returns an array of substrings extracted from `self` according to
|
563
|
+
# a given format.
|
564
|
+
# * #unpack1: Returns the first substring extracted from `self` according to a
|
565
|
+
# given format.
|
637
566
|
#
|
638
567
|
# *Numerics*
|
639
568
|
#
|
640
|
-
#
|
641
|
-
#
|
642
|
-
#
|
643
|
-
#
|
644
|
-
#
|
645
|
-
# :
|
646
|
-
#
|
647
|
-
#
|
648
|
-
#
|
649
|
-
# : Returns the integer ordinal of the first character in `self`.
|
650
|
-
#
|
651
|
-
# #to_i
|
652
|
-
# : Returns the integer value of leading characters, interpreted as an
|
653
|
-
# integer.
|
654
|
-
#
|
655
|
-
# #to_f
|
656
|
-
# : Returns the floating-point value of leading characters, interpreted as
|
657
|
-
# a floating-point number.
|
658
|
-
#
|
659
|
-
#
|
569
|
+
# * #hex: Returns the integer value of the leading characters, interpreted as
|
570
|
+
# hexadecimal digits.
|
571
|
+
# * #oct: Returns the integer value of the leading characters, interpreted as
|
572
|
+
# octal digits.
|
573
|
+
# * #ord: Returns the integer ordinal of the first character in `self`.
|
574
|
+
# * #to_i: Returns the integer value of leading characters, interpreted as an
|
575
|
+
# integer.
|
576
|
+
# * #to_f: Returns the floating-point value of leading characters, interpreted
|
577
|
+
# as a floating-point number.
|
660
578
|
#
|
661
579
|
# *Strings and Symbols*
|
662
580
|
#
|
663
|
-
#
|
664
|
-
#
|
665
|
-
#
|
666
|
-
#
|
667
|
-
# #to_sym, #intern
|
668
|
-
# : Returns the symbol corresponding to `self`.
|
669
|
-
#
|
670
|
-
#
|
581
|
+
# * #inspect: Returns a copy of `self`, enclosed in double quotes, with
|
582
|
+
# special characters escaped.
|
583
|
+
# * #intern (aliased as #to_sym): Returns the symbol corresponding to `self`.
|
671
584
|
#
|
672
585
|
# ### Methods for Iterating
|
673
586
|
#
|
674
|
-
#
|
675
|
-
# :
|
676
|
-
#
|
677
|
-
#
|
678
|
-
#
|
679
|
-
#
|
680
|
-
#
|
681
|
-
# :
|
682
|
-
#
|
683
|
-
#
|
684
|
-
# #
|
685
|
-
# : Calls the given block with each successive grapheme cluster in `self`.
|
686
|
-
#
|
687
|
-
# #each_line
|
688
|
-
# : Calls the given block with each successive line in `self`, as
|
689
|
-
# determined by a given record separator.
|
690
|
-
#
|
691
|
-
# #upto
|
692
|
-
# : Calls the given block with each string value returned by successive
|
693
|
-
# calls to #succ.
|
587
|
+
# * #each_byte: Calls the given block with each successive byte in `self`.
|
588
|
+
# * #each_char: Calls the given block with each successive character in
|
589
|
+
# `self`.
|
590
|
+
# * #each_codepoint: Calls the given block with each successive integer
|
591
|
+
# codepoint in `self`.
|
592
|
+
# * #each_grapheme_cluster: Calls the given block with each successive
|
593
|
+
# grapheme cluster in `self`.
|
594
|
+
# * #each_line: Calls the given block with each successive line in `self`, as
|
595
|
+
# determined by a given record separator.
|
596
|
+
# * #upto: Calls the given block with each string value returned by successive
|
597
|
+
# calls to #succ.
|
694
598
|
#
|
695
599
|
class String
|
696
600
|
include Comparable
|
697
601
|
|
602
|
+
# A `selector` is a special type of string, used within methods like `String#tr`.
|
603
|
+
type selector = string
|
604
|
+
|
698
605
|
# <!--
|
699
606
|
# rdoc-file=string.c
|
700
607
|
# - String.try_convert(object) -> object, new_string, or nil
|
701
608
|
# -->
|
702
|
-
# If `object` is a String object, returns `object`.
|
609
|
+
# If `object` is a `String` object, returns `object`.
|
703
610
|
#
|
704
611
|
# Otherwise if `object` responds to `:to_str`, calls `object.to_str` and returns
|
705
612
|
# the result.
|
706
613
|
#
|
707
614
|
# Returns `nil` if `object` does not respond to `:to_str`.
|
708
615
|
#
|
709
|
-
# Raises an exception unless `object.to_str` returns a String object.
|
616
|
+
# Raises an exception unless `object.to_str` returns a `String` object.
|
710
617
|
#
|
711
|
-
def self.try_convert: (
|
618
|
+
def self.try_convert: (String object) -> String # technically will return `object` unchanged.
|
619
|
+
| (_ToStr object) -> String
|
620
|
+
| (untyped object) -> String?
|
712
621
|
|
713
|
-
|
622
|
+
# <!--
|
623
|
+
# rdoc-file=string.c
|
624
|
+
# - String.new(string = '', **opts) -> new_string
|
625
|
+
# -->
|
626
|
+
# Returns a new String that is a copy of `string`.
|
627
|
+
#
|
628
|
+
# With no arguments, returns the empty string with the Encoding `ASCII-8BIT`:
|
629
|
+
#
|
630
|
+
# s = String.new
|
631
|
+
# s # => ""
|
632
|
+
# s.encoding # => #<Encoding:ASCII-8BIT>
|
633
|
+
#
|
634
|
+
# With optional argument `string` and no keyword arguments, returns a copy of
|
635
|
+
# `string` with the same encoding:
|
636
|
+
#
|
637
|
+
# String.new('foo') # => "foo"
|
638
|
+
# String.new('тест') # => "тест"
|
639
|
+
# String.new('こんにちは') # => "こんにちは"
|
640
|
+
#
|
641
|
+
# (Unlike String.new, a [string
|
642
|
+
# literal](rdoc-ref:syntax/literals.rdoc@String+Literals) like `''` or a [here
|
643
|
+
# document literal](rdoc-ref:syntax/literals.rdoc@Here+Document+Literals) always
|
644
|
+
# has [script encoding](rdoc-ref:encodings.rdoc@Script+Encoding).)
|
645
|
+
#
|
646
|
+
# With optional keyword argument `encoding`, returns a copy of `string` with the
|
647
|
+
# specified encoding; the `encoding` may be an Encoding object, an encoding
|
648
|
+
# name, or an encoding name alias:
|
649
|
+
#
|
650
|
+
# String.new('foo', encoding: Encoding::US_ASCII).encoding # => #<Encoding:US-ASCII>
|
651
|
+
# String.new('foo', encoding: 'US-ASCII').encoding # => #<Encoding:US-ASCII>
|
652
|
+
# String.new('foo', encoding: 'ASCII').encoding # => #<Encoding:US-ASCII>
|
653
|
+
#
|
654
|
+
# The given encoding need not be valid for the string's content, and that
|
655
|
+
# validity is not checked:
|
656
|
+
#
|
657
|
+
# s = String.new('こんにちは', encoding: 'ascii')
|
658
|
+
# s.valid_encoding? # => false
|
659
|
+
#
|
660
|
+
# But the given `encoding` itself is checked:
|
661
|
+
#
|
662
|
+
# String.new('foo', encoding: 'bar') # Raises ArgumentError.
|
663
|
+
#
|
664
|
+
# With optional keyword argument `capacity`, returns a copy of `string` (or an
|
665
|
+
# empty string, if `string` is not given); the given `capacity` is advisory
|
666
|
+
# only, and may or may not set the size of the internal buffer, which may in
|
667
|
+
# turn affect performance:
|
668
|
+
#
|
669
|
+
# String.new(capacity: 1)
|
670
|
+
# String.new('foo', capacity: 4096)
|
671
|
+
#
|
672
|
+
# Note that Ruby strings are null-terminated internally, so the internal buffer
|
673
|
+
# size will be one or more bytes larger than the requested capacity depending on
|
674
|
+
# the encoding.
|
675
|
+
#
|
676
|
+
# The `string`, `encoding`, and `capacity` arguments may all be used together:
|
677
|
+
#
|
678
|
+
# String.new('hello', encoding: 'UTF-8', capacity: 25)
|
679
|
+
#
|
680
|
+
def initialize: (?string source, ?encoding: encoding, ?capacity: int) -> void
|
681
|
+
|
682
|
+
# <!--
|
683
|
+
# rdoc-file=string.c
|
684
|
+
# - replace(other_string) -> self
|
685
|
+
# -->
|
686
|
+
# Replaces the contents of `self` with the contents of `other_string`:
|
687
|
+
#
|
688
|
+
# s = 'foo' # => "foo"
|
689
|
+
# s.replace('bar') # => "bar"
|
690
|
+
#
|
691
|
+
alias initialize_copy replace
|
714
692
|
|
715
693
|
# <!--
|
716
694
|
# rdoc-file=string.c
|
@@ -728,51 +706,62 @@ class String
|
|
728
706
|
# "foo = %{foo}" % {foo: 'bar'} # => "foo = bar"
|
729
707
|
# "foo = %{foo}, baz = %{baz}" % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"
|
730
708
|
#
|
731
|
-
def %: (
|
732
|
-
| (
|
709
|
+
def %: (array[untyped] positional_args) -> String
|
710
|
+
| (hash[Symbol, untyped] named_args) -> String
|
733
711
|
| (untyped arg) -> String
|
734
712
|
|
735
713
|
# <!--
|
736
714
|
# rdoc-file=string.c
|
737
715
|
# - string * integer -> new_string
|
738
716
|
# -->
|
739
|
-
# Returns a new String containing `integer` copies of `self`:
|
717
|
+
# Returns a new `String` containing `integer` copies of `self`:
|
740
718
|
#
|
741
719
|
# "Ho! " * 3 # => "Ho! Ho! Ho! "
|
742
720
|
# "Ho! " * 0 # => ""
|
743
721
|
#
|
744
|
-
def *: (int
|
722
|
+
def *: (int amount) -> String
|
745
723
|
|
746
724
|
# <!--
|
747
725
|
# rdoc-file=string.c
|
748
726
|
# - string + other_string -> new_string
|
749
727
|
# -->
|
750
|
-
# Returns a new String containing `other_string` concatenated to `self`:
|
728
|
+
# Returns a new `String` containing `other_string` concatenated to `self`:
|
751
729
|
#
|
752
730
|
# "Hello from " + self.to_s # => "Hello from main"
|
753
731
|
#
|
754
|
-
def +: (string
|
732
|
+
def +: (string other_string) -> String
|
755
733
|
|
756
734
|
# <!--
|
757
735
|
# rdoc-file=string.c
|
758
736
|
# - +string -> new_string or self
|
759
737
|
# -->
|
760
|
-
# Returns `self` if `self` is not frozen
|
738
|
+
# Returns `self` if `self` is not frozen and can be mutated without warning
|
739
|
+
# issuance.
|
761
740
|
#
|
762
|
-
# Otherwise
|
741
|
+
# Otherwise returns `self.dup`, which is not frozen.
|
763
742
|
#
|
764
|
-
def +@: () ->
|
743
|
+
def +@: () -> self
|
765
744
|
|
766
745
|
# <!--
|
767
746
|
# rdoc-file=string.c
|
768
747
|
# - -string -> frozen_string
|
748
|
+
# - dedup -> frozen_string
|
769
749
|
# -->
|
770
750
|
# Returns a frozen, possibly pre-existing copy of the string.
|
771
751
|
#
|
772
|
-
# The returned String will be deduplicated as long as it does not have any
|
773
|
-
# instance variables set on it.
|
752
|
+
# The returned `String` will be deduplicated as long as it does not have any
|
753
|
+
# instance variables set on it and is not a String subclass.
|
754
|
+
#
|
755
|
+
# Note that `-string` variant is more convenient for defining constants:
|
756
|
+
#
|
757
|
+
# FILENAME = -'config/database.yml'
|
758
|
+
#
|
759
|
+
# while `dedup` is better suitable for using the method in chains of
|
760
|
+
# calculations:
|
774
761
|
#
|
775
|
-
|
762
|
+
# @url_list.concat(urls.map(&:dedup))
|
763
|
+
#
|
764
|
+
def -@: () -> self
|
776
765
|
|
777
766
|
# <!--
|
778
767
|
# rdoc-file=string.c
|
@@ -790,9 +779,25 @@ class String
|
|
790
779
|
# s = 'foo'
|
791
780
|
# s << 33 # => "foo!"
|
792
781
|
#
|
782
|
+
# If that codepoint is not representable in the encoding of *string*, RangeError
|
783
|
+
# is raised.
|
784
|
+
#
|
785
|
+
# s = 'foo'
|
786
|
+
# s.encoding # => <Encoding:UTF-8>
|
787
|
+
# s << 0x00110000 # 1114112 out of char range (RangeError)
|
788
|
+
# s = 'foo'.encode('EUC-JP')
|
789
|
+
# s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
|
790
|
+
#
|
791
|
+
# If the encoding is US-ASCII and the codepoint is 0..0xff, *string* is
|
792
|
+
# automatically promoted to ASCII-8BIT.
|
793
|
+
#
|
794
|
+
# s = 'foo'.encode('US-ASCII')
|
795
|
+
# s << 0xff
|
796
|
+
# s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
|
797
|
+
#
|
793
798
|
# Related: String#concat, which takes multiple arguments.
|
794
799
|
#
|
795
|
-
def <<: (string | Integer str_or_codepoint) ->
|
800
|
+
def <<: (string | Integer str_or_codepoint) -> self
|
796
801
|
|
797
802
|
# <!--
|
798
803
|
# rdoc-file=string.c
|
@@ -805,7 +810,6 @@ class String
|
|
805
810
|
# * 1 if `other_string` is smaller.
|
806
811
|
# * `nil` if the two are incomparable.
|
807
812
|
#
|
808
|
-
#
|
809
813
|
# Examples:
|
810
814
|
#
|
811
815
|
# 'foo' <=> 'foo' # => 0
|
@@ -815,8 +819,8 @@ class String
|
|
815
819
|
# 'foo' <=> 'FOO' # => 1
|
816
820
|
# 'foo' <=> 1 # => nil
|
817
821
|
#
|
818
|
-
def <=>: (string
|
819
|
-
| (untyped
|
822
|
+
def <=>: (string) -> (-1 | 0 | 1)
|
823
|
+
| (untyped) -> (-1 | 0 | 1)?
|
820
824
|
|
821
825
|
# <!--
|
822
826
|
# rdoc-file=string.c
|
@@ -834,10 +838,10 @@ class String
|
|
834
838
|
# Returns `false` if the two strings' encodings are not compatible:
|
835
839
|
# "\u{e4 f6 fc}".encode("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
|
836
840
|
#
|
837
|
-
# If `object` is not an instance of String but responds to `to_str`, then the
|
841
|
+
# If `object` is not an instance of `String` but responds to `to_str`, then the
|
838
842
|
# two strings are compared using `object.==`.
|
839
843
|
#
|
840
|
-
def ==: (untyped
|
844
|
+
def ==: (untyped other) -> bool
|
841
845
|
|
842
846
|
# <!-- rdoc-file=string.c -->
|
843
847
|
# Returns `true` if `object` has the same length and content; as `self`; `false`
|
@@ -851,10 +855,10 @@ class String
|
|
851
855
|
# Returns `false` if the two strings' encodings are not compatible:
|
852
856
|
# "\u{e4 f6 fc}".encode("ISO-8859-1") == ("\u{c4 d6 dc}") # => false
|
853
857
|
#
|
854
|
-
# If `object` is not an instance of String but responds to `to_str`, then the
|
858
|
+
# If `object` is not an instance of `String` but responds to `to_str`, then the
|
855
859
|
# two strings are compared using `object.==`.
|
856
860
|
#
|
857
|
-
|
861
|
+
alias === ==
|
858
862
|
|
859
863
|
# <!--
|
860
864
|
# rdoc-file=string.c
|
@@ -868,14 +872,13 @@ class String
|
|
868
872
|
# 'foo' =~ /o/ # => 1
|
869
873
|
# 'foo' =~ /x/ # => nil
|
870
874
|
#
|
871
|
-
# Note: also updates
|
872
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
875
|
+
# Note: also updates Regexp@Global+Variables.
|
873
876
|
#
|
874
877
|
# If the given `object` is not a Regexp, returns the value returned by `object
|
875
878
|
# =~ self`.
|
876
879
|
#
|
877
880
|
# Note that `string =~ regexp` is different from `regexp =~ string` (see
|
878
|
-
#
|
881
|
+
# Regexp#=~):
|
879
882
|
#
|
880
883
|
# number= nil
|
881
884
|
# "no. 9" =~ /(?<number>\d+)/
|
@@ -883,7 +886,12 @@ class String
|
|
883
886
|
# /(?<number>\d+)/ =~ "no. 9"
|
884
887
|
# number #=> "9"
|
885
888
|
#
|
886
|
-
def =~: (
|
889
|
+
def =~: (Regexp regex) -> Integer?
|
890
|
+
| [T] (_MatchAgainst[self, T] object) -> T
|
891
|
+
|
892
|
+
interface _MatchAgainst[O, T]
|
893
|
+
def =~: (O string) -> T
|
894
|
+
end
|
887
895
|
|
888
896
|
# <!--
|
889
897
|
# rdoc-file=string.c
|
@@ -893,166 +901,233 @@ class String
|
|
893
901
|
# - string[regexp, capture = 0] -> new_string or nil
|
894
902
|
# - string[substring] -> new_string or nil
|
895
903
|
# -->
|
896
|
-
# Returns the substring of `self` specified by the arguments.
|
904
|
+
# Returns the substring of `self` specified by the arguments. See examples at
|
905
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
906
|
+
#
|
907
|
+
def []: (int start, ?int length) -> String?
|
908
|
+
| (range[int?] range) -> String?
|
909
|
+
| (Regexp regexp, ?MatchData::capture backref) -> String?
|
910
|
+
| (String substring) -> String?
|
911
|
+
|
912
|
+
# <!--
|
913
|
+
# rdoc-file=string.c
|
914
|
+
# - string[index] = new_string
|
915
|
+
# - string[start, length] = new_string
|
916
|
+
# - string[range] = new_string
|
917
|
+
# - string[regexp, capture = 0] = new_string
|
918
|
+
# - string[substring] = new_string
|
919
|
+
# -->
|
920
|
+
# Replaces all, some, or none of the contents of `self`; returns `new_string`.
|
921
|
+
# See [String Slices](rdoc-ref:String@String+Slices).
|
897
922
|
#
|
898
|
-
#
|
899
|
-
# substring found in `self` at offset `index`:
|
923
|
+
# A few examples:
|
900
924
|
#
|
901
|
-
# '
|
925
|
+
# s = 'foo'
|
926
|
+
# s[2] = 'rtune' # => "rtune"
|
927
|
+
# s # => "fortune"
|
928
|
+
# s[1, 5] = 'init' # => "init"
|
929
|
+
# s # => "finite"
|
930
|
+
# s[3..4] = 'al' # => "al"
|
931
|
+
# s # => "finale"
|
932
|
+
# s[/e$/] = 'ly' # => "ly"
|
933
|
+
# s # => "finally"
|
934
|
+
# s['lly'] = 'ncial' # => "ncial"
|
935
|
+
# s # => "financial"
|
936
|
+
#
|
937
|
+
def []=: [T < _ToStr] (int index, T replacement) -> T
|
938
|
+
| [T < _ToStr] (int start, int length, T replacement) -> T
|
939
|
+
| [T < _ToStr] (range[int?] range, T replacement) -> T
|
940
|
+
| [T < _ToStr] (Regexp regexp, T replacement) -> T
|
941
|
+
| [T < _ToStr] (Regexp regexp, MatchData::capture backref, T replacement) -> T
|
942
|
+
| [T < _ToStr] (String substring, T replacement) -> T
|
943
|
+
|
944
|
+
# <!--
|
945
|
+
# rdoc-file=string.c
|
946
|
+
# - append_as_bytes(*objects) -> string
|
947
|
+
# -->
|
948
|
+
# Concatenates each object in `objects` into `self` without any encoding
|
949
|
+
# validation or conversion and returns `self`:
|
950
|
+
#
|
951
|
+
# s = 'foo'
|
952
|
+
# s.append_as_bytes(" \xE2\x82") # => "foo \xE2\x82"
|
953
|
+
# s.valid_encoding? # => false
|
954
|
+
# s.append_as_bytes("\xAC 12")
|
955
|
+
# s.valid_encoding? # => true
|
902
956
|
#
|
903
|
-
#
|
957
|
+
# For each given object `object` that is an Integer, the value is considered a
|
958
|
+
# Byte. If the Integer is bigger than one byte, only the lower byte is
|
959
|
+
# considered, similar to String#setbyte:
|
904
960
|
#
|
905
|
-
#
|
961
|
+
# s = ""
|
962
|
+
# s.append_as_bytes(0, 257) # => "\u0000\u0001"
|
906
963
|
#
|
907
|
-
#
|
964
|
+
# Related: String#<<, String#concat, which do an encoding aware concatenation.
|
908
965
|
#
|
909
|
-
|
910
|
-
|
966
|
+
def append_as_bytes: (String) -> String
|
967
|
+
|
968
|
+
# <!--
|
969
|
+
# rdoc-file=string.c
|
970
|
+
# - ascii_only? -> true or false
|
971
|
+
# -->
|
972
|
+
# Returns `true` if `self` contains only ASCII characters, `false` otherwise:
|
911
973
|
#
|
912
|
-
#
|
913
|
-
#
|
974
|
+
# 'abc'.ascii_only? # => true
|
975
|
+
# "abc\u{6666}".ascii_only? # => false
|
914
976
|
#
|
915
|
-
|
916
|
-
|
977
|
+
def ascii_only?: () -> bool
|
978
|
+
|
979
|
+
# <!--
|
980
|
+
# rdoc-file=string.c
|
981
|
+
# - b -> string
|
982
|
+
# -->
|
983
|
+
# Returns a copy of `self` that has ASCII-8BIT encoding; the underlying bytes
|
984
|
+
# are not modified:
|
917
985
|
#
|
918
|
-
#
|
986
|
+
# s = "\x99"
|
987
|
+
# s.encoding # => #<Encoding:UTF-8>
|
988
|
+
# t = s.b # => "\x99"
|
989
|
+
# t.encoding # => #<Encoding:ASCII-8BIT>
|
919
990
|
#
|
920
|
-
#
|
991
|
+
# s = "\u4095" # => "䂕"
|
992
|
+
# s.encoding # => #<Encoding:UTF-8>
|
993
|
+
# s.bytes # => [228, 130, 149]
|
994
|
+
# t = s.b # => "\xE4\x82\x95"
|
995
|
+
# t.encoding # => #<Encoding:ASCII-8BIT>
|
996
|
+
# t.bytes # => [228, 130, 149]
|
921
997
|
#
|
922
|
-
|
923
|
-
|
998
|
+
def b: () -> String
|
999
|
+
|
1000
|
+
# <!--
|
1001
|
+
# rdoc-file=string.c
|
1002
|
+
# - byteindex(substring, offset = 0) -> integer or nil
|
1003
|
+
# - byteindex(regexp, offset = 0) -> integer or nil
|
1004
|
+
# -->
|
1005
|
+
# Returns the Integer byte-based index of the first occurrence of the given
|
1006
|
+
# `substring`, or `nil` if none found:
|
924
1007
|
#
|
925
|
-
# 'foo'
|
1008
|
+
# 'foo'.byteindex('f') # => 0
|
1009
|
+
# 'foo'.byteindex('o') # => 1
|
1010
|
+
# 'foo'.byteindex('oo') # => 1
|
1011
|
+
# 'foo'.byteindex('ooo') # => nil
|
926
1012
|
#
|
927
|
-
# Returns
|
1013
|
+
# Returns the Integer byte-based index of the first match for the given Regexp
|
1014
|
+
# `regexp`, or `nil` if none found:
|
928
1015
|
#
|
929
|
-
# 'foo'
|
930
|
-
# 'foo'
|
1016
|
+
# 'foo'.byteindex(/f/) # => 0
|
1017
|
+
# 'foo'.byteindex(/o/) # => 1
|
1018
|
+
# 'foo'.byteindex(/oo/) # => 1
|
1019
|
+
# 'foo'.byteindex(/ooo/) # => nil
|
931
1020
|
#
|
932
|
-
#
|
1021
|
+
# Integer argument `offset`, if given, specifies the byte-based position in the
|
1022
|
+
# string to begin the search:
|
933
1023
|
#
|
934
|
-
# 'foo'
|
1024
|
+
# 'foo'.byteindex('o', 1) # => 1
|
1025
|
+
# 'foo'.byteindex('o', 2) # => 2
|
1026
|
+
# 'foo'.byteindex('o', 3) # => nil
|
935
1027
|
#
|
936
|
-
#
|
1028
|
+
# If `offset` is negative, counts backward from the end of `self`:
|
937
1029
|
#
|
938
|
-
# 'foo'
|
1030
|
+
# 'foo'.byteindex('o', -1) # => 2
|
1031
|
+
# 'foo'.byteindex('o', -2) # => 1
|
1032
|
+
# 'foo'.byteindex('o', -3) # => 1
|
1033
|
+
# 'foo'.byteindex('o', -4) # => nil
|
939
1034
|
#
|
940
|
-
#
|
941
|
-
#
|
1035
|
+
# If `offset` does not land on character (codepoint) boundary, `IndexError` is
|
1036
|
+
# raised.
|
942
1037
|
#
|
943
|
-
#
|
944
|
-
# * `'foo'[0...1]` is equivalent to `'foo'[0, 1]`.
|
1038
|
+
# Related: String#index, String#byterindex.
|
945
1039
|
#
|
1040
|
+
def byteindex: (Regexp | string pattern, ?int offset) -> Integer?
|
1041
|
+
|
1042
|
+
# <!--
|
1043
|
+
# rdoc-file=string.c
|
1044
|
+
# - byterindex(substring, offset = self.bytesize) -> integer or nil
|
1045
|
+
# - byterindex(regexp, offset = self.bytesize) -> integer or nil
|
1046
|
+
# -->
|
1047
|
+
# Returns the Integer byte-based index of the *last* occurrence of the given
|
1048
|
+
# `substring`, or `nil` if none found:
|
946
1049
|
#
|
947
|
-
#
|
948
|
-
#
|
1050
|
+
# 'foo'.byterindex('f') # => 0
|
1051
|
+
# 'foo'.byterindex('o') # => 2
|
1052
|
+
# 'foo'.byterindex('oo') # => 1
|
1053
|
+
# 'foo'.byterindex('ooo') # => nil
|
949
1054
|
#
|
950
|
-
#
|
951
|
-
#
|
952
|
-
# s = 'hello there'
|
953
|
-
# s[/[aeiou](.)\1/] # => "ell"
|
954
|
-
# s[/[aeiou](.)\1/, 0] # => "ell"
|
1055
|
+
# Returns the Integer byte-based index of the *last* match for the given Regexp
|
1056
|
+
# `regexp`, or `nil` if none found:
|
955
1057
|
#
|
956
|
-
#
|
957
|
-
#
|
958
|
-
#
|
959
|
-
#
|
1058
|
+
# 'foo'.byterindex(/f/) # => 0
|
1059
|
+
# 'foo'.byterindex(/o/) # => 2
|
1060
|
+
# 'foo'.byterindex(/oo/) # => 1
|
1061
|
+
# 'foo'.byterindex(/ooo/) # => nil
|
960
1062
|
#
|
961
|
-
#
|
962
|
-
#
|
963
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
964
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
1063
|
+
# The *last* match means starting at the possible last position, not the last of
|
1064
|
+
# longest matches.
|
965
1065
|
#
|
966
|
-
#
|
967
|
-
#
|
1066
|
+
# 'foo'.byterindex(/o+/) # => 2
|
1067
|
+
# $~ #=> #<MatchData "o">
|
968
1068
|
#
|
969
|
-
#
|
970
|
-
# from `self` if found, otherwise `nil`:
|
1069
|
+
# To get the last longest match, needs to combine with negative lookbehind.
|
971
1070
|
#
|
972
|
-
# 'foo'
|
973
|
-
#
|
1071
|
+
# 'foo'.byterindex(/(?<!o)o+/) # => 1
|
1072
|
+
# $~ #=> #<MatchData "oo">
|
974
1073
|
#
|
975
|
-
# String#
|
1074
|
+
# Or String#byteindex with negative lookforward.
|
976
1075
|
#
|
977
|
-
|
978
|
-
|
979
|
-
| (Range[Integer] | Range[Integer?] range) -> String?
|
980
|
-
| (Regexp regexp) -> String?
|
981
|
-
| (Regexp regexp, int | String capture) -> String?
|
982
|
-
| (String match_str) -> String?
|
983
|
-
|
984
|
-
# <!--
|
985
|
-
# rdoc-file=string.c
|
986
|
-
# - str[integer] = new_str
|
987
|
-
# - str[integer, integer] = new_str
|
988
|
-
# - str[range] = aString
|
989
|
-
# - str[regexp] = new_str
|
990
|
-
# - str[regexp, integer] = new_str
|
991
|
-
# - str[regexp, name] = new_str
|
992
|
-
# - str[other_str] = new_str
|
993
|
-
# -->
|
994
|
-
# Element Assignment---Replaces some or all of the content of *str*. The portion
|
995
|
-
# of the string affected is determined using the same criteria as String#[]. If
|
996
|
-
# the replacement string is not the same length as the text it is replacing, the
|
997
|
-
# string will be adjusted accordingly. If the regular expression or string is
|
998
|
-
# used as the index doesn't match a position in the string, IndexError is
|
999
|
-
# raised. If the regular expression form is used, the optional second Integer
|
1000
|
-
# allows you to specify which portion of the match to replace (effectively using
|
1001
|
-
# the MatchData indexing rules. The forms that take an Integer will raise an
|
1002
|
-
# IndexError if the value is out of range; the Range form will raise a
|
1003
|
-
# RangeError, and the Regexp and String will raise an IndexError on negative
|
1004
|
-
# match.
|
1076
|
+
# 'foo'.byteindex(/o+(?!.*o)/) # => 1
|
1077
|
+
# $~ #=> #<MatchData "oo">
|
1005
1078
|
#
|
1006
|
-
|
1007
|
-
|
1008
|
-
| (Range[Integer] | Range[Integer?] range, String new_str) -> String
|
1009
|
-
| (Regexp regexp, String new_str) -> String
|
1010
|
-
| (Regexp regexp, int capture, String new_str) -> String
|
1011
|
-
| (Regexp regexp, String name, String new_str) -> String
|
1012
|
-
| (String other_str, String new_str) -> String
|
1013
|
-
|
1014
|
-
# <!--
|
1015
|
-
# rdoc-file=string.c
|
1016
|
-
# - str.ascii_only? -> true or false
|
1017
|
-
# -->
|
1018
|
-
# Returns true for a string which has only ASCII characters.
|
1079
|
+
# Integer argument `offset`, if given and non-negative, specifies the maximum
|
1080
|
+
# starting byte-based position in the string to *end* the search:
|
1019
1081
|
#
|
1020
|
-
#
|
1021
|
-
#
|
1082
|
+
# 'foo'.byterindex('o', 0) # => nil
|
1083
|
+
# 'foo'.byterindex('o', 1) # => 1
|
1084
|
+
# 'foo'.byterindex('o', 2) # => 2
|
1085
|
+
# 'foo'.byterindex('o', 3) # => 2
|
1022
1086
|
#
|
1023
|
-
|
1024
|
-
|
1025
|
-
# <!--
|
1026
|
-
# rdoc-file=string.c
|
1027
|
-
# - str.b -> str
|
1028
|
-
# -->
|
1029
|
-
# Returns a copied string whose encoding is ASCII-8BIT.
|
1087
|
+
# If `offset` is a negative Integer, the maximum starting position in the string
|
1088
|
+
# to *end* the search is the sum of the string's length and `offset`:
|
1030
1089
|
#
|
1031
|
-
|
1090
|
+
# 'foo'.byterindex('o', -1) # => 2
|
1091
|
+
# 'foo'.byterindex('o', -2) # => 1
|
1092
|
+
# 'foo'.byterindex('o', -3) # => nil
|
1093
|
+
# 'foo'.byterindex('o', -4) # => nil
|
1094
|
+
#
|
1095
|
+
# If `offset` does not land on character (codepoint) boundary, `IndexError` is
|
1096
|
+
# raised.
|
1097
|
+
#
|
1098
|
+
# Related: String#byteindex.
|
1099
|
+
#
|
1100
|
+
def byterindex: (Regexp | string pattern, ?int offset) -> Integer?
|
1032
1101
|
|
1033
1102
|
# <!--
|
1034
1103
|
# rdoc-file=string.c
|
1035
|
-
# -
|
1104
|
+
# - bytes -> array_of_bytes
|
1036
1105
|
# -->
|
1037
|
-
# Returns an array of bytes in
|
1038
|
-
# `str.each_byte.to_a`.
|
1106
|
+
# Returns an array of the bytes in `self`:
|
1039
1107
|
#
|
1040
|
-
#
|
1041
|
-
#
|
1108
|
+
# 'hello'.bytes # => [104, 101, 108, 108, 111]
|
1109
|
+
# 'тест'.bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
|
1110
|
+
# 'こんにちは'.bytes
|
1111
|
+
# # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
1042
1112
|
#
|
1043
1113
|
def bytes: () -> Array[Integer]
|
1044
|
-
| () { (Integer byte) -> void } ->
|
1114
|
+
| () { (Integer byte) -> void } -> self
|
1045
1115
|
|
1046
1116
|
# <!--
|
1047
1117
|
# rdoc-file=string.c
|
1048
1118
|
# - bytesize -> integer
|
1049
1119
|
# -->
|
1050
|
-
# Returns the count
|
1120
|
+
# Returns the count of bytes (not characters) in `self`:
|
1051
1121
|
#
|
1052
|
-
#
|
1053
|
-
#
|
1122
|
+
# 'foo'.bytesize # => 3
|
1123
|
+
# 'тест'.bytesize # => 8
|
1124
|
+
# 'こんにちは'.bytesize # => 15
|
1054
1125
|
#
|
1055
|
-
#
|
1126
|
+
# Contrast with String#length:
|
1127
|
+
#
|
1128
|
+
# 'foo'.length # => 3
|
1129
|
+
# 'тест'.length # => 4
|
1130
|
+
# 'こんにちは'.length # => 5
|
1056
1131
|
#
|
1057
1132
|
def bytesize: () -> Integer
|
1058
1133
|
|
@@ -1098,7 +1173,34 @@ class String
|
|
1098
1173
|
# s.byteslice(4).encoding # => #<Encoding:UTF-8>
|
1099
1174
|
#
|
1100
1175
|
def byteslice: (int start, ?int length) -> String?
|
1101
|
-
| (
|
1176
|
+
| (range[int?] range) -> String?
|
1177
|
+
|
1178
|
+
# <!--
|
1179
|
+
# rdoc-file=string.c
|
1180
|
+
# - bytesplice(index, length, str) -> string
|
1181
|
+
# - bytesplice(index, length, str, str_index, str_length) -> string
|
1182
|
+
# - bytesplice(range, str) -> string
|
1183
|
+
# - bytesplice(range, str, str_range) -> string
|
1184
|
+
# -->
|
1185
|
+
# Replaces some or all of the content of `self` with `str`, and returns `self`.
|
1186
|
+
# The portion of the string affected is determined using the same criteria as
|
1187
|
+
# String#byteslice, except that `length` cannot be omitted. If the replacement
|
1188
|
+
# string is not the same length as the text it is replacing, the string will be
|
1189
|
+
# adjusted accordingly.
|
1190
|
+
#
|
1191
|
+
# If `str_index` and `str_length`, or `str_range` are given, the content of
|
1192
|
+
# `self` is replaced by str.byteslice(str_index, str_length) or
|
1193
|
+
# str.byteslice(str_range); however the substring of `str` is not allocated as a
|
1194
|
+
# new string.
|
1195
|
+
#
|
1196
|
+
# The form that take an Integer will raise an IndexError if the value is out of
|
1197
|
+
# range; the Range form will raise a RangeError. If the beginning or ending
|
1198
|
+
# offset does not land on character (codepoint) boundary, an IndexError will be
|
1199
|
+
# raised.
|
1200
|
+
#
|
1201
|
+
def bytesplice: (Integer start, int length, string str) -> String
|
1202
|
+
| (int start, int length, string str, int str_start, int str_length) -> String
|
1203
|
+
| (range[int?] range, string str, ?range[int?] str_range) -> String
|
1102
1204
|
|
1103
1205
|
# <!--
|
1104
1206
|
# rdoc-file=string.c
|
@@ -1111,7 +1213,7 @@ class String
|
|
1111
1213
|
# s.capitalize # => "Hello world!"
|
1112
1214
|
#
|
1113
1215
|
# The casing may be affected by the given `options`; see [Case
|
1114
|
-
# Mapping](
|
1216
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1115
1217
|
#
|
1116
1218
|
# Related: String#capitalize!.
|
1117
1219
|
#
|
@@ -1133,14 +1235,14 @@ class String
|
|
1133
1235
|
# s.capitalize! # => nil
|
1134
1236
|
#
|
1135
1237
|
# The casing may be affected by the given `options`; see [Case
|
1136
|
-
# Mapping](
|
1238
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1137
1239
|
#
|
1138
1240
|
# Related: String#capitalize.
|
1139
1241
|
#
|
1140
|
-
def capitalize!: () ->
|
1141
|
-
| (:ascii | :lithuanian | :turkic) ->
|
1142
|
-
| (:lithuanian, :turkic) ->
|
1143
|
-
| (:turkic, :lithuanian) ->
|
1242
|
+
def capitalize!: () -> self?
|
1243
|
+
| (:ascii | :lithuanian | :turkic) -> self?
|
1244
|
+
| (:lithuanian, :turkic) -> self?
|
1245
|
+
| (:turkic, :lithuanian) -> self?
|
1144
1246
|
|
1145
1247
|
# <!--
|
1146
1248
|
# rdoc-file=string.c
|
@@ -1153,7 +1255,6 @@ class String
|
|
1153
1255
|
# * 1 if `other_string.downcase` is smaller.
|
1154
1256
|
# * `nil` if the two are incomparable.
|
1155
1257
|
#
|
1156
|
-
#
|
1157
1258
|
# Examples:
|
1158
1259
|
#
|
1159
1260
|
# 'foo'.casecmp('foo') # => 0
|
@@ -1163,11 +1264,12 @@ class String
|
|
1163
1264
|
# 'foo'.casecmp('FOO') # => 0
|
1164
1265
|
# 'foo'.casecmp(1) # => nil
|
1165
1266
|
#
|
1166
|
-
# See [Case Mapping](
|
1267
|
+
# See [Case Mapping](rdoc-ref:case_mapping.rdoc).
|
1167
1268
|
#
|
1168
1269
|
# Related: String#casecmp?.
|
1169
1270
|
#
|
1170
|
-
def casecmp: (
|
1271
|
+
def casecmp: (string other) -> (-1 | 0 | 1)
|
1272
|
+
| (untyped) -> (-1 | 0 | 1)?
|
1171
1273
|
|
1172
1274
|
# <!--
|
1173
1275
|
# rdoc-file=string.c
|
@@ -1186,95 +1288,132 @@ class String
|
|
1186
1288
|
#
|
1187
1289
|
# 'foo'.casecmp?(1) # => nil
|
1188
1290
|
#
|
1189
|
-
# See [Case Mapping](
|
1291
|
+
# See [Case Mapping](rdoc-ref:case_mapping.rdoc).
|
1190
1292
|
#
|
1191
1293
|
# Related: String#casecmp.
|
1192
1294
|
#
|
1193
|
-
def casecmp?: (
|
1295
|
+
def casecmp?: (string other) -> bool
|
1296
|
+
| (untyped) -> bool?
|
1194
1297
|
|
1195
1298
|
# <!--
|
1196
1299
|
# rdoc-file=string.c
|
1197
|
-
# -
|
1300
|
+
# - center(size, pad_string = ' ') -> new_string
|
1198
1301
|
# -->
|
1199
|
-
#
|
1200
|
-
#
|
1201
|
-
# `
|
1302
|
+
# Returns a centered copy of `self`.
|
1303
|
+
#
|
1304
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
1305
|
+
# returns a new string of length `size` that is a copy of `self`, centered and
|
1306
|
+
# padded on both ends with `pad_string`:
|
1202
1307
|
#
|
1203
|
-
#
|
1204
|
-
#
|
1205
|
-
#
|
1308
|
+
# 'hello'.center(10) # => " hello "
|
1309
|
+
# ' hello'.center(10) # => " hello "
|
1310
|
+
# 'hello'.center(10, 'ab') # => "abhelloaba"
|
1311
|
+
# 'тест'.center(10) # => " тест "
|
1312
|
+
# 'こんにちは'.center(10) # => " こんにちは "
|
1206
1313
|
#
|
1207
|
-
|
1314
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
1315
|
+
#
|
1316
|
+
# 'hello'.center(5) # => "hello"
|
1317
|
+
# 'hello'.center(1) # => "hello"
|
1318
|
+
#
|
1319
|
+
# Related: String#ljust, String#rjust.
|
1320
|
+
#
|
1321
|
+
def center: (int width, ?string pad_string) -> String
|
1208
1322
|
|
1209
1323
|
# <!--
|
1210
1324
|
# rdoc-file=string.c
|
1211
|
-
# -
|
1325
|
+
# - chars -> array_of_characters
|
1212
1326
|
# -->
|
1213
|
-
# Returns an array of characters in
|
1214
|
-
# `str.each_char.to_a`.
|
1327
|
+
# Returns an array of the characters in `self`:
|
1215
1328
|
#
|
1216
|
-
#
|
1217
|
-
#
|
1329
|
+
# 'hello'.chars # => ["h", "e", "l", "l", "o"]
|
1330
|
+
# 'тест'.chars # => ["т", "е", "с", "т"]
|
1331
|
+
# 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
|
1218
1332
|
#
|
1219
1333
|
def chars: () -> Array[String]
|
1220
|
-
| () { (String char) -> void } ->
|
1334
|
+
| () { (String char) -> void } -> self
|
1221
1335
|
|
1222
1336
|
# <!--
|
1223
1337
|
# rdoc-file=string.c
|
1224
|
-
# -
|
1338
|
+
# - chomp(line_sep = $/) -> new_string
|
1225
1339
|
# -->
|
1226
|
-
# Returns a new
|
1227
|
-
#
|
1228
|
-
#
|
1229
|
-
#
|
1230
|
-
#
|
1340
|
+
# Returns a new string copied from `self`, with trailing characters possibly
|
1341
|
+
# removed:
|
1342
|
+
#
|
1343
|
+
# When `line_sep` is `"\n"`, removes the last one or two characters if they are
|
1344
|
+
# `"\r"`, `"\n"`, or `"\r\n"` (but not `"\n\r"`):
|
1231
1345
|
#
|
1232
|
-
#
|
1233
|
-
# "
|
1234
|
-
# "
|
1235
|
-
# "
|
1236
|
-
# "
|
1237
|
-
# "
|
1238
|
-
# "
|
1239
|
-
# "hello\r\n\r\n".chomp('') #=> "hello"
|
1240
|
-
# "hello\r\n\r\r\n".chomp('') #=> "hello\r\n\r"
|
1346
|
+
# $/ # => "\n"
|
1347
|
+
# "abc\r".chomp # => "abc"
|
1348
|
+
# "abc\n".chomp # => "abc"
|
1349
|
+
# "abc\r\n".chomp # => "abc"
|
1350
|
+
# "abc\n\r".chomp # => "abc\n"
|
1351
|
+
# "тест\r\n".chomp # => "тест"
|
1352
|
+
# "こんにちは\r\n".chomp # => "こんにちは"
|
1241
1353
|
#
|
1242
|
-
|
1354
|
+
# When `line_sep` is `''` (an empty string), removes multiple trailing
|
1355
|
+
# occurrences of `"\n"` or `"\r\n"` (but not `"\r"` or `"\n\r"`):
|
1356
|
+
#
|
1357
|
+
# "abc\n\n\n".chomp('') # => "abc"
|
1358
|
+
# "abc\r\n\r\n\r\n".chomp('') # => "abc"
|
1359
|
+
# "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc"
|
1360
|
+
# "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r"
|
1361
|
+
# "abc\r\r\r".chomp('') # => "abc\r\r\r"
|
1362
|
+
#
|
1363
|
+
# When `line_sep` is neither `"\n"` nor `''`, removes a single trailing line
|
1364
|
+
# separator if there is one:
|
1365
|
+
#
|
1366
|
+
# 'abcd'.chomp('d') # => "abc"
|
1367
|
+
# 'abcdd'.chomp('d') # => "abcd"
|
1368
|
+
#
|
1369
|
+
def chomp: (?string? separator) -> String
|
1243
1370
|
|
1244
1371
|
# <!--
|
1245
1372
|
# rdoc-file=string.c
|
1246
|
-
# -
|
1373
|
+
# - chomp!(line_sep = $/) -> self or nil
|
1247
1374
|
# -->
|
1248
|
-
#
|
1249
|
-
# `
|
1375
|
+
# Like String#chomp, but modifies `self` in place; returns `nil` if no
|
1376
|
+
# modification made, `self` otherwise.
|
1250
1377
|
#
|
1251
|
-
def chomp!: (
|
1378
|
+
def chomp!: (nil) -> nil
|
1379
|
+
# | (?string separator) -> self? # https://github.com/ruby/rbs/pull/1672#discussion_r1423324796
|
1380
|
+
| (?string? separator) -> self?
|
1252
1381
|
|
1253
1382
|
# <!--
|
1254
1383
|
# rdoc-file=string.c
|
1255
|
-
# -
|
1384
|
+
# - chop -> new_string
|
1256
1385
|
# -->
|
1257
|
-
# Returns a new
|
1258
|
-
#
|
1259
|
-
#
|
1260
|
-
#
|
1386
|
+
# Returns a new string copied from `self`, with trailing characters possibly
|
1387
|
+
# removed.
|
1388
|
+
#
|
1389
|
+
# Removes `"\r\n"` if those are the last two characters.
|
1390
|
+
#
|
1391
|
+
# "abc\r\n".chop # => "abc"
|
1392
|
+
# "тест\r\n".chop # => "тест"
|
1393
|
+
# "こんにちは\r\n".chop # => "こんにちは"
|
1394
|
+
#
|
1395
|
+
# Otherwise removes the last character if it exists.
|
1396
|
+
#
|
1397
|
+
# 'abcd'.chop # => "abc"
|
1398
|
+
# 'тест'.chop # => "тес"
|
1399
|
+
# 'こんにちは'.chop # => "こんにち"
|
1400
|
+
# ''.chop # => ""
|
1261
1401
|
#
|
1262
|
-
#
|
1263
|
-
#
|
1264
|
-
# "string\n".chop #=> "string"
|
1265
|
-
# "string".chop #=> "strin"
|
1266
|
-
# "x".chop.chop #=> ""
|
1402
|
+
# If you only need to remove the newline separator at the end of the string,
|
1403
|
+
# String#chomp is a better alternative.
|
1267
1404
|
#
|
1268
1405
|
def chop: () -> String
|
1269
1406
|
|
1270
1407
|
# <!--
|
1271
1408
|
# rdoc-file=string.c
|
1272
|
-
# -
|
1409
|
+
# - chop! -> self or nil
|
1273
1410
|
# -->
|
1274
|
-
#
|
1275
|
-
# empty
|
1411
|
+
# Like String#chop, but modifies `self` in place; returns `nil` if `self` is
|
1412
|
+
# empty, `self` otherwise.
|
1276
1413
|
#
|
1277
|
-
|
1414
|
+
# Related: String#chomp!.
|
1415
|
+
#
|
1416
|
+
def chop!: () -> self?
|
1278
1417
|
|
1279
1418
|
# <!--
|
1280
1419
|
# rdoc-file=string.c
|
@@ -1296,20 +1435,21 @@ class String
|
|
1296
1435
|
# s = 'foo' # => "foo"
|
1297
1436
|
# s.clear # => ""
|
1298
1437
|
#
|
1299
|
-
def clear: () ->
|
1438
|
+
def clear: () -> self
|
1300
1439
|
|
1301
1440
|
# <!--
|
1302
1441
|
# rdoc-file=string.c
|
1303
|
-
# -
|
1442
|
+
# - codepoints -> array_of_integers
|
1304
1443
|
# -->
|
1305
|
-
# Returns an array of the
|
1306
|
-
#
|
1444
|
+
# Returns an array of the codepoints in `self`; each codepoint is the integer
|
1445
|
+
# value for a character:
|
1307
1446
|
#
|
1308
|
-
#
|
1309
|
-
#
|
1447
|
+
# 'hello'.codepoints # => [104, 101, 108, 108, 111]
|
1448
|
+
# 'тест'.codepoints # => [1090, 1077, 1089, 1090]
|
1449
|
+
# 'こんにちは'.codepoints # => [12371, 12435, 12395, 12385, 12399]
|
1310
1450
|
#
|
1311
|
-
def codepoints: () ->
|
1312
|
-
| () { (Integer codepoint) -> void } ->
|
1451
|
+
def codepoints: () -> Array[Integer]
|
1452
|
+
| () { (Integer codepoint) -> void } -> self
|
1313
1453
|
|
1314
1454
|
# <!--
|
1315
1455
|
# rdoc-file=string.c
|
@@ -1329,18 +1469,15 @@ class String
|
|
1329
1469
|
#
|
1330
1470
|
# Related: String#<<, which takes a single argument.
|
1331
1471
|
#
|
1332
|
-
def concat: (*string | Integer
|
1472
|
+
def concat: (*string | Integer string_or_codepoints) -> self
|
1333
1473
|
|
1334
1474
|
# <!--
|
1335
1475
|
# rdoc-file=string.c
|
1336
|
-
# -
|
1476
|
+
# - count(*selectors) -> integer
|
1337
1477
|
# -->
|
1338
|
-
#
|
1339
|
-
#
|
1340
|
-
#
|
1341
|
-
# means all characters between c1 and c2. The backslash character `\` can be
|
1342
|
-
# used to escape `^` or `-` and is otherwise ignored unless it appears at the
|
1343
|
-
# end of a sequence or the end of a `other_str`.
|
1478
|
+
# Returns the total number of characters in `self` that are specified by the
|
1479
|
+
# given `selectors` (see [Multiple Character
|
1480
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
1344
1481
|
#
|
1345
1482
|
# a = "hello world"
|
1346
1483
|
# a.count "lo" #=> 5
|
@@ -1356,11 +1493,11 @@ class String
|
|
1356
1493
|
# c.count "\\A" #=> 0
|
1357
1494
|
# c.count "X-\\w" #=> 3
|
1358
1495
|
#
|
1359
|
-
def count: (
|
1496
|
+
def count: (selector selector_0, *selector more_selectors) -> Integer
|
1360
1497
|
|
1361
1498
|
# <!--
|
1362
1499
|
# rdoc-file=string.c
|
1363
|
-
# -
|
1500
|
+
# - crypt(salt_str) -> new_string
|
1364
1501
|
# -->
|
1365
1502
|
# Returns the string generated by calling `crypt(3)` standard library function
|
1366
1503
|
# with `str` and `salt_str`, in this order, as its arguments. Please do not use
|
@@ -1393,7 +1530,6 @@ class String
|
|
1393
1530
|
# "foo".crypt("$5$rounds=1000$salt$") # OK, proper usage
|
1394
1531
|
# "foo".crypt("$5$round=1000$salt$") # Typo not detected
|
1395
1532
|
#
|
1396
|
-
#
|
1397
1533
|
# * Even in the "modular" mode, some hash functions are considered archaic and
|
1398
1534
|
# no longer recommended at all; for instance module `$1$` is officially
|
1399
1535
|
# abandoned by its author: see http://phk.freebsd.dk/sagas/md5crypt_eol/ .
|
@@ -1407,80 +1543,100 @@ class String
|
|
1407
1543
|
#
|
1408
1544
|
# "foo".crypt("$5$rounds=1000$salt$") # => "$5fNPQMxC5j6."
|
1409
1545
|
#
|
1410
|
-
#
|
1411
1546
|
# If for some reason you cannot migrate to other secure contemporary password
|
1412
1547
|
# hashing algorithms, install the string-crypt gem and `require 'string/crypt'`
|
1413
1548
|
# to continue using it.
|
1414
1549
|
#
|
1415
1550
|
def crypt: (string salt_str) -> String
|
1416
1551
|
|
1552
|
+
# <!-- rdoc-file=string.c -->
|
1553
|
+
# Returns a frozen, possibly pre-existing copy of the string.
|
1554
|
+
#
|
1555
|
+
# The returned `String` will be deduplicated as long as it does not have any
|
1556
|
+
# instance variables set on it and is not a String subclass.
|
1557
|
+
#
|
1558
|
+
# Note that `-string` variant is more convenient for defining constants:
|
1559
|
+
#
|
1560
|
+
# FILENAME = -'config/database.yml'
|
1561
|
+
#
|
1562
|
+
# while `dedup` is better suitable for using the method in chains of
|
1563
|
+
# calculations:
|
1564
|
+
#
|
1565
|
+
# @url_list.concat(urls.map(&:dedup))
|
1566
|
+
#
|
1567
|
+
alias dedup -@
|
1568
|
+
|
1417
1569
|
# <!--
|
1418
1570
|
# rdoc-file=string.c
|
1419
|
-
# -
|
1571
|
+
# - delete(*selectors) -> new_string
|
1420
1572
|
# -->
|
1421
|
-
# Returns a copy of
|
1422
|
-
#
|
1423
|
-
#
|
1573
|
+
# Returns a copy of `self` with characters specified by `selectors` removed (see
|
1574
|
+
# [Multiple Character
|
1575
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
1424
1576
|
#
|
1425
1577
|
# "hello".delete "l","lo" #=> "heo"
|
1426
1578
|
# "hello".delete "lo" #=> "he"
|
1427
1579
|
# "hello".delete "aeiou", "^e" #=> "hell"
|
1428
1580
|
# "hello".delete "ej-m" #=> "ho"
|
1429
1581
|
#
|
1430
|
-
def delete: (
|
1582
|
+
def delete: (selector selector_0, *selector more_selectors) -> String
|
1431
1583
|
|
1432
1584
|
# <!--
|
1433
1585
|
# rdoc-file=string.c
|
1434
|
-
# -
|
1586
|
+
# - delete!(*selectors) -> self or nil
|
1435
1587
|
# -->
|
1436
|
-
#
|
1437
|
-
#
|
1588
|
+
# Like String#delete, but modifies `self` in place. Returns `self` if any
|
1589
|
+
# changes were made, `nil` otherwise.
|
1438
1590
|
#
|
1439
|
-
def delete!: (
|
1591
|
+
def delete!: (selector selector_0, *selector more_selectors) -> self?
|
1440
1592
|
|
1441
1593
|
# <!--
|
1442
1594
|
# rdoc-file=string.c
|
1443
|
-
# -
|
1595
|
+
# - delete_prefix(prefix) -> new_string
|
1444
1596
|
# -->
|
1445
|
-
# Returns a copy of
|
1597
|
+
# Returns a copy of `self` with leading substring `prefix` removed:
|
1598
|
+
#
|
1599
|
+
# 'hello'.delete_prefix('hel') # => "lo"
|
1600
|
+
# 'hello'.delete_prefix('llo') # => "hello"
|
1601
|
+
# 'тест'.delete_prefix('те') # => "ст"
|
1602
|
+
# 'こんにちは'.delete_prefix('こん') # => "にちは"
|
1446
1603
|
#
|
1447
|
-
#
|
1448
|
-
# "hello".delete_prefix("llo") #=> "hello"
|
1604
|
+
# Related: String#delete_prefix!, String#delete_suffix.
|
1449
1605
|
#
|
1450
1606
|
def delete_prefix: (string prefix) -> String
|
1451
1607
|
|
1452
1608
|
# <!--
|
1453
1609
|
# rdoc-file=string.c
|
1454
|
-
# -
|
1610
|
+
# - delete_prefix!(prefix) -> self or nil
|
1455
1611
|
# -->
|
1456
|
-
#
|
1612
|
+
# Like String#delete_prefix, except that `self` is modified in place. Returns
|
1613
|
+
# `self` if the prefix is removed, `nil` otherwise.
|
1457
1614
|
#
|
1458
|
-
|
1459
|
-
# "hello".delete_prefix!("llo") #=> nil
|
1460
|
-
#
|
1461
|
-
def delete_prefix!: (string prefix) -> String?
|
1615
|
+
def delete_prefix!: (string prefix) -> self?
|
1462
1616
|
|
1463
1617
|
# <!--
|
1464
1618
|
# rdoc-file=string.c
|
1465
|
-
# -
|
1619
|
+
# - delete_suffix(suffix) -> new_string
|
1466
1620
|
# -->
|
1467
|
-
# Returns a copy of
|
1621
|
+
# Returns a copy of `self` with trailing substring `suffix` removed:
|
1622
|
+
#
|
1623
|
+
# 'hello'.delete_suffix('llo') # => "he"
|
1624
|
+
# 'hello'.delete_suffix('hel') # => "hello"
|
1625
|
+
# 'тест'.delete_suffix('ст') # => "те"
|
1626
|
+
# 'こんにちは'.delete_suffix('ちは') # => "こんに"
|
1468
1627
|
#
|
1469
|
-
#
|
1470
|
-
# "hello".delete_suffix("hel") #=> "hello"
|
1628
|
+
# Related: String#delete_suffix!, String#delete_prefix.
|
1471
1629
|
#
|
1472
1630
|
def delete_suffix: (string suffix) -> String
|
1473
1631
|
|
1474
1632
|
# <!--
|
1475
1633
|
# rdoc-file=string.c
|
1476
|
-
# -
|
1634
|
+
# - delete_suffix!(suffix) -> self or nil
|
1477
1635
|
# -->
|
1478
|
-
#
|
1636
|
+
# Like String#delete_suffix, except that `self` is modified in place. Returns
|
1637
|
+
# `self` if the suffix is removed, `nil` otherwise.
|
1479
1638
|
#
|
1480
|
-
|
1481
|
-
# "hello".delete_suffix!("hel") #=> nil
|
1482
|
-
#
|
1483
|
-
def delete_suffix!: (string suffix) -> String?
|
1639
|
+
def delete_suffix!: (string suffix) -> self?
|
1484
1640
|
|
1485
1641
|
# <!--
|
1486
1642
|
# rdoc-file=string.c
|
@@ -1492,7 +1648,7 @@ class String
|
|
1492
1648
|
# s.downcase # => "hello world!"
|
1493
1649
|
#
|
1494
1650
|
# The casing may be affected by the given `options`; see [Case
|
1495
|
-
# Mapping](
|
1651
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1496
1652
|
#
|
1497
1653
|
# Related: String#downcase!, String#upcase, String#upcase!.
|
1498
1654
|
#
|
@@ -1514,14 +1670,14 @@ class String
|
|
1514
1670
|
# s.downcase! # => nil
|
1515
1671
|
#
|
1516
1672
|
# The casing may be affected by the given `options`; see [Case
|
1517
|
-
# Mapping](
|
1673
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
1518
1674
|
#
|
1519
1675
|
# Related: String#downcase, String#upcase, String#upcase!.
|
1520
1676
|
#
|
1521
|
-
def downcase!: () ->
|
1522
|
-
| (:ascii | :fold | :lithuanian | :turkic) ->
|
1523
|
-
| (:lithuanian, :turkic) ->
|
1524
|
-
| (:turkic, :lithuanian) ->
|
1677
|
+
def downcase!: () -> self?
|
1678
|
+
| (:ascii | :fold | :lithuanian | :turkic) -> self?
|
1679
|
+
| (:lithuanian, :turkic) -> self?
|
1680
|
+
| (:turkic, :lithuanian) -> self?
|
1525
1681
|
|
1526
1682
|
# <!--
|
1527
1683
|
# rdoc-file=string.c
|
@@ -1540,120 +1696,168 @@ class String
|
|
1540
1696
|
|
1541
1697
|
# <!--
|
1542
1698
|
# rdoc-file=string.c
|
1543
|
-
# -
|
1544
|
-
# -
|
1699
|
+
# - each_byte {|byte| ... } -> self
|
1700
|
+
# - each_byte -> enumerator
|
1545
1701
|
# -->
|
1546
|
-
#
|
1547
|
-
# block is given.
|
1702
|
+
# Calls the given block with each successive byte from `self`; returns `self`:
|
1548
1703
|
#
|
1549
|
-
#
|
1704
|
+
# 'hello'.each_byte {|byte| print byte, ' ' }
|
1705
|
+
# print "\n"
|
1706
|
+
# 'тест'.each_byte {|byte| print byte, ' ' }
|
1707
|
+
# print "\n"
|
1708
|
+
# 'こんにちは'.each_byte {|byte| print byte, ' ' }
|
1709
|
+
# print "\n"
|
1550
1710
|
#
|
1551
|
-
#
|
1711
|
+
# Output:
|
1552
1712
|
#
|
1553
1713
|
# 104 101 108 108 111
|
1714
|
+
# 209 130 208 181 209 129 209 130
|
1715
|
+
# 227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
|
1554
1716
|
#
|
1555
|
-
|
1556
|
-
|
1717
|
+
# Returns an enumerator if no block is given.
|
1718
|
+
#
|
1719
|
+
def each_byte: () -> Enumerator[Integer, self]
|
1720
|
+
| () { (Integer byte) -> void } -> self
|
1557
1721
|
|
1558
1722
|
# <!--
|
1559
1723
|
# rdoc-file=string.c
|
1560
|
-
# -
|
1561
|
-
# -
|
1724
|
+
# - each_char {|c| ... } -> self
|
1725
|
+
# - each_char -> enumerator
|
1562
1726
|
# -->
|
1563
|
-
#
|
1564
|
-
#
|
1727
|
+
# Calls the given block with each successive character from `self`; returns
|
1728
|
+
# `self`:
|
1565
1729
|
#
|
1566
|
-
#
|
1730
|
+
# 'hello'.each_char {|char| print char, ' ' }
|
1731
|
+
# print "\n"
|
1732
|
+
# 'тест'.each_char {|char| print char, ' ' }
|
1733
|
+
# print "\n"
|
1734
|
+
# 'こんにちは'.each_char {|char| print char, ' ' }
|
1735
|
+
# print "\n"
|
1567
1736
|
#
|
1568
|
-
#
|
1737
|
+
# Output:
|
1569
1738
|
#
|
1570
1739
|
# h e l l o
|
1740
|
+
# т е с т
|
1741
|
+
# こ ん に ち は
|
1571
1742
|
#
|
1572
|
-
|
1573
|
-
|
1743
|
+
# Returns an enumerator if no block is given.
|
1744
|
+
#
|
1745
|
+
def each_char: () -> Enumerator[String, self]
|
1746
|
+
| () { (String char) -> void } -> self
|
1574
1747
|
|
1575
1748
|
# <!--
|
1576
1749
|
# rdoc-file=string.c
|
1577
|
-
# -
|
1578
|
-
# -
|
1750
|
+
# - each_codepoint {|integer| ... } -> self
|
1751
|
+
# - each_codepoint -> enumerator
|
1579
1752
|
# -->
|
1580
|
-
#
|
1581
|
-
#
|
1582
|
-
# other than UTF-8/UTF-16(BE|LE)/UTF-32(BE|LE), values are directly derived from
|
1583
|
-
# the binary representation of each character.
|
1753
|
+
# Calls the given block with each successive codepoint from `self`; each
|
1754
|
+
# codepoint is the integer value for a character; returns `self`:
|
1584
1755
|
#
|
1585
|
-
#
|
1756
|
+
# 'hello'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1757
|
+
# print "\n"
|
1758
|
+
# 'тест'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1759
|
+
# print "\n"
|
1760
|
+
# 'こんにちは'.each_codepoint {|codepoint| print codepoint, ' ' }
|
1761
|
+
# print "\n"
|
1586
1762
|
#
|
1587
|
-
#
|
1763
|
+
# Output:
|
1588
1764
|
#
|
1589
|
-
#
|
1765
|
+
# 104 101 108 108 111
|
1766
|
+
# 1090 1077 1089 1090
|
1767
|
+
# 12371 12435 12395 12385 12399
|
1590
1768
|
#
|
1591
|
-
#
|
1769
|
+
# Returns an enumerator if no block is given.
|
1592
1770
|
#
|
1593
|
-
def each_codepoint: ()
|
1594
|
-
| ()
|
1771
|
+
def each_codepoint: () -> Enumerator[Integer, self]
|
1772
|
+
| () { (Integer codepoint) -> void } -> self
|
1595
1773
|
|
1596
1774
|
# <!--
|
1597
1775
|
# rdoc-file=string.c
|
1598
|
-
# -
|
1599
|
-
# -
|
1776
|
+
# - each_grapheme_cluster {|gc| ... } -> self
|
1777
|
+
# - each_grapheme_cluster -> enumerator
|
1600
1778
|
# -->
|
1601
|
-
#
|
1602
|
-
#
|
1603
|
-
#
|
1604
|
-
#
|
1779
|
+
# Calls the given block with each successive grapheme cluster from `self` (see
|
1780
|
+
# [Unicode Grapheme Cluster
|
1781
|
+
# Boundaries](https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)
|
1782
|
+
# ); returns `self`:
|
1783
|
+
#
|
1784
|
+
# s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈"
|
1785
|
+
# s.each_grapheme_cluster {|gc| print gc, ' ' }
|
1786
|
+
#
|
1787
|
+
# Output:
|
1605
1788
|
#
|
1606
|
-
#
|
1607
|
-
# "a\u0300".each_grapheme_cluster.to_a.size #=> 1
|
1789
|
+
# ä - p q r - b̈ - x y z - c̈
|
1608
1790
|
#
|
1609
|
-
|
1610
|
-
|
1791
|
+
# Returns an enumerator if no block is given.
|
1792
|
+
#
|
1793
|
+
def each_grapheme_cluster: () -> Enumerator[String, self]
|
1794
|
+
| () { (String grapheme_cluter) -> void } -> self
|
1611
1795
|
|
1612
1796
|
# <!--
|
1613
1797
|
# rdoc-file=string.c
|
1614
|
-
# -
|
1615
|
-
# -
|
1798
|
+
# - each_line(line_sep = $/, chomp: false) {|substring| ... } -> self
|
1799
|
+
# - each_line(line_sep = $/, chomp: false) -> enumerator
|
1616
1800
|
# -->
|
1617
|
-
#
|
1618
|
-
#
|
1619
|
-
#
|
1620
|
-
#
|
1801
|
+
# With a block given, forms the substrings ("lines") that are the result of
|
1802
|
+
# splitting `self` at each occurrence of the given line separator `line_sep`;
|
1803
|
+
# passes each line to the block; returns `self`:
|
1804
|
+
#
|
1805
|
+
# s = <<~EOT
|
1806
|
+
# This is the first line.
|
1807
|
+
# This is line two.
|
1808
|
+
#
|
1809
|
+
# This is line four.
|
1810
|
+
# This is line five.
|
1811
|
+
# EOT
|
1812
|
+
#
|
1813
|
+
# s.each_line {|line| p line }
|
1814
|
+
#
|
1815
|
+
# Output:
|
1816
|
+
#
|
1817
|
+
# "This is the first line.\n"
|
1818
|
+
# "This is line two.\n"
|
1819
|
+
# "\n"
|
1820
|
+
# "This is line four.\n"
|
1821
|
+
# "This is line five.\n"
|
1822
|
+
#
|
1823
|
+
# With a different `line_sep`:
|
1621
1824
|
#
|
1622
|
-
#
|
1825
|
+
# s.each_line(' is ') {|line| p line }
|
1826
|
+
#
|
1827
|
+
# Output:
|
1828
|
+
#
|
1829
|
+
# "This is "
|
1830
|
+
# "the first line.\nThis is "
|
1831
|
+
# "line two.\n\nThis is "
|
1832
|
+
# "line four.\nThis is "
|
1833
|
+
# "line five.\n"
|
1834
|
+
#
|
1835
|
+
# With `chomp` as `true`, removes the trailing `line_sep` from each line:
|
1836
|
+
#
|
1837
|
+
# s.each_line(chomp: true) {|line| p line }
|
1838
|
+
#
|
1839
|
+
# Output:
|
1623
1840
|
#
|
1624
|
-
#
|
1841
|
+
# "This is the first line."
|
1842
|
+
# "This is line two."
|
1843
|
+
# ""
|
1844
|
+
# "This is line four."
|
1845
|
+
# "This is line five."
|
1625
1846
|
#
|
1626
|
-
#
|
1627
|
-
#
|
1628
|
-
# # "hello\n"
|
1629
|
-
# # "world"
|
1847
|
+
# With an empty string as `line_sep`, forms and passes "paragraphs" by splitting
|
1848
|
+
# at each occurrence of two or more newlines:
|
1630
1849
|
#
|
1631
|
-
#
|
1632
|
-
# # prints:
|
1633
|
-
# # "hel"
|
1634
|
-
# # "l"
|
1635
|
-
# # "o\nworl"
|
1636
|
-
# # "d"
|
1850
|
+
# s.each_line('') {|line| p line }
|
1637
1851
|
#
|
1638
|
-
#
|
1639
|
-
# # prints
|
1640
|
-
# # "hello\n\n"
|
1641
|
-
# # "world"
|
1852
|
+
# Output:
|
1642
1853
|
#
|
1643
|
-
# "
|
1644
|
-
#
|
1645
|
-
# # "hello"
|
1646
|
-
# # "world"
|
1854
|
+
# "This is the first line.\nThis is line two.\n\n"
|
1855
|
+
# "This is line four.\nThis is line five.\n"
|
1647
1856
|
#
|
1648
|
-
#
|
1649
|
-
# # prints:
|
1650
|
-
# # "he"
|
1651
|
-
# # ""
|
1652
|
-
# # "o\nwor"
|
1653
|
-
# # "d"
|
1857
|
+
# With no block given, returns an enumerator.
|
1654
1858
|
#
|
1655
|
-
def each_line: (?string separator, ?chomp: boolish)
|
1656
|
-
| (?string separator, ?chomp: boolish)
|
1859
|
+
def each_line: (?string? separator, ?chomp: boolish) -> Enumerator[String, self]
|
1860
|
+
| (?string? separator, ?chomp: boolish) { (String line) -> void } -> self
|
1657
1861
|
|
1658
1862
|
# <!--
|
1659
1863
|
# rdoc-file=string.c
|
@@ -1669,68 +1873,96 @@ class String
|
|
1669
1873
|
|
1670
1874
|
# <!--
|
1671
1875
|
# rdoc-file=transcode.c
|
1672
|
-
# -
|
1673
|
-
# -
|
1674
|
-
#
|
1675
|
-
#
|
1676
|
-
#
|
1677
|
-
#
|
1678
|
-
#
|
1679
|
-
#
|
1680
|
-
#
|
1681
|
-
#
|
1682
|
-
#
|
1683
|
-
#
|
1684
|
-
#
|
1685
|
-
#
|
1686
|
-
#
|
1687
|
-
#
|
1688
|
-
#
|
1689
|
-
#
|
1690
|
-
# :
|
1691
|
-
#
|
1692
|
-
#
|
1693
|
-
#
|
1694
|
-
#
|
1695
|
-
#
|
1696
|
-
#
|
1697
|
-
#
|
1698
|
-
#
|
1699
|
-
#
|
1700
|
-
#
|
1701
|
-
#
|
1702
|
-
# :
|
1703
|
-
#
|
1704
|
-
#
|
1705
|
-
#
|
1706
|
-
#
|
1707
|
-
#
|
1708
|
-
#
|
1709
|
-
#
|
1710
|
-
#
|
1711
|
-
#
|
1712
|
-
#
|
1713
|
-
#
|
1714
|
-
# :
|
1715
|
-
#
|
1716
|
-
#
|
1717
|
-
#
|
1718
|
-
#
|
1719
|
-
#
|
1720
|
-
|
1876
|
+
# - encode(dst_encoding = Encoding.default_internal, **enc_opts) -> string
|
1877
|
+
# - encode(dst_encoding, src_encoding, **enc_opts) -> string
|
1878
|
+
# -->
|
1879
|
+
# Returns a copy of `self` transcoded as determined by `dst_encoding`. By
|
1880
|
+
# default, raises an exception if `self` contains an invalid byte or a character
|
1881
|
+
# not defined in `dst_encoding`; that behavior may be modified by encoding
|
1882
|
+
# options; see below.
|
1883
|
+
#
|
1884
|
+
# With no arguments:
|
1885
|
+
#
|
1886
|
+
# * Uses the same encoding if `Encoding.default_internal` is `nil` (the
|
1887
|
+
# default):
|
1888
|
+
#
|
1889
|
+
# Encoding.default_internal # => nil
|
1890
|
+
# s = "Ruby\x99".force_encoding('Windows-1252')
|
1891
|
+
# s.encoding # => #<Encoding:Windows-1252>
|
1892
|
+
# s.bytes # => [82, 117, 98, 121, 153]
|
1893
|
+
# t = s.encode # => "Ruby\x99"
|
1894
|
+
# t.encoding # => #<Encoding:Windows-1252>
|
1895
|
+
# t.bytes # => [82, 117, 98, 121, 226, 132, 162]
|
1896
|
+
#
|
1897
|
+
# * Otherwise, uses the encoding `Encoding.default_internal`:
|
1898
|
+
#
|
1899
|
+
# Encoding.default_internal = 'UTF-8'
|
1900
|
+
# t = s.encode # => "Ruby™"
|
1901
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1902
|
+
#
|
1903
|
+
# With only argument `dst_encoding` given, uses that encoding:
|
1904
|
+
#
|
1905
|
+
# s = "Ruby\x99".force_encoding('Windows-1252')
|
1906
|
+
# s.encoding # => #<Encoding:Windows-1252>
|
1907
|
+
# t = s.encode('UTF-8') # => "Ruby™"
|
1908
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1909
|
+
#
|
1910
|
+
# With arguments `dst_encoding` and `src_encoding` given, interprets `self`
|
1911
|
+
# using `src_encoding`, encodes the new string using `dst_encoding`:
|
1912
|
+
#
|
1913
|
+
# s = "Ruby\x99"
|
1914
|
+
# t = s.encode('UTF-8', 'Windows-1252') # => "Ruby™"
|
1915
|
+
# t.encoding # => #<Encoding:UTF-8>
|
1916
|
+
#
|
1917
|
+
# Optional keyword arguments `enc_opts` specify encoding options; see [Encoding
|
1918
|
+
# Options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
1919
|
+
#
|
1920
|
+
# Please note that, unless `invalid: :replace` option is given, conversion from
|
1921
|
+
# an encoding `enc` to the same encoding `enc` (independent of whether `enc` is
|
1922
|
+
# given explicitly or implicitly) is a no-op, i.e. the string is simply copied
|
1923
|
+
# without any changes, and no exceptions are raised, even if there are invalid
|
1924
|
+
# bytes.
|
1925
|
+
#
|
1926
|
+
def encode: (
|
1927
|
+
?encoding source_encoding,
|
1928
|
+
?encoding? from_encoding,
|
1929
|
+
?invalid: :replace ?,
|
1930
|
+
?undef: :replace ?,
|
1931
|
+
?replace: string?,
|
1932
|
+
?xml: (:text | :attr)?,
|
1933
|
+
?newline: (:universal | :crlf | :cr | :lf)?,
|
1934
|
+
?universal_newline: boolish,
|
1935
|
+
?cr_newline: boolish,
|
1936
|
+
?crlf_newline: boolish,
|
1937
|
+
?lf_newline: boolish,
|
1938
|
+
?fallback: ^(String) -> string? | Method | _EncodeFallbackAref
|
1939
|
+
) -> instance
|
1940
|
+
|
1941
|
+
interface _EncodeFallbackAref
|
1942
|
+
def []: (String) -> string?
|
1943
|
+
end
|
1721
1944
|
|
1722
1945
|
# <!--
|
1723
1946
|
# rdoc-file=transcode.c
|
1724
|
-
# -
|
1725
|
-
# -
|
1947
|
+
# - encode!(dst_encoding = Encoding.default_internal, **enc_opts) -> self
|
1948
|
+
# - encode!(dst_encoding, src_encoding, **enc_opts) -> self
|
1726
1949
|
# -->
|
1727
|
-
#
|
1728
|
-
# `encoding`. The second form transcodes the contents of *str* from src_encoding
|
1729
|
-
# to dst_encoding. The `options` keyword arguments give details for conversion.
|
1730
|
-
# See String#encode for details. Returns the string even if no changes were
|
1731
|
-
# made.
|
1950
|
+
# Like #encode, but applies encoding changes to `self`; returns `self`.
|
1732
1951
|
#
|
1733
|
-
def encode!: (
|
1952
|
+
def encode!: (
|
1953
|
+
?encoding source_encoding,
|
1954
|
+
?encoding? from_encoding,
|
1955
|
+
?invalid: :replace ?,
|
1956
|
+
?undef: :replace ?,
|
1957
|
+
?replace: string?,
|
1958
|
+
?xml: (:text | :attr)?,
|
1959
|
+
?newline: (:universal | :crlf | :cr | :lf)?,
|
1960
|
+
?universal_newline: boolish,
|
1961
|
+
?cr_newline: boolish,
|
1962
|
+
?crlf_newline: boolish,
|
1963
|
+
?lf_newline: boolish,
|
1964
|
+
?fallback: ^(String) -> string? | Method | _EncodeFallbackAref
|
1965
|
+
) -> self
|
1734
1966
|
|
1735
1967
|
# <!--
|
1736
1968
|
# rdoc-file=string.c
|
@@ -1742,15 +1974,19 @@ class String
|
|
1742
1974
|
|
1743
1975
|
# <!--
|
1744
1976
|
# rdoc-file=string.c
|
1745
|
-
# -
|
1977
|
+
# - end_with?(*strings) -> true or false
|
1746
1978
|
# -->
|
1747
|
-
# Returns
|
1979
|
+
# Returns whether `self` ends with any of the given `strings`.
|
1980
|
+
#
|
1981
|
+
# Returns `true` if any given string matches the end, `false` otherwise:
|
1748
1982
|
#
|
1749
|
-
#
|
1983
|
+
# 'hello'.end_with?('ello') #=> true
|
1984
|
+
# 'hello'.end_with?('heaven', 'ello') #=> true
|
1985
|
+
# 'hello'.end_with?('heaven', 'paradise') #=> false
|
1986
|
+
# 'тест'.end_with?('т') # => true
|
1987
|
+
# 'こんにちは'.end_with?('は') # => true
|
1750
1988
|
#
|
1751
|
-
#
|
1752
|
-
# "hello".end_with?("heaven", "ello") #=> true
|
1753
|
-
# "hello".end_with?("heaven", "paradise") #=> false
|
1989
|
+
# Related: String#start_with?.
|
1754
1990
|
#
|
1755
1991
|
def end_with?: (*string suffixes) -> bool
|
1756
1992
|
|
@@ -1774,11 +2010,29 @@ class String
|
|
1774
2010
|
|
1775
2011
|
# <!--
|
1776
2012
|
# rdoc-file=string.c
|
1777
|
-
# -
|
2013
|
+
# - force_encoding(encoding) -> self
|
1778
2014
|
# -->
|
1779
|
-
# Changes the encoding to `encoding
|
2015
|
+
# Changes the encoding of `self` to `encoding`, which may be a string encoding
|
2016
|
+
# name or an Encoding object; returns self:
|
2017
|
+
#
|
2018
|
+
# s = 'łał'
|
2019
|
+
# s.bytes # => [197, 130, 97, 197, 130]
|
2020
|
+
# s.encoding # => #<Encoding:UTF-8>
|
2021
|
+
# s.force_encoding('ascii') # => "\xC5\x82a\xC5\x82"
|
2022
|
+
# s.encoding # => #<Encoding:US-ASCII>
|
2023
|
+
#
|
2024
|
+
# Does not change the underlying bytes:
|
1780
2025
|
#
|
1781
|
-
|
2026
|
+
# s.bytes # => [197, 130, 97, 197, 130]
|
2027
|
+
#
|
2028
|
+
# Makes the change even if the given `encoding` is invalid for `self` (as is the
|
2029
|
+
# change above):
|
2030
|
+
#
|
2031
|
+
# s.valid_encoding? # => false
|
2032
|
+
# s.force_encoding(Encoding::UTF_8) # => "łał"
|
2033
|
+
# s.valid_encoding? # => true
|
2034
|
+
#
|
2035
|
+
def force_encoding: (encoding enc) -> self
|
1782
2036
|
|
1783
2037
|
# <!--
|
1784
2038
|
# rdoc-file=string.c
|
@@ -1789,13 +2043,15 @@ class String
|
|
1789
2043
|
|
1790
2044
|
# <!--
|
1791
2045
|
# rdoc-file=string.c
|
1792
|
-
# - getbyte(index) -> integer
|
2046
|
+
# - getbyte(index) -> integer or nil
|
1793
2047
|
# -->
|
1794
|
-
# Returns the byte at zero-based `index` as an integer
|
2048
|
+
# Returns the byte at zero-based `index` as an integer, or `nil` if `index` is
|
2049
|
+
# out of range:
|
1795
2050
|
#
|
1796
|
-
# s = 'abcde'
|
1797
|
-
# s.getbyte(0)
|
1798
|
-
# s.getbyte(1) # =>
|
2051
|
+
# s = 'abcde' # => "abcde"
|
2052
|
+
# s.getbyte(0) # => 97
|
2053
|
+
# s.getbyte(-1) # => 101
|
2054
|
+
# s.getbyte(5) # => nil
|
1799
2055
|
#
|
1800
2056
|
# Related: String#setbyte.
|
1801
2057
|
#
|
@@ -1803,15 +2059,19 @@ class String
|
|
1803
2059
|
|
1804
2060
|
# <!--
|
1805
2061
|
# rdoc-file=string.c
|
1806
|
-
# -
|
2062
|
+
# - grapheme_clusters -> array_of_grapheme_clusters
|
1807
2063
|
# -->
|
1808
|
-
# Returns an array of grapheme clusters in
|
1809
|
-
#
|
2064
|
+
# Returns an array of the grapheme clusters in `self` (see [Unicode Grapheme
|
2065
|
+
# Cluster
|
2066
|
+
# Boundaries](https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries)
|
2067
|
+
# ):
|
1810
2068
|
#
|
1811
|
-
#
|
1812
|
-
#
|
2069
|
+
# s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈"
|
2070
|
+
# s.grapheme_clusters
|
2071
|
+
# # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
|
1813
2072
|
#
|
1814
|
-
def grapheme_clusters: () ->
|
2073
|
+
def grapheme_clusters: () -> Array[String]
|
2074
|
+
| () { (String grapheme_cluter) -> void } -> self
|
1815
2075
|
|
1816
2076
|
# <!--
|
1817
2077
|
# rdoc-file=string.c
|
@@ -1821,16 +2081,15 @@ class String
|
|
1821
2081
|
# -->
|
1822
2082
|
# Returns a copy of `self` with all occurrences of the given `pattern` replaced.
|
1823
2083
|
#
|
1824
|
-
# See [Substitution Methods](
|
2084
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
1825
2085
|
#
|
1826
2086
|
# Returns an Enumerator if no `replacement` and no block given.
|
1827
2087
|
#
|
1828
2088
|
# Related: String#sub, String#sub!, String#gsub!.
|
1829
2089
|
#
|
1830
|
-
def gsub: (Regexp | string pattern, string replacement) -> String
|
1831
|
-
| (Regexp | string pattern
|
2090
|
+
def gsub: (Regexp | string pattern, string | hash[String, _ToS] replacement) -> String
|
2091
|
+
| (Regexp | string pattern) -> Enumerator[String, String]
|
1832
2092
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
1833
|
-
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
1834
2093
|
|
1835
2094
|
# <!--
|
1836
2095
|
# rdoc-file=string.c
|
@@ -1841,16 +2100,15 @@ class String
|
|
1841
2100
|
# Performs the specified substring replacement(s) on `self`; returns `self` if
|
1842
2101
|
# any replacement occurred, `nil` otherwise.
|
1843
2102
|
#
|
1844
|
-
# See [Substitution Methods](
|
2103
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
1845
2104
|
#
|
1846
2105
|
# Returns an Enumerator if no `replacement` and no block given.
|
1847
2106
|
#
|
1848
2107
|
# Related: String#sub, String#gsub, String#sub!.
|
1849
2108
|
#
|
1850
|
-
def gsub!: (Regexp | string pattern, string replacement) ->
|
1851
|
-
| (Regexp | string pattern
|
1852
|
-
| (Regexp | string pattern) { (String match) -> _ToS } ->
|
1853
|
-
| (Regexp | string pattern) -> ::Enumerator[String, self]
|
2109
|
+
def gsub!: (Regexp | string pattern, string | hash[String, _ToS] replacement) -> self?
|
2110
|
+
| (Regexp | string pattern) -> Enumerator[String, self?]
|
2111
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> self?
|
1854
2112
|
|
1855
2113
|
# <!--
|
1856
2114
|
# rdoc-file=string.c
|
@@ -1865,22 +2123,24 @@ class String
|
|
1865
2123
|
|
1866
2124
|
# <!--
|
1867
2125
|
# rdoc-file=string.c
|
1868
|
-
# -
|
2126
|
+
# - hex -> integer
|
1869
2127
|
# -->
|
1870
|
-
#
|
1871
|
-
# an optional sign and an optional `0x`) and returns the corresponding
|
1872
|
-
#
|
2128
|
+
# Interprets the leading substring of `self` as a string of hexadecimal digits
|
2129
|
+
# (with an optional sign and an optional `0x`) and returns the corresponding
|
2130
|
+
# number; returns zero if there is no such leading substring:
|
2131
|
+
#
|
2132
|
+
# '0x0a'.hex # => 10
|
2133
|
+
# '-1234'.hex # => -4660
|
2134
|
+
# '0'.hex # => 0
|
2135
|
+
# 'non-numeric'.hex # => 0
|
1873
2136
|
#
|
1874
|
-
#
|
1875
|
-
# "-1234".hex #=> -4660
|
1876
|
-
# "0".hex #=> 0
|
1877
|
-
# "wombat".hex #=> 0
|
2137
|
+
# Related: String#oct.
|
1878
2138
|
#
|
1879
2139
|
def hex: () -> Integer
|
1880
2140
|
|
1881
2141
|
# <!--
|
1882
2142
|
# rdoc-file=string.c
|
1883
|
-
# - include?
|
2143
|
+
# - include?(other_string) -> true or false
|
1884
2144
|
# -->
|
1885
2145
|
# Returns `true` if `self` contains `other_string`, `false` otherwise:
|
1886
2146
|
#
|
@@ -1889,46 +2149,53 @@ class String
|
|
1889
2149
|
# s.include?('fo') # => true
|
1890
2150
|
# s.include?('food') # => false
|
1891
2151
|
#
|
1892
|
-
def include?: (string
|
2152
|
+
def include?: (string other_string) -> bool
|
1893
2153
|
|
1894
2154
|
# <!--
|
1895
2155
|
# rdoc-file=string.c
|
1896
2156
|
# - index(substring, offset = 0) -> integer or nil
|
1897
2157
|
# - index(regexp, offset = 0) -> integer or nil
|
1898
2158
|
# -->
|
1899
|
-
# Returns the
|
1900
|
-
# `
|
2159
|
+
# Returns the integer index of the first match for the given argument, or `nil`
|
2160
|
+
# if none found; the search of `self` is forward, and begins at position
|
2161
|
+
# `offset` (in characters).
|
1901
2162
|
#
|
1902
|
-
#
|
1903
|
-
#
|
1904
|
-
# 'foo'.index('oo') # => 1
|
1905
|
-
# 'foo'.index('ooo') # => nil
|
2163
|
+
# With string argument `substring`, returns the index of the first matching
|
2164
|
+
# substring in `self`:
|
1906
2165
|
#
|
1907
|
-
#
|
1908
|
-
#
|
2166
|
+
# 'foo'.index('f') # => 0
|
2167
|
+
# 'foo'.index('o') # => 1
|
2168
|
+
# 'foo'.index('oo') # => 1
|
2169
|
+
# 'foo'.index('ooo') # => nil
|
2170
|
+
# 'тест'.index('с') # => 2
|
2171
|
+
# 'こんにちは'.index('ち') # => 3
|
1909
2172
|
#
|
1910
|
-
#
|
1911
|
-
# 'foo'.index(/o/) # => 1
|
1912
|
-
# 'foo'.index(/oo/) # => 1
|
1913
|
-
# 'foo'.index(/ooo/) # => nil
|
2173
|
+
# With Regexp argument `regexp`, returns the index of the first match in `self`:
|
1914
2174
|
#
|
1915
|
-
#
|
1916
|
-
#
|
2175
|
+
# 'foo'.index(/o./) # => 1
|
2176
|
+
# 'foo'.index(/.o/) # => 0
|
1917
2177
|
#
|
1918
|
-
#
|
1919
|
-
# 'foo'.index('o', 2) # => 2
|
1920
|
-
# 'foo'.index('o', 3) # => nil
|
2178
|
+
# With positive integer `offset`, begins the search at position `offset`:
|
1921
2179
|
#
|
1922
|
-
#
|
2180
|
+
# 'foo'.index('o', 1) # => 1
|
2181
|
+
# 'foo'.index('o', 2) # => 2
|
2182
|
+
# 'foo'.index('o', 3) # => nil
|
2183
|
+
# 'тест'.index('с', 1) # => 2
|
2184
|
+
# 'こんにちは'.index('ち', 2) # => 3
|
2185
|
+
#
|
2186
|
+
# With negative integer `offset`, selects the search position by counting
|
2187
|
+
# backward from the end of `self`:
|
1923
2188
|
#
|
1924
|
-
# 'foo'.index('o', -1)
|
1925
|
-
# 'foo'.index('o', -2)
|
1926
|
-
# 'foo'.index('o', -3)
|
1927
|
-
# 'foo'.index('o', -4)
|
2189
|
+
# 'foo'.index('o', -1) # => 2
|
2190
|
+
# 'foo'.index('o', -2) # => 1
|
2191
|
+
# 'foo'.index('o', -3) # => 1
|
2192
|
+
# 'foo'.index('o', -4) # => nil
|
2193
|
+
# 'foo'.index(/o./, -2) # => 1
|
2194
|
+
# 'foo'.index(/.o/, -2) # => 1
|
1928
2195
|
#
|
1929
2196
|
# Related: String#rindex.
|
1930
2197
|
#
|
1931
|
-
def index: (Regexp | string
|
2198
|
+
def index: (Regexp | string pattern, ?int offset) -> Integer?
|
1932
2199
|
|
1933
2200
|
# <!--
|
1934
2201
|
# rdoc-file=string.c
|
@@ -1945,7 +2212,7 @@ class String
|
|
1945
2212
|
#
|
1946
2213
|
# 'foo'.insert(-2, 'bar') # => "fobaro"
|
1947
2214
|
#
|
1948
|
-
def insert: (int index, string other_str) ->
|
2215
|
+
def insert: (int index, string other_str) -> self
|
1949
2216
|
|
1950
2217
|
# <!--
|
1951
2218
|
# rdoc-file=string.c
|
@@ -1955,7 +2222,6 @@ class String
|
|
1955
2222
|
# special characters escaped:
|
1956
2223
|
#
|
1957
2224
|
# s = "foo\tbar\tbaz\n"
|
1958
|
-
# # => "foo\tbar\tbaz\n"
|
1959
2225
|
# s.inspect
|
1960
2226
|
# # => "\"foo\\tbar\\tbaz\\n\""
|
1961
2227
|
#
|
@@ -1966,7 +2232,7 @@ class String
|
|
1966
2232
|
# - str.intern -> symbol
|
1967
2233
|
# - str.to_sym -> symbol
|
1968
2234
|
# -->
|
1969
|
-
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
2235
|
+
# Returns the `Symbol` corresponding to *str*, creating the symbol if it did not
|
1970
2236
|
# previously exist. See Symbol#id2name.
|
1971
2237
|
#
|
1972
2238
|
# "Koala".intern #=> :Koala
|
@@ -1988,74 +2254,77 @@ class String
|
|
1988
2254
|
# -->
|
1989
2255
|
# Returns the count of characters (not bytes) in `self`:
|
1990
2256
|
#
|
1991
|
-
#
|
1992
|
-
#
|
2257
|
+
# 'foo'.length # => 3
|
2258
|
+
# 'тест'.length # => 4
|
2259
|
+
# 'こんにちは'.length # => 5
|
1993
2260
|
#
|
1994
|
-
#
|
2261
|
+
# Contrast with String#bytesize:
|
1995
2262
|
#
|
1996
|
-
#
|
2263
|
+
# 'foo'.bytesize # => 3
|
2264
|
+
# 'тест'.bytesize # => 8
|
2265
|
+
# 'こんにちは'.bytesize # => 15
|
1997
2266
|
#
|
1998
2267
|
def length: () -> Integer
|
1999
2268
|
|
2000
2269
|
# <!--
|
2001
2270
|
# rdoc-file=string.c
|
2002
|
-
# -
|
2271
|
+
# - lines(Line_sep = $/, chomp: false) -> array_of_strings
|
2003
2272
|
# -->
|
2004
|
-
#
|
2005
|
-
#
|
2006
|
-
# getline_args).to_a`.
|
2007
|
-
#
|
2008
|
-
# If `chomp` is `true`, `separator` will be removed from the end of each line.
|
2009
|
-
#
|
2010
|
-
# "hello\nworld\n".lines #=> ["hello\n", "world\n"]
|
2011
|
-
# "hello world".lines(' ') #=> ["hello ", " ", "world"]
|
2012
|
-
# "hello\nworld\n".lines(chomp: true) #=> ["hello", "world"]
|
2273
|
+
# Forms substrings ("lines") of `self` according to the given arguments (see
|
2274
|
+
# String#each_line for details); returns the lines in an array.
|
2013
2275
|
#
|
2014
|
-
|
2015
|
-
|
2016
|
-
#
|
2017
|
-
def lines: (?string separator, ?chomp: boolish) -> Array[String]
|
2276
|
+
def lines: (?string? separator, ?chomp: boolish) -> Array[String]
|
2277
|
+
| (?string? separator, ?chomp: boolish) { (String line) -> void } -> self
|
2018
2278
|
|
2019
2279
|
# <!--
|
2020
2280
|
# rdoc-file=string.c
|
2021
|
-
# -
|
2281
|
+
# - ljust(size, pad_string = ' ') -> new_string
|
2022
2282
|
# -->
|
2023
|
-
#
|
2024
|
-
#
|
2025
|
-
#
|
2283
|
+
# Returns a left-justified copy of `self`.
|
2284
|
+
#
|
2285
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
2286
|
+
# returns a new string of length `size` that is a copy of `self`, left justified
|
2287
|
+
# and padded on the right with `pad_string`:
|
2288
|
+
#
|
2289
|
+
# 'hello'.ljust(10) # => "hello "
|
2290
|
+
# ' hello'.ljust(10) # => " hello "
|
2291
|
+
# 'hello'.ljust(10, 'ab') # => "helloababa"
|
2292
|
+
# 'тест'.ljust(10) # => "тест "
|
2293
|
+
# 'こんにちは'.ljust(10) # => "こんにちは "
|
2026
2294
|
#
|
2027
|
-
#
|
2028
|
-
# "hello".ljust(20) #=> "hello "
|
2029
|
-
# "hello".ljust(20, '1234') #=> "hello123412341234123"
|
2295
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
2030
2296
|
#
|
2031
|
-
|
2297
|
+
# 'hello'.ljust(5) # => "hello"
|
2298
|
+
# 'hello'.ljust(1) # => "hello"
|
2299
|
+
#
|
2300
|
+
# Related: String#rjust, String#center.
|
2301
|
+
#
|
2302
|
+
def ljust: (int size, ?string pad_string) -> String
|
2032
2303
|
|
2033
2304
|
# <!--
|
2034
2305
|
# rdoc-file=string.c
|
2035
|
-
# -
|
2306
|
+
# - lstrip -> new_string
|
2036
2307
|
# -->
|
2037
|
-
# Returns a copy of
|
2038
|
-
# String
|
2308
|
+
# Returns a copy of `self` with leading whitespace removed; see [Whitespace in
|
2309
|
+
# Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2039
2310
|
#
|
2040
|
-
#
|
2311
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2312
|
+
# s = whitespace + 'abc' + whitespace
|
2313
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2314
|
+
# s.lstrip # => "abc\u0000\t\n\v\f\r "
|
2041
2315
|
#
|
2042
|
-
#
|
2043
|
-
# "hello".lstrip #=> "hello"
|
2316
|
+
# Related: String#rstrip, String#strip.
|
2044
2317
|
#
|
2045
2318
|
def lstrip: () -> String
|
2046
2319
|
|
2047
2320
|
# <!--
|
2048
2321
|
# rdoc-file=string.c
|
2049
|
-
# -
|
2322
|
+
# - lstrip! -> self or nil
|
2050
2323
|
# -->
|
2051
|
-
#
|
2052
|
-
# `
|
2324
|
+
# Like String#lstrip, except that any modifications are made in `self`; returns
|
2325
|
+
# `self` if any modification are made, `nil` otherwise.
|
2053
2326
|
#
|
2054
|
-
#
|
2055
|
-
#
|
2056
|
-
# " hello ".lstrip! #=> "hello "
|
2057
|
-
# "hello ".lstrip! #=> nil
|
2058
|
-
# "hello".lstrip! #=> nil
|
2327
|
+
# Related: String#rstrip!, String#strip!.
|
2059
2328
|
#
|
2060
2329
|
def lstrip!: () -> self?
|
2061
2330
|
|
@@ -2064,10 +2333,9 @@ class String
|
|
2064
2333
|
# - match(pattern, offset = 0) -> matchdata or nil
|
2065
2334
|
# - match(pattern, offset = 0) {|matchdata| ... } -> object
|
2066
2335
|
# -->
|
2067
|
-
# Returns a
|
2336
|
+
# Returns a MatchData object (or `nil`) based on `self` and the given `pattern`.
|
2068
2337
|
#
|
2069
|
-
# Note: also updates
|
2070
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2338
|
+
# Note: also updates Regexp@Global+Variables.
|
2071
2339
|
#
|
2072
2340
|
# * Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2073
2341
|
# regexp = Regexp.new(pattern)
|
@@ -2076,7 +2344,6 @@ class String
|
|
2076
2344
|
# (see Regexp#match):
|
2077
2345
|
# matchdata = <tt>regexp.match(self)
|
2078
2346
|
#
|
2079
|
-
#
|
2080
2347
|
# With no block given, returns the computed `matchdata`:
|
2081
2348
|
#
|
2082
2349
|
# 'foo'.match('f') # => #<MatchData "f">
|
@@ -2095,8 +2362,8 @@ class String
|
|
2095
2362
|
# 'foo'.match(/x/) {|matchdata| matchdata } # => nil
|
2096
2363
|
# 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
|
2097
2364
|
#
|
2098
|
-
def match: (Regexp | string pattern, ?int
|
2099
|
-
| [
|
2365
|
+
def match: (Regexp | string pattern, ?int offset) -> MatchData?
|
2366
|
+
| [T] (Regexp | string pattern, ?int offset) { (MatchData matchdata) -> T } -> T?
|
2100
2367
|
|
2101
2368
|
# <!--
|
2102
2369
|
# rdoc-file=string.c
|
@@ -2105,13 +2372,12 @@ class String
|
|
2105
2372
|
# Returns `true` or `false` based on whether a match is found for `self` and
|
2106
2373
|
# `pattern`.
|
2107
2374
|
#
|
2108
|
-
# Note: does not update
|
2109
|
-
# variables](Regexp.html#class-Regexp-label-Special+global+variables).
|
2375
|
+
# Note: does not update Regexp@Global+Variables.
|
2110
2376
|
#
|
2111
2377
|
# Computes `regexp` by converting `pattern` (if not already a Regexp).
|
2112
2378
|
# regexp = Regexp.new(pattern)
|
2113
2379
|
#
|
2114
|
-
# Returns `true` if `self+.match(regexp)` returns a
|
2380
|
+
# Returns `true` if `self+.match(regexp)` returns a MatchData object, `false`
|
2115
2381
|
# otherwise:
|
2116
2382
|
#
|
2117
2383
|
# 'foo'.match?(/o/) # => true
|
@@ -2122,7 +2388,7 @@ class String
|
|
2122
2388
|
# 'foo'.match?('f', 1) # => false
|
2123
2389
|
# 'foo'.match?('o', 1) # => true
|
2124
2390
|
#
|
2125
|
-
def match?: (Regexp | string pattern, ?int
|
2391
|
+
def match?: (Regexp | string pattern, ?int offset) -> bool
|
2126
2392
|
|
2127
2393
|
# <!-- rdoc-file=string.c -->
|
2128
2394
|
# Returns the successor to `self`. The successor is calculated by incrementing
|
@@ -2172,62 +2438,78 @@ class String
|
|
2172
2438
|
# s = '99zz99zz'
|
2173
2439
|
# s.succ # => "100aa00aa"
|
2174
2440
|
#
|
2175
|
-
# The successor to an empty String is a new empty String
|
2441
|
+
# The successor to an empty `String` is a new empty `String`:
|
2176
2442
|
#
|
2177
2443
|
# ''.succ # => ""
|
2178
2444
|
#
|
2179
|
-
|
2180
|
-
#
|
2181
|
-
def next: () -> String
|
2445
|
+
alias next succ
|
2182
2446
|
|
2183
2447
|
# <!-- rdoc-file=string.c -->
|
2184
2448
|
# Equivalent to String#succ, but modifies `self` in place; returns `self`.
|
2185
2449
|
#
|
2186
|
-
|
2187
|
-
#
|
2188
|
-
def next!: () -> self
|
2450
|
+
alias next! succ!
|
2189
2451
|
|
2190
2452
|
# <!--
|
2191
2453
|
# rdoc-file=string.c
|
2192
|
-
# -
|
2454
|
+
# - oct -> integer
|
2193
2455
|
# -->
|
2194
|
-
#
|
2195
|
-
# optional sign) and returns the corresponding number
|
2196
|
-
#
|
2456
|
+
# Interprets the leading substring of `self` as a string of octal digits (with
|
2457
|
+
# an optional sign) and returns the corresponding number; returns zero if there
|
2458
|
+
# is no such leading substring:
|
2197
2459
|
#
|
2198
|
-
#
|
2199
|
-
#
|
2200
|
-
#
|
2201
|
-
#
|
2460
|
+
# '123'.oct # => 83
|
2461
|
+
# '-377'.oct # => -255
|
2462
|
+
# '0377non-numeric'.oct # => 255
|
2463
|
+
# 'non-numeric'.oct # => 0
|
2202
2464
|
#
|
2203
|
-
# If `
|
2465
|
+
# If `self` starts with `0`, radix indicators are honored; see Kernel#Integer.
|
2466
|
+
#
|
2467
|
+
# Related: String#hex.
|
2204
2468
|
#
|
2205
2469
|
def oct: () -> Integer
|
2206
2470
|
|
2207
2471
|
# <!--
|
2208
2472
|
# rdoc-file=string.c
|
2209
|
-
# -
|
2473
|
+
# - ord -> integer
|
2210
2474
|
# -->
|
2211
|
-
# Returns the
|
2475
|
+
# Returns the integer ordinal of the first character of `self`:
|
2212
2476
|
#
|
2213
|
-
#
|
2477
|
+
# 'h'.ord # => 104
|
2478
|
+
# 'hello'.ord # => 104
|
2479
|
+
# 'тест'.ord # => 1090
|
2480
|
+
# 'こんにちは'.ord # => 12371
|
2214
2481
|
#
|
2215
2482
|
def ord: () -> Integer
|
2216
2483
|
|
2217
2484
|
# <!--
|
2218
2485
|
# rdoc-file=string.c
|
2219
|
-
# -
|
2220
|
-
# - str.partition(regexp) -> [head, match, tail]
|
2486
|
+
# - partition(string_or_regexp) -> [head, match, tail]
|
2221
2487
|
# -->
|
2222
|
-
#
|
2223
|
-
#
|
2224
|
-
#
|
2488
|
+
# Returns a 3-element array of substrings of `self`.
|
2489
|
+
#
|
2490
|
+
# Matches a pattern against `self`, scanning from the beginning. The pattern is:
|
2225
2491
|
#
|
2226
|
-
#
|
2227
|
-
#
|
2228
|
-
# "hello".partition(/.l/) #=> ["h", "el", "lo"]
|
2492
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2493
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2229
2494
|
#
|
2230
|
-
|
2495
|
+
# If the pattern is matched, returns pre-match, first-match, post-match:
|
2496
|
+
#
|
2497
|
+
# 'hello'.partition('l') # => ["he", "l", "lo"]
|
2498
|
+
# 'hello'.partition('ll') # => ["he", "ll", "o"]
|
2499
|
+
# 'hello'.partition('h') # => ["", "h", "ello"]
|
2500
|
+
# 'hello'.partition('o') # => ["hell", "o", ""]
|
2501
|
+
# 'hello'.partition(/l+/) #=> ["he", "ll", "o"]
|
2502
|
+
# 'hello'.partition('') # => ["", "", "hello"]
|
2503
|
+
# 'тест'.partition('т') # => ["", "т", "ест"]
|
2504
|
+
# 'こんにちは'.partition('に') # => ["こん", "に", "ちは"]
|
2505
|
+
#
|
2506
|
+
# If the pattern is not matched, returns a copy of `self` and two empty strings:
|
2507
|
+
#
|
2508
|
+
# 'hello'.partition('x') # => ["hello", "", ""]
|
2509
|
+
#
|
2510
|
+
# Related: String#rpartition, String#split.
|
2511
|
+
#
|
2512
|
+
def partition: (Regexp | string pattern) -> [String, String, String]
|
2231
2513
|
|
2232
2514
|
# <!--
|
2233
2515
|
# rdoc-file=string.c
|
@@ -2241,7 +2523,7 @@ class String
|
|
2241
2523
|
#
|
2242
2524
|
# Related: String#concat.
|
2243
2525
|
#
|
2244
|
-
def prepend: (*string
|
2526
|
+
def prepend: (*string other_strings) -> self
|
2245
2527
|
|
2246
2528
|
# <!-- rdoc-file=string.c -->
|
2247
2529
|
# Replaces the contents of `self` with the contents of `other_string`:
|
@@ -2249,7 +2531,7 @@ class String
|
|
2249
2531
|
# s = 'foo' # => "foo"
|
2250
2532
|
# s.replace('bar') # => "bar"
|
2251
2533
|
#
|
2252
|
-
def replace: (string
|
2534
|
+
def replace: (string other_string) -> self
|
2253
2535
|
|
2254
2536
|
# <!--
|
2255
2537
|
# rdoc-file=string.c
|
@@ -2311,13 +2593,12 @@ class String
|
|
2311
2593
|
# $~ #=> #<MatchData "oo">
|
2312
2594
|
#
|
2313
2595
|
# Integer argument `offset`, if given and non-negative, specifies the maximum
|
2314
|
-
# starting position in the
|
2315
|
-
# string to _end_ the search:
|
2596
|
+
# starting position in the string to *end* the search:
|
2316
2597
|
#
|
2317
|
-
#
|
2318
|
-
#
|
2319
|
-
#
|
2320
|
-
#
|
2598
|
+
# 'foo'.rindex('o', 0) # => nil
|
2599
|
+
# 'foo'.rindex('o', 1) # => 1
|
2600
|
+
# 'foo'.rindex('o', 2) # => 2
|
2601
|
+
# 'foo'.rindex('o', 3) # => 2
|
2321
2602
|
#
|
2322
2603
|
# If `offset` is a negative Integer, the maximum starting position in the string
|
2323
2604
|
# to *end* the search is the sum of the string's length and `offset`:
|
@@ -2329,146 +2610,176 @@ class String
|
|
2329
2610
|
#
|
2330
2611
|
# Related: String#index.
|
2331
2612
|
#
|
2332
|
-
def rindex: (
|
2613
|
+
def rindex: (Regexp | string pattern, ?int offset) -> Integer?
|
2333
2614
|
|
2334
2615
|
# <!--
|
2335
2616
|
# rdoc-file=string.c
|
2336
|
-
# -
|
2617
|
+
# - rjust(size, pad_string = ' ') -> new_string
|
2337
2618
|
# -->
|
2338
|
-
#
|
2339
|
-
#
|
2340
|
-
#
|
2619
|
+
# Returns a right-justified copy of `self`.
|
2620
|
+
#
|
2621
|
+
# If integer argument `size` is greater than the size (in characters) of `self`,
|
2622
|
+
# returns a new string of length `size` that is a copy of `self`, right
|
2623
|
+
# justified and padded on the left with `pad_string`:
|
2624
|
+
#
|
2625
|
+
# 'hello'.rjust(10) # => " hello"
|
2626
|
+
# 'hello '.rjust(10) # => " hello "
|
2627
|
+
# 'hello'.rjust(10, 'ab') # => "ababahello"
|
2628
|
+
# 'тест'.rjust(10) # => " тест"
|
2629
|
+
# 'こんにちは'.rjust(10) # => " こんにちは"
|
2341
2630
|
#
|
2342
|
-
#
|
2343
|
-
# "hello".rjust(20) #=> " hello"
|
2344
|
-
# "hello".rjust(20, '1234') #=> "123412341234123hello"
|
2631
|
+
# If `size` is not greater than the size of `self`, returns a copy of `self`:
|
2345
2632
|
#
|
2346
|
-
|
2633
|
+
# 'hello'.rjust(5, 'ab') # => "hello"
|
2634
|
+
# 'hello'.rjust(1, 'ab') # => "hello"
|
2635
|
+
#
|
2636
|
+
# Related: String#ljust, String#center.
|
2637
|
+
#
|
2638
|
+
def rjust: (int size, ?string pad_string) -> String
|
2347
2639
|
|
2348
2640
|
# <!--
|
2349
2641
|
# rdoc-file=string.c
|
2350
|
-
# -
|
2351
|
-
# - str.rpartition(regexp) -> [head, match, tail]
|
2642
|
+
# - rpartition(sep) -> [head, match, tail]
|
2352
2643
|
# -->
|
2353
|
-
#
|
2354
|
-
# and returns the part before it, the match, and the part after it. If it is not
|
2355
|
-
# found, returns two empty strings and *str*.
|
2644
|
+
# Returns a 3-element array of substrings of `self`.
|
2356
2645
|
#
|
2357
|
-
#
|
2358
|
-
#
|
2359
|
-
# "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
|
2646
|
+
# Matches a pattern against `self`, scanning backwards from the end. The pattern
|
2647
|
+
# is:
|
2360
2648
|
#
|
2361
|
-
#
|
2362
|
-
#
|
2649
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2650
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2363
2651
|
#
|
2364
|
-
#
|
2652
|
+
# If the pattern is matched, returns pre-match, last-match, post-match:
|
2365
2653
|
#
|
2366
|
-
#
|
2367
|
-
#
|
2654
|
+
# 'hello'.rpartition('l') # => ["hel", "l", "o"]
|
2655
|
+
# 'hello'.rpartition('ll') # => ["he", "ll", "o"]
|
2656
|
+
# 'hello'.rpartition('h') # => ["", "h", "ello"]
|
2657
|
+
# 'hello'.rpartition('o') # => ["hell", "o", ""]
|
2658
|
+
# 'hello'.rpartition(/l+/) # => ["hel", "l", "o"]
|
2659
|
+
# 'hello'.rpartition('') # => ["hello", "", ""]
|
2660
|
+
# 'тест'.rpartition('т') # => ["тес", "т", ""]
|
2661
|
+
# 'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
|
2368
2662
|
#
|
2369
|
-
#
|
2663
|
+
# If the pattern is not matched, returns two empty strings and a copy of `self`:
|
2370
2664
|
#
|
2371
|
-
#
|
2665
|
+
# 'hello'.rpartition('x') # => ["", "", "hello"]
|
2372
2666
|
#
|
2373
|
-
#
|
2667
|
+
# Related: String#partition, String#split.
|
2374
2668
|
#
|
2375
|
-
def rpartition: (
|
2669
|
+
def rpartition: (Regexp | string pattern) -> [String, String, String]
|
2376
2670
|
|
2377
2671
|
# <!--
|
2378
2672
|
# rdoc-file=string.c
|
2379
|
-
# -
|
2673
|
+
# - rstrip -> new_string
|
2380
2674
|
# -->
|
2381
|
-
# Returns a copy of the receiver with trailing whitespace removed
|
2382
|
-
#
|
2675
|
+
# Returns a copy of the receiver with trailing whitespace removed; see
|
2676
|
+
# [Whitespace in Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2383
2677
|
#
|
2384
|
-
#
|
2678
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2679
|
+
# s = whitespace + 'abc' + whitespace
|
2680
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2681
|
+
# s.rstrip # => "\u0000\t\n\v\f\r abc"
|
2385
2682
|
#
|
2386
|
-
#
|
2387
|
-
# "hello".rstrip #=> "hello"
|
2683
|
+
# Related: String#lstrip, String#strip.
|
2388
2684
|
#
|
2389
2685
|
def rstrip: () -> String
|
2390
2686
|
|
2391
2687
|
# <!--
|
2392
2688
|
# rdoc-file=string.c
|
2393
|
-
# -
|
2689
|
+
# - rstrip! -> self or nil
|
2394
2690
|
# -->
|
2395
|
-
#
|
2396
|
-
#
|
2691
|
+
# Like String#rstrip, except that any modifications are made in `self`; returns
|
2692
|
+
# `self` if any modification are made, `nil` otherwise.
|
2397
2693
|
#
|
2398
|
-
#
|
2399
|
-
#
|
2400
|
-
# " hello ".rstrip! #=> " hello"
|
2401
|
-
# " hello".rstrip! #=> nil
|
2402
|
-
# "hello".rstrip! #=> nil
|
2694
|
+
# Related: String#lstrip!, String#strip!.
|
2403
2695
|
#
|
2404
2696
|
def rstrip!: () -> self?
|
2405
2697
|
|
2406
2698
|
# <!--
|
2407
2699
|
# rdoc-file=string.c
|
2408
|
-
# -
|
2409
|
-
# -
|
2700
|
+
# - scan(string_or_regexp) -> array
|
2701
|
+
# - scan(string_or_regexp) {|matches| ... } -> self
|
2410
2702
|
# -->
|
2411
|
-
#
|
2412
|
-
#
|
2413
|
-
#
|
2414
|
-
#
|
2415
|
-
# contains groups, each individual result is itself an array containing one
|
2416
|
-
# entry per group.
|
2703
|
+
# Matches a pattern against `self`; the pattern is:
|
2704
|
+
#
|
2705
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2706
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2417
2707
|
#
|
2418
|
-
#
|
2419
|
-
# a.scan(/\w+/) #=> ["cruel", "world"]
|
2420
|
-
# a.scan(/.../) #=> ["cru", "el ", "wor"]
|
2421
|
-
# a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
|
2422
|
-
# a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]
|
2708
|
+
# Iterates through `self`, generating a collection of matching results:
|
2423
2709
|
#
|
2424
|
-
#
|
2710
|
+
# * If the pattern contains no groups, each result is the matched string,
|
2711
|
+
# `$&`.
|
2712
|
+
# * If the pattern contains groups, each result is an array containing one
|
2713
|
+
# entry per group.
|
2425
2714
|
#
|
2426
|
-
#
|
2715
|
+
# With no block given, returns an array of the results:
|
2716
|
+
#
|
2717
|
+
# s = 'cruel world'
|
2718
|
+
# s.scan(/\w+/) # => ["cruel", "world"]
|
2719
|
+
# s.scan(/.../) # => ["cru", "el ", "wor"]
|
2720
|
+
# s.scan(/(...)/) # => [["cru"], ["el "], ["wor"]]
|
2721
|
+
# s.scan(/(..)(..)/) # => [["cr", "ue"], ["l ", "wo"]]
|
2722
|
+
#
|
2723
|
+
# With a block given, calls the block with each result; returns `self`:
|
2724
|
+
#
|
2725
|
+
# s.scan(/\w+/) {|w| print "<<#{w}>> " }
|
2427
2726
|
# print "\n"
|
2428
|
-
#
|
2727
|
+
# s.scan(/(.)(.)/) {|x,y| print y, x }
|
2429
2728
|
# print "\n"
|
2430
2729
|
#
|
2431
|
-
#
|
2730
|
+
# Output:
|
2432
2731
|
#
|
2433
2732
|
# <<cruel>> <<world>>
|
2434
2733
|
# rceu lowlr
|
2435
2734
|
#
|
2436
|
-
def scan: (Regexp
|
2437
|
-
| (Regexp
|
2735
|
+
def scan: (Regexp pattern) -> Array[String | Array[String?]]
|
2736
|
+
| (Regexp pattern) { (String | Array[String?] matches) -> void } -> self
|
2737
|
+
| (string pattern) -> Array[String]
|
2738
|
+
| (string pattern) { (String match) -> void } -> self
|
2438
2739
|
|
2439
2740
|
# <!--
|
2440
2741
|
# rdoc-file=string.c
|
2441
|
-
# -
|
2442
|
-
# -
|
2443
|
-
# - str.scrub{|bytes|} -> new_str
|
2742
|
+
# - scrub(replacement_string = default_replacement) -> new_string
|
2743
|
+
# - scrub{|bytes| ... } -> new_string
|
2444
2744
|
# -->
|
2445
|
-
#
|
2446
|
-
#
|
2447
|
-
#
|
2745
|
+
# Returns a copy of `self` with each invalid byte sequence replaced by the given
|
2746
|
+
# `replacement_string`.
|
2747
|
+
#
|
2748
|
+
# With no block given and no argument, replaces each invalid sequence with the
|
2749
|
+
# default replacement string (`"�"` for a Unicode encoding, `'?'` otherwise):
|
2750
|
+
#
|
2751
|
+
# s = "foo\x81\x81bar"
|
2752
|
+
# s.scrub # => "foo��bar"
|
2753
|
+
#
|
2754
|
+
# With no block given and argument `replacement_string` given, replaces each
|
2755
|
+
# invalid sequence with that string:
|
2756
|
+
#
|
2757
|
+
# "foo\x81\x81bar".scrub('xyzzy') # => "fooxyzzyxyzzybar"
|
2758
|
+
#
|
2759
|
+
# With a block given, replaces each invalid sequence with the value of the
|
2760
|
+
# block:
|
2761
|
+
#
|
2762
|
+
# "foo\x81\x81bar".scrub {|bytes| p bytes; 'XYZZY' }
|
2763
|
+
# # => "fooXYZZYXYZZYbar"
|
2764
|
+
#
|
2765
|
+
# Output:
|
2448
2766
|
#
|
2449
|
-
# "
|
2450
|
-
# "
|
2451
|
-
# "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
|
2767
|
+
# "\x81"
|
2768
|
+
# "\x81"
|
2452
2769
|
#
|
2453
|
-
def scrub: (?string
|
2454
|
-
| () { (String bytes) -> string } -> String
|
2770
|
+
def scrub: (?string? replacement) -> String
|
2771
|
+
| (?nil) { (String bytes) -> string } -> String
|
2455
2772
|
|
2456
2773
|
# <!--
|
2457
2774
|
# rdoc-file=string.c
|
2458
|
-
# -
|
2459
|
-
# -
|
2460
|
-
# -
|
2775
|
+
# - scrub! -> self
|
2776
|
+
# - scrub!(replacement_string = default_replacement) -> self
|
2777
|
+
# - scrub!{|bytes| ... } -> self
|
2461
2778
|
# -->
|
2462
|
-
#
|
2463
|
-
# replacement character, else returns self. If block is given, replace invalid
|
2464
|
-
# bytes with returned value of the block.
|
2465
|
-
#
|
2466
|
-
# "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
|
2467
|
-
# "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
|
2468
|
-
# "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack1('H*')+'>' } #=> "abc\u3042<e380>"
|
2779
|
+
# Like String#scrub, except that any replacements are made in `self`.
|
2469
2780
|
#
|
2470
|
-
def scrub!: (?string
|
2471
|
-
| () { (String bytes) -> string } -> self
|
2781
|
+
def scrub!: (?string? replacement) -> self
|
2782
|
+
| (?nil) { (String bytes) -> string } -> self
|
2472
2783
|
|
2473
2784
|
# <!--
|
2474
2785
|
# rdoc-file=string.c
|
@@ -2482,101 +2793,26 @@ class String
|
|
2482
2793
|
#
|
2483
2794
|
# Related: String#getbyte.
|
2484
2795
|
#
|
2485
|
-
def setbyte: (int index,
|
2796
|
+
def setbyte: [T < _ToInt] (int index, T byte) -> T
|
2486
2797
|
|
2487
2798
|
# <!-- rdoc-file=string.c -->
|
2488
2799
|
# Returns the count of characters (not bytes) in `self`:
|
2489
2800
|
#
|
2490
|
-
#
|
2491
|
-
#
|
2801
|
+
# 'foo'.length # => 3
|
2802
|
+
# 'тест'.length # => 4
|
2803
|
+
# 'こんにちは'.length # => 5
|
2492
2804
|
#
|
2493
|
-
#
|
2805
|
+
# Contrast with String#bytesize:
|
2494
2806
|
#
|
2495
|
-
#
|
2807
|
+
# 'foo'.bytesize # => 3
|
2808
|
+
# 'тест'.bytesize # => 8
|
2809
|
+
# 'こんにちは'.bytesize # => 15
|
2496
2810
|
#
|
2497
2811
|
alias size length
|
2498
2812
|
|
2499
2813
|
# <!-- rdoc-file=string.c -->
|
2500
|
-
# Returns the substring of `self` specified by the arguments.
|
2501
|
-
#
|
2502
|
-
# When the single Integer argument `index` is given, returns the 1-character
|
2503
|
-
# substring found in `self` at offset `index`:
|
2504
|
-
#
|
2505
|
-
# 'bar'[2] # => "r"
|
2506
|
-
#
|
2507
|
-
# Counts backward from the end of `self` if `index` is negative:
|
2508
|
-
#
|
2509
|
-
# 'foo'[-3] # => "f"
|
2510
|
-
#
|
2511
|
-
# Returns `nil` if `index` is out of range:
|
2512
|
-
#
|
2513
|
-
# 'foo'[3] # => nil
|
2514
|
-
# 'foo'[-4] # => nil
|
2515
|
-
#
|
2516
|
-
# When the two Integer arguments `start` and `length` are given, returns the
|
2517
|
-
# substring of the given `length` found in `self` at offset `start`:
|
2518
|
-
#
|
2519
|
-
# 'foo'[0, 2] # => "fo"
|
2520
|
-
# 'foo'[0, 0] # => ""
|
2521
|
-
#
|
2522
|
-
# Counts backward from the end of `self` if `start` is negative:
|
2523
|
-
#
|
2524
|
-
# 'foo'[-2, 2] # => "oo"
|
2525
|
-
#
|
2526
|
-
# Special case: returns a new empty String if `start` is equal to the length of
|
2527
|
-
# `self`:
|
2528
|
-
#
|
2529
|
-
# 'foo'[3, 2] # => ""
|
2530
|
-
#
|
2531
|
-
# Returns `nil` if `start` is out of range:
|
2532
|
-
#
|
2533
|
-
# 'foo'[4, 2] # => nil
|
2534
|
-
# 'foo'[-4, 2] # => nil
|
2535
|
-
#
|
2536
|
-
# Returns the trailing substring of `self` if `length` is large:
|
2537
|
-
#
|
2538
|
-
# 'foo'[1, 50] # => "oo"
|
2539
|
-
#
|
2540
|
-
# Returns `nil` if `length` is negative:
|
2541
|
-
#
|
2542
|
-
# 'foo'[0, -1] # => nil
|
2543
|
-
#
|
2544
|
-
# When the single Range argument `range` is given, derives `start` and `length`
|
2545
|
-
# values from the given `range`, and returns values as above:
|
2546
|
-
#
|
2547
|
-
# * `'foo'[0..1]` is equivalent to `'foo'[0, 2]`.
|
2548
|
-
# * `'foo'[0...1]` is equivalent to `'foo'[0, 1]`.
|
2549
|
-
#
|
2550
|
-
#
|
2551
|
-
# When the Regexp argument `regexp` is given, and the `capture` argument is `0`,
|
2552
|
-
# returns the first matching substring found in `self`, or `nil` if none found:
|
2553
|
-
#
|
2554
|
-
# 'foo'[/o/] # => "o"
|
2555
|
-
# 'foo'[/x/] # => nil
|
2556
|
-
# s = 'hello there'
|
2557
|
-
# s[/[aeiou](.)\1/] # => "ell"
|
2558
|
-
# s[/[aeiou](.)\1/, 0] # => "ell"
|
2559
|
-
#
|
2560
|
-
# If argument `capture` is given and not `0`, it should be either an Integer
|
2561
|
-
# capture group index or a String or Symbol capture group name; the method call
|
2562
|
-
# returns only the specified capture (see [Regexp
|
2563
|
-
# Capturing](Regexp.html#class-Regexp-label-Capturing)):
|
2564
|
-
#
|
2565
|
-
# s = 'hello there'
|
2566
|
-
# s[/[aeiou](.)\1/, 1] # => "l"
|
2567
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
|
2568
|
-
# s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
|
2569
|
-
#
|
2570
|
-
# If an invalid capture group index is given, `nil` is returned. If an invalid
|
2571
|
-
# capture group name is given, `IndexError` is raised.
|
2572
|
-
#
|
2573
|
-
# When the single String argument `substring` is given, returns the substring
|
2574
|
-
# from `self` if found, otherwise `nil`:
|
2575
|
-
#
|
2576
|
-
# 'foo'['oo'] # => "oo"
|
2577
|
-
# 'foo'['xx'] # => nil
|
2578
|
-
#
|
2579
|
-
# String#slice is an alias for String#[].
|
2814
|
+
# Returns the substring of `self` specified by the arguments. See examples at
|
2815
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
2580
2816
|
#
|
2581
2817
|
alias slice []
|
2582
2818
|
|
@@ -2588,10 +2824,8 @@ class String
|
|
2588
2824
|
# - slice!(regexp, capture = 0) -> new_string or nil
|
2589
2825
|
# - slice!(substring) -> new_string or nil
|
2590
2826
|
# -->
|
2591
|
-
# Removes the substring of `self` specified by the arguments
|
2592
|
-
#
|
2593
|
-
#
|
2594
|
-
# See String#[] for details about the arguments that specify the substring.
|
2827
|
+
# Removes and returns the substring of `self` specified by the arguments. See
|
2828
|
+
# [String Slices](rdoc-ref:String@String+Slices).
|
2595
2829
|
#
|
2596
2830
|
# A few examples:
|
2597
2831
|
#
|
@@ -2602,132 +2836,173 @@ class String
|
|
2602
2836
|
# string.slice!("r") #=> "r"
|
2603
2837
|
# string #=> "Thing"
|
2604
2838
|
#
|
2605
|
-
def slice!: (int
|
2606
|
-
| (
|
2607
|
-
| (
|
2608
|
-
| (
|
2839
|
+
def slice!: (int index, ?int length) -> String?
|
2840
|
+
| (range[int?] range) -> String?
|
2841
|
+
| (String substring) -> String?
|
2842
|
+
| (Regexp regexp, ?MatchData::capture backref) -> String?
|
2609
2843
|
|
2610
2844
|
# <!--
|
2611
2845
|
# rdoc-file=string.c
|
2612
|
-
# -
|
2613
|
-
# -
|
2846
|
+
# - split(field_sep = $;, limit = 0) -> array
|
2847
|
+
# - split(field_sep = $;, limit = 0) {|substring| ... } -> self
|
2614
2848
|
# -->
|
2615
|
-
#
|
2616
|
-
#
|
2849
|
+
# Returns an array of substrings of `self` that are the result of splitting
|
2850
|
+
# `self` at each occurrence of the given field separator `field_sep`.
|
2851
|
+
#
|
2852
|
+
# When `field_sep` is `$;`:
|
2853
|
+
#
|
2854
|
+
# * If `$;` is `nil` (its default value), the split occurs just as if
|
2855
|
+
# `field_sep` were given as a space character (see below).
|
2856
|
+
#
|
2857
|
+
# * If `$;` is a string, the split occurs just as if `field_sep` were given as
|
2858
|
+
# that string (see below).
|
2859
|
+
#
|
2860
|
+
# When `field_sep` is `' '` and `limit` is `0` (its default value), the split
|
2861
|
+
# occurs at each sequence of whitespace:
|
2862
|
+
#
|
2863
|
+
# 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
|
2864
|
+
# "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
|
2865
|
+
# 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
|
2866
|
+
# ''.split(' ') => []
|
2867
|
+
#
|
2868
|
+
# When `field_sep` is a string different from `' '` and `limit` is `0`, the
|
2869
|
+
# split occurs at each occurrence of `field_sep`; trailing empty substrings are
|
2870
|
+
# not returned:
|
2871
|
+
#
|
2872
|
+
# 'abracadabra'.split('ab') => ["", "racad", "ra"]
|
2873
|
+
# 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
|
2874
|
+
# ''.split('a') => []
|
2875
|
+
# '3.14159'.split('1') => ["3.", "4", "59"]
|
2876
|
+
# '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
|
2877
|
+
# 'тест'.split('т') => ["", "ес"]
|
2878
|
+
# 'こんにちは'.split('に') => ["こん", "ちは"]
|
2879
|
+
#
|
2880
|
+
# When `field_sep` is a Regexp and `limit` is `0`, the split occurs at each
|
2881
|
+
# occurrence of a match; trailing empty substrings are not returned:
|
2882
|
+
#
|
2883
|
+
# 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
|
2884
|
+
# 'aaabcdaaa'.split(/a/) => ["", "", "", "bcd"]
|
2885
|
+
# 'aaabcdaaa'.split(//) => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
|
2886
|
+
# '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
|
2887
|
+
#
|
2888
|
+
# If the Regexp contains groups, their matches are also included in the returned
|
2889
|
+
# array:
|
2890
|
+
#
|
2891
|
+
# '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
|
2892
|
+
#
|
2893
|
+
# As seen above, if `limit` is `0`, trailing empty substrings are not returned:
|
2894
|
+
#
|
2895
|
+
# 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
|
2617
2896
|
#
|
2618
|
-
# If
|
2619
|
-
#
|
2620
|
-
#
|
2621
|
-
# characters ignored.
|
2897
|
+
# If `limit` is positive integer `n`, no more than `n - 1-` splits occur, so
|
2898
|
+
# that at most `n` substrings are returned, and trailing empty substrings are
|
2899
|
+
# included:
|
2622
2900
|
#
|
2623
|
-
#
|
2624
|
-
#
|
2625
|
-
#
|
2626
|
-
#
|
2901
|
+
# 'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"]
|
2902
|
+
# 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"]
|
2903
|
+
# 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"]
|
2904
|
+
# 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""]
|
2905
|
+
# 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
|
2627
2906
|
#
|
2628
|
-
#
|
2629
|
-
# the
|
2907
|
+
# Note that if `field_sep` is a Regexp containing groups, their matches are in
|
2908
|
+
# the returned array, but do not count toward the limit.
|
2630
2909
|
#
|
2631
|
-
# If
|
2632
|
-
#
|
2633
|
-
# returned (captured groups will be returned as well, but are not counted
|
2634
|
-
# towards the limit). If *limit* is `1`, the entire string is returned as the
|
2635
|
-
# only entry in an array. If negative, there is no limit to the number of fields
|
2636
|
-
# returned, and trailing null fields are not suppressed.
|
2910
|
+
# If `limit` is negative, it behaves the same as if `limit` was zero, meaning
|
2911
|
+
# that there is no limit, and trailing empty substrings are included:
|
2637
2912
|
#
|
2638
|
-
#
|
2639
|
-
# considered to have no fields to split.
|
2913
|
+
# 'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
|
2640
2914
|
#
|
2641
|
-
#
|
2642
|
-
# " now's the time ".split(' ') #=> ["now's", "the", "time"]
|
2643
|
-
# " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
|
2644
|
-
# "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
|
2645
|
-
# "hello".split(//) #=> ["h", "e", "l", "l", "o"]
|
2646
|
-
# "hello".split(//, 3) #=> ["h", "e", "llo"]
|
2647
|
-
# "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
|
2915
|
+
# If a block is given, it is called with each substring:
|
2648
2916
|
#
|
2649
|
-
#
|
2650
|
-
# "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
|
2651
|
-
# "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
|
2652
|
-
# "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
|
2917
|
+
# 'abc def ghi'.split(' ') {|substring| p substring }
|
2653
2918
|
#
|
2654
|
-
#
|
2919
|
+
# Output:
|
2655
2920
|
#
|
2656
|
-
# ""
|
2921
|
+
# "abc"
|
2922
|
+
# "def"
|
2923
|
+
# "ghi"
|
2657
2924
|
#
|
2658
|
-
#
|
2925
|
+
# Related: String#partition, String#rpartition.
|
2659
2926
|
#
|
2660
|
-
def split: (?Regexp | string pattern, ?int limit) -> Array[String]
|
2661
|
-
| (?Regexp | string pattern, ?int limit) { (String) -> void } -> self
|
2927
|
+
def split: (?Regexp | string | nil pattern, ?int limit) -> Array[String]
|
2928
|
+
| (?Regexp | string | nil pattern, ?int limit) { (String substring) -> void } -> self
|
2662
2929
|
|
2663
2930
|
# <!--
|
2664
2931
|
# rdoc-file=string.c
|
2665
|
-
# -
|
2932
|
+
# - squeeze(*selectors) -> new_string
|
2666
2933
|
# -->
|
2667
|
-
#
|
2668
|
-
#
|
2669
|
-
#
|
2670
|
-
#
|
2671
|
-
#
|
2934
|
+
# Returns a copy of `self` with characters specified by `selectors` "squeezed"
|
2935
|
+
# (see [Multiple Character
|
2936
|
+
# Selectors](rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors)):
|
2937
|
+
#
|
2938
|
+
# "Squeezed" means that each multiple-character run of a selected character is
|
2939
|
+
# squeezed down to a single character; with no arguments given, squeezes all
|
2940
|
+
# characters:
|
2672
2941
|
#
|
2673
2942
|
# "yellow moon".squeeze #=> "yelow mon"
|
2674
2943
|
# " now is the".squeeze(" ") #=> " now is the"
|
2675
2944
|
# "putters shoot balls".squeeze("m-z") #=> "puters shot balls"
|
2676
2945
|
#
|
2677
|
-
def squeeze: (*
|
2946
|
+
def squeeze: (*selector selectors) -> String
|
2678
2947
|
|
2679
2948
|
# <!--
|
2680
2949
|
# rdoc-file=string.c
|
2681
|
-
# -
|
2950
|
+
# - squeeze!(*selectors) -> self or nil
|
2682
2951
|
# -->
|
2683
|
-
#
|
2684
|
-
# made.
|
2952
|
+
# Like String#squeeze, but modifies `self` in place. Returns `self` if any
|
2953
|
+
# changes were made, `nil` otherwise.
|
2685
2954
|
#
|
2686
|
-
def squeeze!: (*
|
2955
|
+
def squeeze!: (*selector selectors) -> self?
|
2687
2956
|
|
2688
2957
|
# <!--
|
2689
2958
|
# rdoc-file=string.c
|
2690
|
-
# -
|
2959
|
+
# - start_with?(*string_or_regexp) -> true or false
|
2691
2960
|
# -->
|
2692
|
-
# Returns
|
2693
|
-
#
|
2961
|
+
# Returns whether `self` starts with any of the given `string_or_regexp`.
|
2962
|
+
#
|
2963
|
+
# Matches patterns against the beginning of `self`. For each given
|
2964
|
+
# `string_or_regexp`, the pattern is:
|
2965
|
+
#
|
2966
|
+
# * `string_or_regexp` itself, if it is a Regexp.
|
2967
|
+
# * `Regexp.quote(string_or_regexp)`, if `string_or_regexp` is a string.
|
2694
2968
|
#
|
2695
|
-
#
|
2696
|
-
# "hello".start_with?(/H/i) #=> true
|
2969
|
+
# Returns `true` if any pattern matches the beginning, `false` otherwise:
|
2697
2970
|
#
|
2698
|
-
# #
|
2699
|
-
#
|
2700
|
-
#
|
2971
|
+
# 'hello'.start_with?('hell') # => true
|
2972
|
+
# 'hello'.start_with?(/H/i) # => true
|
2973
|
+
# 'hello'.start_with?('heaven', 'hell') # => true
|
2974
|
+
# 'hello'.start_with?('heaven', 'paradise') # => false
|
2975
|
+
# 'тест'.start_with?('т') # => true
|
2976
|
+
# 'こんにちは'.start_with?('こ') # => true
|
2701
2977
|
#
|
2702
|
-
|
2978
|
+
# Related: String#end_with?.
|
2979
|
+
#
|
2980
|
+
def start_with?: (*Regexp | string prefixes) -> bool
|
2703
2981
|
|
2704
2982
|
# <!--
|
2705
2983
|
# rdoc-file=string.c
|
2706
|
-
# -
|
2984
|
+
# - strip -> new_string
|
2707
2985
|
# -->
|
2708
|
-
# Returns a copy of the receiver with leading and trailing whitespace removed
|
2986
|
+
# Returns a copy of the receiver with leading and trailing whitespace removed;
|
2987
|
+
# see [Whitespace in Strings](rdoc-ref:String@Whitespace+in+Strings):
|
2709
2988
|
#
|
2710
|
-
#
|
2711
|
-
#
|
2989
|
+
# whitespace = "\x00\t\n\v\f\r "
|
2990
|
+
# s = whitespace + 'abc' + whitespace
|
2991
|
+
# s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r "
|
2992
|
+
# s.strip # => "abc"
|
2712
2993
|
#
|
2713
|
-
#
|
2714
|
-
# "\tgoodbye\r\n".strip #=> "goodbye"
|
2715
|
-
# "\x00\t\n\v\f\r ".strip #=> ""
|
2716
|
-
# "hello".strip #=> "hello"
|
2994
|
+
# Related: String#lstrip, String#rstrip.
|
2717
2995
|
#
|
2718
2996
|
def strip: () -> String
|
2719
2997
|
|
2720
2998
|
# <!--
|
2721
2999
|
# rdoc-file=string.c
|
2722
|
-
# -
|
3000
|
+
# - strip! -> self or nil
|
2723
3001
|
# -->
|
2724
|
-
#
|
2725
|
-
#
|
2726
|
-
#
|
2727
|
-
# Refer to String#strip for the definition of whitespace.
|
3002
|
+
# Like String#strip, except that any modifications are made in `self`; returns
|
3003
|
+
# `self` if any modification are made, `nil` otherwise.
|
2728
3004
|
#
|
2729
|
-
#
|
2730
|
-
# "hello".strip! #=> nil
|
3005
|
+
# Related: String#lstrip!, String#strip!.
|
2731
3006
|
#
|
2732
3007
|
def strip!: () -> self?
|
2733
3008
|
|
@@ -2739,11 +3014,11 @@ class String
|
|
2739
3014
|
# Returns a copy of `self` with only the first occurrence (not all occurrences)
|
2740
3015
|
# of the given `pattern` replaced.
|
2741
3016
|
#
|
2742
|
-
# See [Substitution Methods](
|
3017
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
2743
3018
|
#
|
2744
3019
|
# Related: String#sub!, String#gsub, String#gsub!.
|
2745
3020
|
#
|
2746
|
-
def sub: (Regexp | string pattern, string |
|
3021
|
+
def sub: (Regexp | string pattern, string | hash[String, _ToS] replacement) -> String
|
2747
3022
|
| (Regexp | string pattern) { (String match) -> _ToS } -> String
|
2748
3023
|
|
2749
3024
|
# <!--
|
@@ -2751,15 +3026,15 @@ class String
|
|
2751
3026
|
# - sub!(pattern, replacement) -> self or nil
|
2752
3027
|
# - sub!(pattern) {|match| ... } -> self or nil
|
2753
3028
|
# -->
|
2754
|
-
#
|
2755
|
-
#
|
3029
|
+
# Replaces the first occurrence (not all occurrences) of the given `pattern` on
|
3030
|
+
# `self`; returns `self` if a replacement occurred, `nil` otherwise.
|
2756
3031
|
#
|
2757
|
-
# See [Substitution Methods](
|
3032
|
+
# See [Substitution Methods](rdoc-ref:String@Substitution+Methods).
|
2758
3033
|
#
|
2759
3034
|
# Related: String#sub, String#gsub, String#gsub!.
|
2760
3035
|
#
|
2761
|
-
def sub!: (Regexp | string pattern, string |
|
2762
|
-
| (Regexp | string pattern) { (String match) -> _ToS } ->
|
3036
|
+
def sub!: (Regexp | string pattern, string | hash[String, _ToS] replacement) -> self?
|
3037
|
+
| (Regexp | string pattern) { (String match) -> _ToS } -> self?
|
2763
3038
|
|
2764
3039
|
# <!--
|
2765
3040
|
# rdoc-file=string.c
|
@@ -2812,12 +3087,10 @@ class String
|
|
2812
3087
|
# s = '99zz99zz'
|
2813
3088
|
# s.succ # => "100aa00aa"
|
2814
3089
|
#
|
2815
|
-
# The successor to an empty String is a new empty String
|
3090
|
+
# The successor to an empty `String` is a new empty `String`:
|
2816
3091
|
#
|
2817
3092
|
# ''.succ # => ""
|
2818
3093
|
#
|
2819
|
-
# String#next is an alias for String#succ.
|
2820
|
-
#
|
2821
3094
|
def succ: () -> String
|
2822
3095
|
|
2823
3096
|
# <!--
|
@@ -2826,20 +3099,24 @@ class String
|
|
2826
3099
|
# -->
|
2827
3100
|
# Equivalent to String#succ, but modifies `self` in place; returns `self`.
|
2828
3101
|
#
|
2829
|
-
|
2830
|
-
#
|
2831
|
-
def succ!: () -> String
|
3102
|
+
def succ!: () -> self
|
2832
3103
|
|
2833
3104
|
# <!--
|
2834
3105
|
# rdoc-file=string.c
|
2835
|
-
# -
|
3106
|
+
# - sum(n = 16) -> integer
|
2836
3107
|
# -->
|
2837
|
-
# Returns a basic
|
2838
|
-
#
|
2839
|
-
# the binary value of each byte in *str* modulo `2**n - 1`. This is not a
|
2840
|
-
# particularly good checksum.
|
3108
|
+
# Returns a basic `n`-bit checksum of the characters in `self`; the checksum is
|
3109
|
+
# the sum of the binary value of each byte in `self`, modulo `2**n - 1`:
|
2841
3110
|
#
|
2842
|
-
|
3111
|
+
# 'hello'.sum # => 532
|
3112
|
+
# 'hello'.sum(4) # => 4
|
3113
|
+
# 'hello'.sum(64) # => 532
|
3114
|
+
# 'тест'.sum # => 1405
|
3115
|
+
# 'こんにちは'.sum # => 2582
|
3116
|
+
#
|
3117
|
+
# This is not a particularly strong checksum.
|
3118
|
+
#
|
3119
|
+
def sum: (?int bits) -> Integer
|
2843
3120
|
|
2844
3121
|
# <!--
|
2845
3122
|
# rdoc-file=string.c
|
@@ -2852,7 +3129,7 @@ class String
|
|
2852
3129
|
# s.swapcase # => "hELLO wORLD!"
|
2853
3130
|
#
|
2854
3131
|
# The casing may be affected by the given `options`; see [Case
|
2855
|
-
# Mapping](
|
3132
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
2856
3133
|
#
|
2857
3134
|
# Related: String#swapcase!.
|
2858
3135
|
#
|
@@ -2870,11 +3147,11 @@ class String
|
|
2870
3147
|
#
|
2871
3148
|
# s = 'Hello World!' # => "Hello World!"
|
2872
3149
|
# s.swapcase! # => "hELLO wORLD!"
|
2873
|
-
# s # => "
|
3150
|
+
# s # => "hELLO wORLD!"
|
2874
3151
|
# ''.swapcase! # => nil
|
2875
3152
|
#
|
2876
3153
|
# The casing may be affected by the given `options`; see [Case
|
2877
|
-
# Mapping](
|
3154
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
2878
3155
|
#
|
2879
3156
|
# Related: String#swapcase.
|
2880
3157
|
#
|
@@ -2885,25 +3162,30 @@ class String
|
|
2885
3162
|
|
2886
3163
|
# <!--
|
2887
3164
|
# rdoc-file=complex.c
|
2888
|
-
# -
|
2889
|
-
# -->
|
2890
|
-
# Returns
|
2891
|
-
#
|
2892
|
-
#
|
2893
|
-
#
|
2894
|
-
# '
|
2895
|
-
# '2.5'.to_c
|
2896
|
-
# '2
|
2897
|
-
# '-
|
2898
|
-
# '
|
2899
|
-
# '
|
2900
|
-
# '
|
2901
|
-
# '-
|
2902
|
-
# '
|
2903
|
-
# '1
|
2904
|
-
#
|
2905
|
-
#
|
2906
|
-
#
|
3165
|
+
# - to_c -> complex
|
3166
|
+
# -->
|
3167
|
+
# Returns `self` interpreted as a Complex object; leading whitespace and
|
3168
|
+
# trailing garbage are ignored:
|
3169
|
+
#
|
3170
|
+
# '9'.to_c # => (9+0i)
|
3171
|
+
# '2.5'.to_c # => (2.5+0i)
|
3172
|
+
# '2.5/1'.to_c # => ((5/2)+0i)
|
3173
|
+
# '-3/2'.to_c # => ((-3/2)+0i)
|
3174
|
+
# '-i'.to_c # => (0-1i)
|
3175
|
+
# '45i'.to_c # => (0+45i)
|
3176
|
+
# '3-4i'.to_c # => (3-4i)
|
3177
|
+
# '-4e2-4e-2i'.to_c # => (-400.0-0.04i)
|
3178
|
+
# '-0.0-0.0i'.to_c # => (-0.0-0.0i)
|
3179
|
+
# '1/2+3/4i'.to_c # => ((1/2)+(3/4)*i)
|
3180
|
+
# '1.0@0'.to_c # => (1+0.0i)
|
3181
|
+
# "1.0@#{Math::PI/2}".to_c # => (0.0+1i)
|
3182
|
+
# "1.0@#{Math::PI}".to_c # => (-1+0.0i)
|
3183
|
+
#
|
3184
|
+
# Returns Complex zero if the string cannot be converted:
|
3185
|
+
#
|
3186
|
+
# 'ruby'.to_c # => (0+0i)
|
3187
|
+
#
|
3188
|
+
# See Kernel#Complex.
|
2907
3189
|
#
|
2908
3190
|
def to_c: () -> Complex
|
2909
3191
|
|
@@ -2931,11 +3213,21 @@ class String
|
|
2931
3213
|
# - to_i(base = 10) -> integer
|
2932
3214
|
# -->
|
2933
3215
|
# Returns the result of interpreting leading characters in `self` as an integer
|
2934
|
-
# in the given `base` (which must be in (2..36)):
|
3216
|
+
# in the given `base` (which must be in (0, 2..36)):
|
2935
3217
|
#
|
2936
3218
|
# '123456'.to_i # => 123456
|
2937
3219
|
# '123def'.to_i(16) # => 1195503
|
2938
3220
|
#
|
3221
|
+
# With `base` zero, string `object` may contain leading characters to specify
|
3222
|
+
# the actual base:
|
3223
|
+
#
|
3224
|
+
# '123def'.to_i(0) # => 123
|
3225
|
+
# '0123def'.to_i(0) # => 83
|
3226
|
+
# '0b123def'.to_i(0) # => 1
|
3227
|
+
# '0o123def'.to_i(0) # => 83
|
3228
|
+
# '0d123def'.to_i(0) # => 123
|
3229
|
+
# '0x123def'.to_i(0) # => 1195503
|
3230
|
+
#
|
2939
3231
|
# Characters past a leading valid number (in the given `base`) are ignored:
|
2940
3232
|
#
|
2941
3233
|
# '12.345'.to_i # => 12
|
@@ -2946,7 +3238,7 @@ class String
|
|
2946
3238
|
# 'abcdef'.to_i # => 0
|
2947
3239
|
# '2'.to_i(2) # => 0
|
2948
3240
|
#
|
2949
|
-
def to_i: (?int
|
3241
|
+
def to_i: (?int radix) -> Integer
|
2950
3242
|
|
2951
3243
|
# <!--
|
2952
3244
|
# rdoc-file=rational.c
|
@@ -2981,23 +3273,19 @@ class String
|
|
2981
3273
|
# rdoc-file=string.c
|
2982
3274
|
# - to_s -> self or string
|
2983
3275
|
# -->
|
2984
|
-
# Returns `self` if `self` is a String
|
2985
|
-
# `self` is a subclass of String
|
2986
|
-
#
|
2987
|
-
# String#to_str is an alias for String#to_s.
|
3276
|
+
# Returns `self` if `self` is a `String`, or `self` converted to a `String` if
|
3277
|
+
# `self` is a subclass of `String`.
|
2988
3278
|
#
|
2989
3279
|
def to_s: () -> String
|
2990
3280
|
|
2991
3281
|
# <!-- rdoc-file=string.c -->
|
2992
|
-
# Returns `self` if `self` is a String
|
2993
|
-
# `self` is a subclass of String
|
2994
|
-
#
|
2995
|
-
# String#to_str is an alias for String#to_s.
|
3282
|
+
# Returns `self` if `self` is a `String`, or `self` converted to a `String` if
|
3283
|
+
# `self` is a subclass of `String`.
|
2996
3284
|
#
|
2997
|
-
|
3285
|
+
alias to_str to_s
|
2998
3286
|
|
2999
3287
|
# <!-- rdoc-file=string.c -->
|
3000
|
-
# Returns the Symbol corresponding to *str*, creating the symbol if it did not
|
3288
|
+
# Returns the `Symbol` corresponding to *str*, creating the symbol if it did not
|
3001
3289
|
# previously exist. See Symbol#id2name.
|
3002
3290
|
#
|
3003
3291
|
# "Koala".intern #=> :Koala
|
@@ -3011,73 +3299,81 @@ class String
|
|
3011
3299
|
#
|
3012
3300
|
# 'cat and dog'.to_sym #=> :"cat and dog"
|
3013
3301
|
#
|
3014
|
-
|
3302
|
+
alias to_sym intern
|
3015
3303
|
|
3016
3304
|
# <!--
|
3017
3305
|
# rdoc-file=string.c
|
3018
|
-
# -
|
3306
|
+
# - tr(selector, replacements) -> new_string
|
3019
3307
|
# -->
|
3020
|
-
# Returns a copy of `
|
3021
|
-
# corresponding
|
3022
|
-
#
|
3308
|
+
# Returns a copy of `self` with each character specified by string `selector`
|
3309
|
+
# translated to the corresponding character in string `replacements`. The
|
3310
|
+
# correspondence is *positional*:
|
3023
3311
|
#
|
3024
|
-
#
|
3025
|
-
#
|
3026
|
-
#
|
3312
|
+
# * Each occurrence of the first character specified by `selector` is
|
3313
|
+
# translated to the first character in `replacements`.
|
3314
|
+
# * Each occurrence of the second character specified by `selector` is
|
3315
|
+
# translated to the second character in `replacements`.
|
3316
|
+
# * And so on.
|
3027
3317
|
#
|
3028
|
-
#
|
3029
|
-
# `from_str` may start with a `^`, which denotes all characters except those
|
3030
|
-
# listed.
|
3318
|
+
# Example:
|
3031
3319
|
#
|
3032
|
-
#
|
3033
|
-
# "hello".tr('^aeiou', '*') #=> "*e**o"
|
3320
|
+
# 'hello'.tr('el', 'ip') #=> "hippo"
|
3034
3321
|
#
|
3035
|
-
#
|
3036
|
-
#
|
3037
|
-
# or `to_str`:
|
3322
|
+
# If `replacements` is shorter than `selector`, it is implicitly padded with its
|
3323
|
+
# own last character:
|
3038
3324
|
#
|
3039
|
-
#
|
3040
|
-
#
|
3325
|
+
# 'hello'.tr('aeiou', '-') # => "h-ll-"
|
3326
|
+
# 'hello'.tr('aeiou', 'AA-') # => "hAll-"
|
3041
3327
|
#
|
3042
|
-
#
|
3043
|
-
#
|
3044
|
-
#
|
3328
|
+
# Arguments `selector` and `replacements` must be valid character selectors (see
|
3329
|
+
# [Character Selectors](rdoc-ref:character_selectors.rdoc)), and may use any of
|
3330
|
+
# its valid forms, including negation, ranges, and escaping:
|
3045
3331
|
#
|
3046
|
-
#
|
3047
|
-
#
|
3332
|
+
# # Negation.
|
3333
|
+
# 'hello'.tr('^aeiou', '-') # => "-e--o"
|
3334
|
+
# # Ranges.
|
3335
|
+
# 'ibm'.tr('b-z', 'a-z') # => "hal"
|
3336
|
+
# # Escapes.
|
3337
|
+
# 'hel^lo'.tr('\^aeiou', '-') # => "h-l-l-" # Escaped leading caret.
|
3338
|
+
# 'i-b-m'.tr('b\-z', 'a-z') # => "ibabm" # Escaped embedded hyphen.
|
3339
|
+
# 'foo\\bar'.tr('ab\\', 'XYZ') # => "fooZYXr" # Escaped backslash.
|
3048
3340
|
#
|
3049
|
-
def tr: (
|
3341
|
+
def tr: (selector source, string relpacement) -> String
|
3050
3342
|
|
3051
3343
|
# <!--
|
3052
3344
|
# rdoc-file=string.c
|
3053
|
-
# -
|
3345
|
+
# - tr!(selector, replacements) -> self or nil
|
3054
3346
|
# -->
|
3055
|
-
#
|
3056
|
-
#
|
3347
|
+
# Like String#tr, but modifies `self` in place. Returns `self` if any changes
|
3348
|
+
# were made, `nil` otherwise.
|
3057
3349
|
#
|
3058
|
-
def tr!: (
|
3350
|
+
def tr!: (selector source, string relpacement) -> self?
|
3059
3351
|
|
3060
3352
|
# <!--
|
3061
3353
|
# rdoc-file=string.c
|
3062
|
-
# -
|
3354
|
+
# - tr_s(selector, replacements) -> string
|
3063
3355
|
# -->
|
3064
|
-
#
|
3065
|
-
#
|
3356
|
+
# Like String#tr, but also squeezes the modified portions of the translated
|
3357
|
+
# string; returns a new string (translated and squeezed).
|
3066
3358
|
#
|
3067
|
-
#
|
3068
|
-
#
|
3069
|
-
#
|
3359
|
+
# 'hello'.tr_s('l', 'r') #=> "hero"
|
3360
|
+
# 'hello'.tr_s('el', '-') #=> "h-o"
|
3361
|
+
# 'hello'.tr_s('el', 'hx') #=> "hhxo"
|
3070
3362
|
#
|
3071
|
-
|
3363
|
+
# Related: String#squeeze.
|
3364
|
+
#
|
3365
|
+
def tr_s: (selector source, string replacement) -> String
|
3072
3366
|
|
3073
3367
|
# <!--
|
3074
3368
|
# rdoc-file=string.c
|
3075
|
-
# -
|
3369
|
+
# - tr_s!(selector, replacements) -> self or nil
|
3076
3370
|
# -->
|
3077
|
-
#
|
3078
|
-
#
|
3371
|
+
# Like String#tr_s, but modifies `self` in place. Returns `self` if any changes
|
3372
|
+
# were made, `nil` otherwise.
|
3373
|
+
#
|
3374
|
+
# Related: String#squeeze!.
|
3079
3375
|
#
|
3080
|
-
def tr_s!: (
|
3376
|
+
def tr_s!: (selector source, string replacement) -> self?
|
3081
3377
|
|
3082
3378
|
# <!--
|
3083
3379
|
# rdoc-file=string.c
|
@@ -3096,211 +3392,96 @@ class String
|
|
3096
3392
|
|
3097
3393
|
# <!--
|
3098
3394
|
# rdoc-file=string.c
|
3099
|
-
# -
|
3395
|
+
# - unicode_normalize(form = :nfc) -> string
|
3100
3396
|
# -->
|
3101
|
-
#
|
3102
|
-
#
|
3103
|
-
#
|
3104
|
-
#
|
3397
|
+
# Returns a copy of `self` with [Unicode
|
3398
|
+
# normalization](https://unicode.org/reports/tr15) applied.
|
3399
|
+
#
|
3400
|
+
# Argument `form` must be one of the following symbols (see [Unicode
|
3401
|
+
# normalization forms](https://unicode.org/reports/tr15/#Norm_Forms)):
|
3402
|
+
#
|
3403
|
+
# * `:nfc`: Canonical decomposition, followed by canonical composition.
|
3404
|
+
# * `:nfd`: Canonical decomposition.
|
3405
|
+
# * `:nfkc`: Compatibility decomposition, followed by canonical composition.
|
3406
|
+
# * `:nfkd`: Compatibility decomposition.
|
3105
3407
|
#
|
3106
|
-
#
|
3107
|
-
# this context, 'Unicode Encoding' means any of UTF-8, UTF-16BE/LE, and
|
3108
|
-
# UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything other than
|
3109
|
-
# UTF-8 is implemented by converting to UTF-8, which makes it slower than UTF-8.
|
3408
|
+
# The encoding of `self` must be one of:
|
3110
3409
|
#
|
3111
|
-
#
|
3112
|
-
#
|
3113
|
-
#
|
3114
|
-
#
|
3115
|
-
#
|
3410
|
+
# * Encoding::UTF_8
|
3411
|
+
# * Encoding::UTF_16BE
|
3412
|
+
# * Encoding::UTF_16LE
|
3413
|
+
# * Encoding::UTF_32BE
|
3414
|
+
# * Encoding::UTF_32LE
|
3415
|
+
# * Encoding::GB18030
|
3416
|
+
# * Encoding::UCS_2BE
|
3417
|
+
# * Encoding::UCS_4BE
|
3116
3418
|
#
|
3117
|
-
|
3419
|
+
# Examples:
|
3420
|
+
#
|
3421
|
+
# "a\u0300".unicode_normalize # => "a"
|
3422
|
+
# "\u00E0".unicode_normalize(:nfd) # => "a "
|
3423
|
+
#
|
3424
|
+
# Related: String#unicode_normalize!, String#unicode_normalized?.
|
3425
|
+
#
|
3426
|
+
def unicode_normalize: (?:nfc | :nfd | :nfkc | :nfkd form) -> self
|
3118
3427
|
|
3119
3428
|
# <!--
|
3120
3429
|
# rdoc-file=string.c
|
3121
|
-
# -
|
3430
|
+
# - unicode_normalize!(form = :nfc) -> self
|
3122
3431
|
# -->
|
3123
|
-
#
|
3124
|
-
#
|
3432
|
+
# Like String#unicode_normalize, except that the normalization is performed on
|
3433
|
+
# `self`.
|
3125
3434
|
#
|
3126
|
-
|
3435
|
+
# Related String#unicode_normalized?.
|
3436
|
+
#
|
3437
|
+
def unicode_normalize!: (?:nfc | :nfd | :nfkc | :nfkd form) -> self
|
3127
3438
|
|
3128
3439
|
# <!--
|
3129
3440
|
# rdoc-file=string.c
|
3130
|
-
# -
|
3441
|
+
# - unicode_normalized?(form = :nfc) -> true or false
|
3131
3442
|
# -->
|
3132
|
-
#
|
3133
|
-
#
|
3443
|
+
# Returns `true` if `self` is in the given `form` of Unicode normalization,
|
3444
|
+
# `false` otherwise. The `form` must be one of `:nfc`, `:nfd`, `:nfkc`, or
|
3445
|
+
# `:nfkd`.
|
3446
|
+
#
|
3447
|
+
# Examples:
|
3134
3448
|
#
|
3135
|
-
#
|
3136
|
-
#
|
3449
|
+
# "a\u0300".unicode_normalized? # => false
|
3450
|
+
# "a\u0300".unicode_normalized?(:nfd) # => true
|
3451
|
+
# "\u00E0".unicode_normalized? # => true
|
3452
|
+
# "\u00E0".unicode_normalized?(:nfd) # => false
|
3137
3453
|
#
|
3138
|
-
#
|
3139
|
-
#
|
3140
|
-
# "\
|
3141
|
-
#
|
3142
|
-
#
|
3143
|
-
#
|
3454
|
+
# Raises an exception if `self` is not in a Unicode encoding:
|
3455
|
+
#
|
3456
|
+
# s = "\xE0".force_encoding('ISO-8859-1')
|
3457
|
+
# s.unicode_normalized? # Raises Encoding::CompatibilityError.
|
3458
|
+
#
|
3459
|
+
# Related: String#unicode_normalize, String#unicode_normalize!.
|
3144
3460
|
#
|
3145
3461
|
def unicode_normalized?: (?:nfc | :nfd | :nfkc | :nfkd) -> bool
|
3146
3462
|
|
3147
3463
|
# <!--
|
3148
3464
|
# rdoc-file=pack.rb
|
3149
|
-
# -
|
3150
|
-
# - str.unpack(format, offset: anInteger) -> anArray
|
3151
|
-
# -->
|
3152
|
-
# Decodes *str* (which may contain binary data) according to the format string,
|
3153
|
-
# returning an array of each value extracted. The format string consists of a
|
3154
|
-
# sequence of single-character directives, summarized in the table at the end of
|
3155
|
-
# this entry. Each directive may be followed by a number, indicating the number
|
3156
|
-
# of times to repeat with this directive. An asterisk (```*`'') will use up all
|
3157
|
-
# remaining elements. The directives `sSiIlL` may each be followed by an
|
3158
|
-
# underscore (```_`'') or exclamation mark (```!`'') to use the underlying
|
3159
|
-
# platform's native size for the specified type; otherwise, it uses a
|
3160
|
-
# platform-independent consistent size. Spaces are ignored in the format string.
|
3161
|
-
#
|
3162
|
-
# See also String#unpack1, Array#pack.
|
3163
|
-
#
|
3164
|
-
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
3165
|
-
# "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
|
3166
|
-
# "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
|
3167
|
-
# "aa".unpack('b8B8') #=> ["10000110", "01100001"]
|
3168
|
-
# "aaa".unpack('h2H2c') #=> ["16", "61", 97]
|
3169
|
-
# "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
|
3170
|
-
# "now=20is".unpack('M*') #=> ["now is"]
|
3171
|
-
# "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
|
3172
|
-
#
|
3173
|
-
# This table summarizes the various formats and the Ruby classes returned by
|
3174
|
-
# each.
|
3175
|
-
#
|
3176
|
-
# Integer | |
|
3177
|
-
# Directive | Returns | Meaning
|
3178
|
-
# ------------------------------------------------------------------
|
3179
|
-
# C | Integer | 8-bit unsigned (unsigned char)
|
3180
|
-
# S | Integer | 16-bit unsigned, native endian (uint16_t)
|
3181
|
-
# L | Integer | 32-bit unsigned, native endian (uint32_t)
|
3182
|
-
# Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
3183
|
-
# J | Integer | pointer width unsigned, native endian (uintptr_t)
|
3184
|
-
# | |
|
3185
|
-
# c | Integer | 8-bit signed (signed char)
|
3186
|
-
# s | Integer | 16-bit signed, native endian (int16_t)
|
3187
|
-
# l | Integer | 32-bit signed, native endian (int32_t)
|
3188
|
-
# q | Integer | 64-bit signed, native endian (int64_t)
|
3189
|
-
# j | Integer | pointer width signed, native endian (intptr_t)
|
3190
|
-
# | |
|
3191
|
-
# S_ S! | Integer | unsigned short, native endian
|
3192
|
-
# I I_ I! | Integer | unsigned int, native endian
|
3193
|
-
# L_ L! | Integer | unsigned long, native endian
|
3194
|
-
# Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
|
3195
|
-
# | | if the platform has no long long type.)
|
3196
|
-
# J! | Integer | uintptr_t, native endian (same with J)
|
3197
|
-
# | |
|
3198
|
-
# s_ s! | Integer | signed short, native endian
|
3199
|
-
# i i_ i! | Integer | signed int, native endian
|
3200
|
-
# l_ l! | Integer | signed long, native endian
|
3201
|
-
# q_ q! | Integer | signed long long, native endian (ArgumentError
|
3202
|
-
# | | if the platform has no long long type.)
|
3203
|
-
# j! | Integer | intptr_t, native endian (same with j)
|
3204
|
-
# | |
|
3205
|
-
# S> s> S!> s!> | Integer | same as the directives without ">" except
|
3206
|
-
# L> l> L!> l!> | | big endian
|
3207
|
-
# I!> i!> | |
|
3208
|
-
# Q> q> Q!> q!> | | "S>" is the same as "n"
|
3209
|
-
# J> j> J!> j!> | | "L>" is the same as "N"
|
3210
|
-
# | |
|
3211
|
-
# S< s< S!< s!< | Integer | same as the directives without "<" except
|
3212
|
-
# L< l< L!< l!< | | little endian
|
3213
|
-
# I!< i!< | |
|
3214
|
-
# Q< q< Q!< q!< | | "S<" is the same as "v"
|
3215
|
-
# J< j< J!< j!< | | "L<" is the same as "V"
|
3216
|
-
# | |
|
3217
|
-
# n | Integer | 16-bit unsigned, network (big-endian) byte order
|
3218
|
-
# N | Integer | 32-bit unsigned, network (big-endian) byte order
|
3219
|
-
# v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
3220
|
-
# V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
3221
|
-
# | |
|
3222
|
-
# U | Integer | UTF-8 character
|
3223
|
-
# w | Integer | BER-compressed integer (see Array#pack)
|
3224
|
-
#
|
3225
|
-
# Float | |
|
3226
|
-
# Directive | Returns | Meaning
|
3227
|
-
# -----------------------------------------------------------------
|
3228
|
-
# D d | Float | double-precision, native format
|
3229
|
-
# F f | Float | single-precision, native format
|
3230
|
-
# E | Float | double-precision, little-endian byte order
|
3231
|
-
# e | Float | single-precision, little-endian byte order
|
3232
|
-
# G | Float | double-precision, network (big-endian) byte order
|
3233
|
-
# g | Float | single-precision, network (big-endian) byte order
|
3234
|
-
#
|
3235
|
-
# String | |
|
3236
|
-
# Directive | Returns | Meaning
|
3237
|
-
# -----------------------------------------------------------------
|
3238
|
-
# A | String | arbitrary binary string (remove trailing nulls and ASCII spaces)
|
3239
|
-
# a | String | arbitrary binary string
|
3240
|
-
# Z | String | null-terminated string
|
3241
|
-
# B | String | bit string (MSB first)
|
3242
|
-
# b | String | bit string (LSB first)
|
3243
|
-
# H | String | hex string (high nibble first)
|
3244
|
-
# h | String | hex string (low nibble first)
|
3245
|
-
# u | String | UU-encoded string
|
3246
|
-
# M | String | quoted-printable, MIME encoding (see RFC2045)
|
3247
|
-
# m | String | base64 encoded string (RFC 2045) (default)
|
3248
|
-
# | | base64 encoded string (RFC 4648) if followed by 0
|
3249
|
-
# P | String | pointer to a structure (fixed-length string)
|
3250
|
-
# p | String | pointer to a null-terminated string
|
3251
|
-
#
|
3252
|
-
# Misc. | |
|
3253
|
-
# Directive | Returns | Meaning
|
3254
|
-
# -----------------------------------------------------------------
|
3255
|
-
# @ | --- | skip to the offset given by the length argument
|
3256
|
-
# X | --- | skip backward one byte
|
3257
|
-
# x | --- | skip forward one byte
|
3258
|
-
#
|
3259
|
-
# The keyword *offset* can be given to start the decoding after skipping the
|
3260
|
-
# specified amount of bytes:
|
3261
|
-
# "abc".unpack("C*") # => [97, 98, 99]
|
3262
|
-
# "abc".unpack("C*", offset: 2) # => [99]
|
3263
|
-
# "abc".unpack("C*", offset: 4) # => offset outside of string (ArgumentError)
|
3264
|
-
#
|
3265
|
-
# HISTORY
|
3266
|
-
#
|
3267
|
-
# * J, J! j, and j! are available since Ruby 2.3.
|
3268
|
-
# * Q_, Q!, q_, and q! are available since Ruby 2.1.
|
3269
|
-
# * I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
|
3270
|
-
#
|
3271
|
-
def unpack: (String format, ?offset: Integer) -> Array[Integer | Float | String | nil]
|
3272
|
-
|
3273
|
-
# <!--
|
3274
|
-
# rdoc-file=pack.rb
|
3275
|
-
# - str.unpack1(format) -> obj
|
3276
|
-
# - str.unpack1(format, offset: anInteger) -> obj
|
3465
|
+
# - unpack(template, offset: 0, &block) -> array
|
3277
3466
|
# -->
|
3278
|
-
#
|
3279
|
-
# returning the first value extracted.
|
3280
|
-
#
|
3281
|
-
# See also String#unpack, Array#pack.
|
3282
|
-
#
|
3283
|
-
# Contrast with String#unpack:
|
3284
|
-
#
|
3285
|
-
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
3286
|
-
# "abc \0\0abc \0\0".unpack1('A6Z6') #=> "abc"
|
3467
|
+
# Extracts data from `self`.
|
3287
3468
|
#
|
3288
|
-
#
|
3289
|
-
#
|
3469
|
+
# If `block` is not given, forming objects that become the elements of a new
|
3470
|
+
# array, and returns that array. Otherwise, yields each object.
|
3290
3471
|
#
|
3291
|
-
#
|
3292
|
-
# "\xff\x00\x00\x00".unpack1("l") #=> 255
|
3472
|
+
# See [Packed Data](rdoc-ref:packed_data.rdoc).
|
3293
3473
|
#
|
3294
|
-
|
3295
|
-
|
3296
|
-
|
3297
|
-
#
|
3298
|
-
#
|
3299
|
-
#
|
3300
|
-
#
|
3301
|
-
#
|
3474
|
+
def unpack: (string template, ?offset: int) -> Array[Integer | Float | String | nil]
|
3475
|
+
| (string template, ?offset: int) { (Integer | Float | String | nil value) -> void } -> nil
|
3476
|
+
|
3477
|
+
# <!--
|
3478
|
+
# rdoc-file=pack.rb
|
3479
|
+
# - unpack1(template, offset: 0) -> object
|
3480
|
+
# -->
|
3481
|
+
# Like String#unpack, but unpacks and returns only the first extracted object.
|
3482
|
+
# See [Packed Data](rdoc-ref:packed_data.rdoc).
|
3302
3483
|
#
|
3303
|
-
def unpack1: (
|
3484
|
+
def unpack1: (string template, ?offset: int) -> (Integer | Float | String)?
|
3304
3485
|
|
3305
3486
|
# <!--
|
3306
3487
|
# rdoc-file=string.c
|
@@ -3312,7 +3493,7 @@ class String
|
|
3312
3493
|
# s.upcase # => "HELLO WORLD!"
|
3313
3494
|
#
|
3314
3495
|
# The casing may be affected by the given `options`; see [Case
|
3315
|
-
# Mapping](
|
3496
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
3316
3497
|
#
|
3317
3498
|
# Related: String#upcase!, String#downcase, String#downcase!.
|
3318
3499
|
#
|
@@ -3334,7 +3515,7 @@ class String
|
|
3334
3515
|
# s.upcase! # => nil
|
3335
3516
|
#
|
3336
3517
|
# The casing may be affected by the given `options`; see [Case
|
3337
|
-
# Mapping](
|
3518
|
+
# Mapping](rdoc-ref:case_mapping.rdoc).
|
3338
3519
|
#
|
3339
3520
|
# Related: String#upcase, String#downcase, String#downcase!.
|
3340
3521
|
#
|
@@ -3348,7 +3529,7 @@ class String
|
|
3348
3529
|
# - upto(other_string, exclusive = false) {|string| ... } -> self
|
3349
3530
|
# - upto(other_string, exclusive = false) -> new_enumerator
|
3350
3531
|
# -->
|
3351
|
-
# With a block given, calls the block with each String value returned by
|
3532
|
+
# With a block given, calls the block with each `String` value returned by
|
3352
3533
|
# successive calls to String#succ; the first value is `self`, the next is
|
3353
3534
|
# `self.succ`, and so on; the sequence terminates when value `other_string` is
|
3354
3535
|
# reached; returns `self`:
|
@@ -3377,82 +3558,26 @@ class String
|
|
3377
3558
|
#
|
3378
3559
|
# 'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
|
3379
3560
|
#
|
3380
|
-
def upto: (string
|
3381
|
-
| (string
|
3561
|
+
def upto: (string other_string, ?boolish exclusive) -> Enumerator[String, self]
|
3562
|
+
| (string other_string, ?boolish exclusive) { (String s) -> void } -> self
|
3382
3563
|
|
3383
3564
|
# <!--
|
3384
3565
|
# rdoc-file=string.c
|
3385
|
-
# -
|
3566
|
+
# - valid_encoding? -> true or false
|
3386
3567
|
# -->
|
3387
|
-
# Returns true
|
3568
|
+
# Returns `true` if `self` is encoded correctly, `false` otherwise:
|
3388
3569
|
#
|
3389
|
-
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding?
|
3390
|
-
# "\xc2".force_encoding("UTF-8").valid_encoding?
|
3391
|
-
# "\x80".force_encoding("UTF-8").valid_encoding?
|
3570
|
+
# "\xc2\xa1".force_encoding("UTF-8").valid_encoding? # => true
|
3571
|
+
# "\xc2".force_encoding("UTF-8").valid_encoding? # => false
|
3572
|
+
# "\x80".force_encoding("UTF-8").valid_encoding? # => false
|
3392
3573
|
#
|
3393
3574
|
def valid_encoding?: () -> bool
|
3394
|
-
|
3395
|
-
private
|
3396
|
-
|
3397
|
-
# <!--
|
3398
|
-
# rdoc-file=string.c
|
3399
|
-
# - String.new(string = '') -> new_string
|
3400
|
-
# - String.new(string = '', encoding: encoding) -> new_string
|
3401
|
-
# - String.new(string = '', capacity: size) -> new_string
|
3402
|
-
# -->
|
3403
|
-
# Returns a new String that is a copy of `string`.
|
3404
|
-
#
|
3405
|
-
# With no arguments, returns the empty string with the Encoding `ASCII-8BIT`:
|
3406
|
-
# s = String.new
|
3407
|
-
# s # => ""
|
3408
|
-
# s.encoding # => #<Encoding:ASCII-8BIT>
|
3409
|
-
#
|
3410
|
-
# With the single String argument `string`, returns a copy of `string` with the
|
3411
|
-
# same encoding as `string`:
|
3412
|
-
# s = String.new("Que veut dire \u{e7}a?")
|
3413
|
-
# s # => "Que veut dire \u{e7}a?"
|
3414
|
-
# s.encoding # => #<Encoding:UTF-8>
|
3415
|
-
#
|
3416
|
-
# Literal strings like `""` or here-documents always use [script
|
3417
|
-
# encoding](Encoding.html#class-Encoding-label-Script+encoding), unlike
|
3418
|
-
# String.new.
|
3419
|
-
#
|
3420
|
-
# With keyword `encoding`, returns a copy of `str` with the specified encoding:
|
3421
|
-
# s = String.new(encoding: 'ASCII')
|
3422
|
-
# s.encoding # => #<Encoding:US-ASCII>
|
3423
|
-
# s = String.new('foo', encoding: 'ASCII')
|
3424
|
-
# s.encoding # => #<Encoding:US-ASCII>
|
3425
|
-
#
|
3426
|
-
# Note that these are equivalent:
|
3427
|
-
# s0 = String.new('foo', encoding: 'ASCII')
|
3428
|
-
# s1 = 'foo'.force_encoding('ASCII')
|
3429
|
-
# s0.encoding == s1.encoding # => true
|
3430
|
-
#
|
3431
|
-
# With keyword `capacity`, returns a copy of `str`; the given `capacity` may set
|
3432
|
-
# the size of the internal buffer, which may affect performance:
|
3433
|
-
# String.new(capacity: 1) # => ""
|
3434
|
-
# String.new(capacity: 4096) # => ""
|
3435
|
-
#
|
3436
|
-
# The `string`, `encoding`, and `capacity` arguments may all be used together:
|
3437
|
-
#
|
3438
|
-
# String.new('hello', encoding: 'UTF-8', capacity: 25)
|
3439
|
-
#
|
3440
|
-
def initialize: (?string str, ?encoding: encoding, ?capacity: int) -> void
|
3441
|
-
|
3442
|
-
# <!--
|
3443
|
-
# rdoc-file=string.c
|
3444
|
-
# - replace(other_string) -> self
|
3445
|
-
# -->
|
3446
|
-
# Replaces the contents of `self` with the contents of `other_string`:
|
3447
|
-
#
|
3448
|
-
# s = 'foo' # => "foo"
|
3449
|
-
# s.replace('bar') # => "bar"
|
3450
|
-
#
|
3451
|
-
alias initialize_copy replace
|
3452
3575
|
end
|
3453
3576
|
|
3577
|
+
%a{steep:deprecated}
|
3454
3578
|
interface _ArefFromStringToString
|
3455
3579
|
def []: (String) -> String
|
3456
3580
|
end
|
3457
3581
|
|
3582
|
+
%a{steep:deprecated}
|
3458
3583
|
type String::encode_fallback = Hash[String, String] | Proc | Method | _ArefFromStringToString
|