rbs-relaxed 3.9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/dependabot.yml +22 -0
- data/.github/workflows/comments.yml +35 -0
- data/.github/workflows/dependabot.yml +30 -0
- data/.github/workflows/ruby.yml +82 -0
- data/.github/workflows/typecheck.yml +38 -0
- data/.github/workflows/windows.yml +43 -0
- data/.gitignore +23 -0
- data/.rubocop.yml +68 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +1868 -0
- data/COPYING +56 -0
- data/README.md +203 -0
- data/Rakefile +417 -0
- data/Steepfile +44 -0
- data/config.yml +313 -0
- data/core/array.rbs +4062 -0
- data/core/basic_object.rbs +375 -0
- data/core/binding.rbs +150 -0
- data/core/builtin.rbs +277 -0
- data/core/class.rbs +220 -0
- data/core/comparable.rbs +171 -0
- data/core/complex.rbs +786 -0
- data/core/constants.rbs +96 -0
- data/core/data.rbs +415 -0
- data/core/dir.rbs +981 -0
- data/core/encoding.rbs +1371 -0
- data/core/enumerable.rbs +2405 -0
- data/core/enumerator/product.rbs +92 -0
- data/core/enumerator.rbs +630 -0
- data/core/env.rbs +6 -0
- data/core/errno.rbs +673 -0
- data/core/errors.rbs +760 -0
- data/core/exception.rbs +485 -0
- data/core/false_class.rbs +82 -0
- data/core/fiber.rbs +550 -0
- data/core/fiber_error.rbs +11 -0
- data/core/file.rbs +2936 -0
- data/core/file_test.rbs +331 -0
- data/core/float.rbs +1151 -0
- data/core/gc.rbs +644 -0
- data/core/global_variables.rbs +184 -0
- data/core/hash.rbs +1861 -0
- data/core/integer.rbs +1413 -0
- data/core/io/buffer.rbs +984 -0
- data/core/io/wait.rbs +70 -0
- data/core/io.rbs +3406 -0
- data/core/kernel.rbs +3096 -0
- data/core/marshal.rbs +207 -0
- data/core/match_data.rbs +635 -0
- data/core/math.rbs +729 -0
- data/core/method.rbs +386 -0
- data/core/module.rbs +1704 -0
- data/core/nil_class.rbs +209 -0
- data/core/numeric.rbs +818 -0
- data/core/object.rbs +110 -0
- data/core/object_space/weak_key_map.rbs +166 -0
- data/core/object_space.rbs +190 -0
- data/core/proc.rbs +868 -0
- data/core/process.rbs +2296 -0
- data/core/ractor.rbs +1068 -0
- data/core/random.rbs +237 -0
- data/core/range.rbs +1107 -0
- data/core/rational.rbs +531 -0
- data/core/rb_config.rbs +88 -0
- data/core/rbs/unnamed/argf.rbs +1229 -0
- data/core/rbs/unnamed/env_class.rbs +1209 -0
- data/core/rbs/unnamed/random.rbs +293 -0
- data/core/refinement.rbs +59 -0
- data/core/regexp.rbs +1930 -0
- data/core/ruby_vm.rbs +765 -0
- data/core/rubygems/basic_specification.rbs +6 -0
- data/core/rubygems/config_file.rbs +38 -0
- data/core/rubygems/dependency_installer.rbs +6 -0
- data/core/rubygems/errors.rbs +176 -0
- data/core/rubygems/installer.rbs +15 -0
- data/core/rubygems/path_support.rbs +6 -0
- data/core/rubygems/platform.rbs +7 -0
- data/core/rubygems/request_set.rbs +49 -0
- data/core/rubygems/requirement.rbs +148 -0
- data/core/rubygems/rubygems.rbs +1171 -0
- data/core/rubygems/source_list.rbs +15 -0
- data/core/rubygems/specification.rbs +23 -0
- data/core/rubygems/stream_ui.rbs +5 -0
- data/core/rubygems/uninstaller.rbs +10 -0
- data/core/rubygems/version.rbs +294 -0
- data/core/set.rbs +621 -0
- data/core/signal.rbs +100 -0
- data/core/string.rbs +3583 -0
- data/core/struct.rbs +667 -0
- data/core/symbol.rbs +475 -0
- data/core/thread.rbs +1765 -0
- data/core/thread_group.rbs +79 -0
- data/core/time.rbs +1762 -0
- data/core/trace_point.rbs +477 -0
- data/core/true_class.rbs +98 -0
- data/core/unbound_method.rbs +329 -0
- data/core/warning.rbs +87 -0
- data/docs/CONTRIBUTING.md +106 -0
- data/docs/architecture.md +110 -0
- data/docs/collection.md +192 -0
- data/docs/data_and_struct.md +86 -0
- data/docs/gem.md +57 -0
- data/docs/rbs_by_example.md +309 -0
- data/docs/repo.md +125 -0
- data/docs/sigs.md +167 -0
- data/docs/stdlib.md +147 -0
- data/docs/syntax.md +910 -0
- data/docs/tools.md +17 -0
- data/exe/rbs +7 -0
- data/ext/rbs_extension/extconf.rb +15 -0
- data/ext/rbs_extension/lexer.c +2728 -0
- data/ext/rbs_extension/lexer.h +179 -0
- data/ext/rbs_extension/lexer.re +147 -0
- data/ext/rbs_extension/lexstate.c +175 -0
- data/ext/rbs_extension/location.c +325 -0
- data/ext/rbs_extension/location.h +85 -0
- data/ext/rbs_extension/main.c +33 -0
- data/ext/rbs_extension/parser.c +2973 -0
- data/ext/rbs_extension/parser.h +18 -0
- data/ext/rbs_extension/parserstate.c +397 -0
- data/ext/rbs_extension/parserstate.h +163 -0
- data/ext/rbs_extension/rbs_extension.h +31 -0
- data/ext/rbs_extension/unescape.c +32 -0
- data/goodcheck.yml +91 -0
- data/include/rbs/constants.h +82 -0
- data/include/rbs/ruby_objs.h +72 -0
- data/include/rbs/util/rbs_constant_pool.h +219 -0
- data/include/rbs.h +7 -0
- data/lib/rbs/ancestor_graph.rb +92 -0
- data/lib/rbs/annotate/annotations.rb +199 -0
- data/lib/rbs/annotate/formatter.rb +92 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +400 -0
- data/lib/rbs/annotate/rdoc_source.rb +131 -0
- data/lib/rbs/annotate.rb +8 -0
- data/lib/rbs/ast/annotation.rb +29 -0
- data/lib/rbs/ast/comment.rb +29 -0
- data/lib/rbs/ast/declarations.rb +467 -0
- data/lib/rbs/ast/directives.rb +49 -0
- data/lib/rbs/ast/members.rb +451 -0
- data/lib/rbs/ast/type_param.rb +225 -0
- data/lib/rbs/ast/visitor.rb +137 -0
- data/lib/rbs/buffer.rb +67 -0
- data/lib/rbs/builtin_names.rb +58 -0
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +83 -0
- data/lib/rbs/cli/validate.rb +357 -0
- data/lib/rbs/cli.rb +1223 -0
- data/lib/rbs/collection/cleaner.rb +38 -0
- data/lib/rbs/collection/config/lockfile.rb +92 -0
- data/lib/rbs/collection/config/lockfile_generator.rb +218 -0
- data/lib/rbs/collection/config.rb +81 -0
- data/lib/rbs/collection/installer.rb +32 -0
- data/lib/rbs/collection/sources/base.rb +14 -0
- data/lib/rbs/collection/sources/git.rb +258 -0
- data/lib/rbs/collection/sources/local.rb +81 -0
- data/lib/rbs/collection/sources/rubygems.rb +48 -0
- data/lib/rbs/collection/sources/stdlib.rb +50 -0
- data/lib/rbs/collection/sources.rb +38 -0
- data/lib/rbs/collection.rb +16 -0
- data/lib/rbs/constant.rb +28 -0
- data/lib/rbs/definition.rb +401 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +620 -0
- data/lib/rbs/definition_builder/method_builder.rb +254 -0
- data/lib/rbs/definition_builder.rb +845 -0
- data/lib/rbs/diff.rb +125 -0
- data/lib/rbs/environment/use_map.rb +77 -0
- data/lib/rbs/environment.rb +829 -0
- data/lib/rbs/environment_loader.rb +173 -0
- data/lib/rbs/environment_walker.rb +155 -0
- data/lib/rbs/errors.rb +645 -0
- data/lib/rbs/factory.rb +18 -0
- data/lib/rbs/file_finder.rb +28 -0
- data/lib/rbs/location_aux.rb +138 -0
- data/lib/rbs/locator.rb +243 -0
- data/lib/rbs/method_type.rb +143 -0
- data/lib/rbs/namespace.rb +125 -0
- data/lib/rbs/parser/lex_result.rb +15 -0
- data/lib/rbs/parser/token.rb +23 -0
- data/lib/rbs/parser_aux.rb +114 -0
- data/lib/rbs/prototype/helpers.rb +140 -0
- data/lib/rbs/prototype/node_usage.rb +99 -0
- data/lib/rbs/prototype/rb.rb +840 -0
- data/lib/rbs/prototype/rbi.rb +641 -0
- data/lib/rbs/prototype/runtime/helpers.rb +59 -0
- data/lib/rbs/prototype/runtime/reflection.rb +19 -0
- data/lib/rbs/prototype/runtime/value_object_generator.rb +279 -0
- data/lib/rbs/prototype/runtime.rb +667 -0
- data/lib/rbs/repository.rb +127 -0
- data/lib/rbs/resolver/constant_resolver.rb +219 -0
- data/lib/rbs/resolver/type_name_resolver.rb +91 -0
- data/lib/rbs/sorter.rb +198 -0
- data/lib/rbs/substitution.rb +83 -0
- data/lib/rbs/subtractor.rb +201 -0
- data/lib/rbs/test/errors.rb +80 -0
- data/lib/rbs/test/guaranteed.rb +30 -0
- data/lib/rbs/test/hook.rb +212 -0
- data/lib/rbs/test/observer.rb +19 -0
- data/lib/rbs/test/setup.rb +84 -0
- data/lib/rbs/test/setup_helper.rb +50 -0
- data/lib/rbs/test/tester.rb +167 -0
- data/lib/rbs/test/type_check.rb +435 -0
- data/lib/rbs/test.rb +112 -0
- data/lib/rbs/type_alias_dependency.rb +100 -0
- data/lib/rbs/type_alias_regularity.rb +126 -0
- data/lib/rbs/type_name.rb +109 -0
- data/lib/rbs/types.rb +1596 -0
- data/lib/rbs/unit_test/convertibles.rb +176 -0
- data/lib/rbs/unit_test/spy.rb +138 -0
- data/lib/rbs/unit_test/type_assertions.rb +347 -0
- data/lib/rbs/unit_test/with_aliases.rb +143 -0
- data/lib/rbs/unit_test.rb +6 -0
- data/lib/rbs/validator.rb +186 -0
- data/lib/rbs/variance_calculator.rb +189 -0
- data/lib/rbs/vendorer.rb +71 -0
- data/lib/rbs/version.rb +5 -0
- data/lib/rbs/writer.rb +424 -0
- data/lib/rbs.rb +94 -0
- data/lib/rdoc/discover.rb +20 -0
- data/lib/rdoc_plugin/parser.rb +163 -0
- data/rbs-relaxed.gemspec +48 -0
- data/schema/annotation.json +14 -0
- data/schema/comment.json +26 -0
- data/schema/decls.json +326 -0
- data/schema/function.json +87 -0
- data/schema/location.json +56 -0
- data/schema/members.json +266 -0
- data/schema/methodType.json +50 -0
- data/schema/typeParam.json +36 -0
- data/schema/types.json +317 -0
- data/sig/ancestor_builder.rbs +163 -0
- data/sig/ancestor_graph.rbs +60 -0
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/annotation.rbs +27 -0
- data/sig/buffer.rbs +32 -0
- data/sig/builtin_names.rbs +44 -0
- data/sig/cli/colored_io.rbs +15 -0
- data/sig/cli/diff.rbs +21 -0
- data/sig/cli/validate.rbs +43 -0
- data/sig/cli.rbs +87 -0
- data/sig/collection/cleaner.rbs +13 -0
- data/sig/collection/config/lockfile.rbs +74 -0
- data/sig/collection/config/lockfile_generator.rbs +66 -0
- data/sig/collection/config.rbs +46 -0
- data/sig/collection/installer.rbs +17 -0
- data/sig/collection/sources.rbs +214 -0
- data/sig/collection.rbs +4 -0
- data/sig/comment.rbs +26 -0
- data/sig/constant.rbs +21 -0
- data/sig/declarations.rbs +267 -0
- data/sig/definition.rbs +173 -0
- data/sig/definition_builder.rbs +165 -0
- data/sig/diff.rbs +28 -0
- data/sig/directives.rbs +77 -0
- data/sig/environment.rbs +279 -0
- data/sig/environment_loader.rbs +111 -0
- data/sig/environment_walker.rbs +65 -0
- data/sig/errors.rbs +405 -0
- data/sig/factory.rbs +5 -0
- data/sig/file_finder.rbs +28 -0
- data/sig/location.rbs +110 -0
- data/sig/locator.rbs +58 -0
- data/sig/manifest.yaml +7 -0
- data/sig/members.rbs +258 -0
- data/sig/method_builder.rbs +84 -0
- data/sig/method_types.rbs +58 -0
- data/sig/namespace.rbs +146 -0
- data/sig/parser.rbs +100 -0
- data/sig/prototype/helpers.rbs +27 -0
- data/sig/prototype/node_usage.rbs +20 -0
- data/sig/prototype/rb.rbs +96 -0
- data/sig/prototype/rbi.rbs +75 -0
- data/sig/prototype/runtime.rbs +182 -0
- data/sig/rbs.rbs +21 -0
- data/sig/rdoc/rbs.rbs +67 -0
- data/sig/repository.rbs +85 -0
- data/sig/resolver/constant_resolver.rbs +92 -0
- data/sig/resolver/context.rbs +34 -0
- data/sig/resolver/type_name_resolver.rbs +35 -0
- data/sig/shims/bundler.rbs +38 -0
- data/sig/shims/enumerable.rbs +5 -0
- data/sig/shims/rubygems.rbs +19 -0
- data/sig/sorter.rbs +41 -0
- data/sig/substitution.rbs +48 -0
- data/sig/subtractor.rbs +37 -0
- data/sig/test/errors.rbs +52 -0
- data/sig/test/guranteed.rbs +9 -0
- data/sig/test/type_check.rbs +19 -0
- data/sig/test.rbs +82 -0
- data/sig/type_alias_dependency.rbs +53 -0
- data/sig/type_alias_regularity.rbs +98 -0
- data/sig/type_param.rbs +110 -0
- data/sig/typename.rbs +79 -0
- data/sig/types.rbs +579 -0
- data/sig/unit_test/convertibles.rbs +154 -0
- data/sig/unit_test/spy.rbs +30 -0
- data/sig/unit_test/type_assertions.rbs +196 -0
- data/sig/unit_test/with_aliases.rbs +136 -0
- data/sig/use_map.rbs +35 -0
- data/sig/util.rbs +9 -0
- data/sig/validator.rbs +63 -0
- data/sig/variance_calculator.rbs +87 -0
- data/sig/vendorer.rbs +51 -0
- data/sig/version.rbs +3 -0
- data/sig/visitor.rbs +47 -0
- data/sig/writer.rbs +127 -0
- data/src/constants.c +153 -0
- data/src/ruby_objs.c +795 -0
- data/src/util/rbs_constant_pool.c +342 -0
- data/stdlib/abbrev/0/abbrev.rbs +66 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +355 -0
- data/stdlib/benchmark/0/benchmark.rbs +452 -0
- data/stdlib/bigdecimal/0/big_decimal.rbs +1629 -0
- data/stdlib/bigdecimal-math/0/big_math.rbs +119 -0
- data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
- data/stdlib/cgi/0/core.rbs +1285 -0
- data/stdlib/cgi/0/manifest.yaml +3 -0
- data/stdlib/coverage/0/coverage.rbs +263 -0
- data/stdlib/csv/0/csv.rbs +3776 -0
- data/stdlib/csv/0/manifest.yaml +3 -0
- data/stdlib/date/0/date.rbs +1585 -0
- data/stdlib/date/0/date_time.rbs +616 -0
- data/stdlib/date/0/time.rbs +26 -0
- data/stdlib/dbm/0/dbm.rbs +421 -0
- data/stdlib/delegate/0/delegator.rbs +184 -0
- data/stdlib/delegate/0/kernel.rbs +47 -0
- data/stdlib/delegate/0/simple_delegator.rbs +96 -0
- data/stdlib/did_you_mean/0/did_you_mean.rbs +343 -0
- data/stdlib/digest/0/digest.rbs +577 -0
- data/stdlib/erb/0/erb.rbs +532 -0
- data/stdlib/etc/0/etc.rbs +865 -0
- data/stdlib/fileutils/0/fileutils.rbs +1734 -0
- data/stdlib/find/0/find.rbs +49 -0
- data/stdlib/forwardable/0/forwardable.rbs +268 -0
- data/stdlib/io-console/0/io-console.rbs +414 -0
- data/stdlib/ipaddr/0/ipaddr.rbs +428 -0
- data/stdlib/json/0/json.rbs +1916 -0
- data/stdlib/kconv/0/kconv.rbs +166 -0
- data/stdlib/logger/0/formatter.rbs +45 -0
- data/stdlib/logger/0/log_device.rbs +100 -0
- data/stdlib/logger/0/logger.rbs +796 -0
- data/stdlib/logger/0/manifest.yaml +2 -0
- data/stdlib/logger/0/period.rbs +17 -0
- data/stdlib/logger/0/severity.rbs +34 -0
- data/stdlib/minitest/0/kernel.rbs +42 -0
- data/stdlib/minitest/0/minitest/abstract_reporter.rbs +52 -0
- data/stdlib/minitest/0/minitest/assertion.rbs +17 -0
- data/stdlib/minitest/0/minitest/assertions.rbs +590 -0
- data/stdlib/minitest/0/minitest/backtrace_filter.rbs +23 -0
- data/stdlib/minitest/0/minitest/bench_spec.rbs +102 -0
- data/stdlib/minitest/0/minitest/benchmark.rbs +259 -0
- data/stdlib/minitest/0/minitest/composite_reporter.rbs +25 -0
- data/stdlib/minitest/0/minitest/compress.rbs +13 -0
- data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
- data/stdlib/minitest/0/minitest/expectation.rbs +2 -0
- data/stdlib/minitest/0/minitest/expectations.rbs +21 -0
- data/stdlib/minitest/0/minitest/guard.rbs +64 -0
- data/stdlib/minitest/0/minitest/mock.rbs +64 -0
- data/stdlib/minitest/0/minitest/parallel/executor.rbs +46 -0
- data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +5 -0
- data/stdlib/minitest/0/minitest/parallel/test.rbs +3 -0
- data/stdlib/minitest/0/minitest/parallel.rbs +2 -0
- data/stdlib/minitest/0/minitest/pride_io.rbs +62 -0
- data/stdlib/minitest/0/minitest/pride_lol.rbs +19 -0
- data/stdlib/minitest/0/minitest/progress_reporter.rbs +11 -0
- data/stdlib/minitest/0/minitest/reportable.rbs +53 -0
- data/stdlib/minitest/0/minitest/reporter.rbs +5 -0
- data/stdlib/minitest/0/minitest/result.rbs +28 -0
- data/stdlib/minitest/0/minitest/runnable.rbs +163 -0
- data/stdlib/minitest/0/minitest/skip.rbs +6 -0
- data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +48 -0
- data/stdlib/minitest/0/minitest/spec/dsl.rbs +129 -0
- data/stdlib/minitest/0/minitest/spec.rbs +11 -0
- data/stdlib/minitest/0/minitest/statistics_reporter.rbs +81 -0
- data/stdlib/minitest/0/minitest/summary_reporter.rbs +18 -0
- data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +92 -0
- data/stdlib/minitest/0/minitest/test.rbs +69 -0
- data/stdlib/minitest/0/minitest/unexpected_error.rbs +12 -0
- data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
- data/stdlib/minitest/0/minitest/unit/test_case.rbs +3 -0
- data/stdlib/minitest/0/minitest/unit.rbs +4 -0
- data/stdlib/minitest/0/minitest.rbs +115 -0
- data/stdlib/monitor/0/monitor.rbs +363 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +104 -0
- data/stdlib/net-http/0/manifest.yaml +3 -0
- data/stdlib/net-http/0/net-http.rbs +5552 -0
- data/stdlib/net-protocol/0/manifest.yaml +2 -0
- data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
- data/stdlib/net-smtp/0/manifest.yaml +2 -0
- data/stdlib/net-smtp/0/net-smtp.rbs +55 -0
- data/stdlib/nkf/0/nkf.rbs +402 -0
- data/stdlib/objspace/0/objspace.rbs +487 -0
- data/stdlib/observable/0/observable.rbs +217 -0
- data/stdlib/open-uri/0/manifest.yaml +4 -0
- data/stdlib/open-uri/0/open-uri.rbs +393 -0
- data/stdlib/open3/0/open3.rbs +147 -0
- data/stdlib/openssl/0/manifest.yaml +3 -0
- data/stdlib/openssl/0/openssl.rbs +12113 -0
- data/stdlib/optparse/0/optparse.rbs +1725 -0
- data/stdlib/pathname/0/pathname.rbs +1406 -0
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +300 -0
- data/stdlib/prettyprint/0/prettyprint.rbs +383 -0
- data/stdlib/pstore/0/pstore.rbs +603 -0
- data/stdlib/psych/0/core_ext.rbs +12 -0
- data/stdlib/psych/0/dbm.rbs +237 -0
- data/stdlib/psych/0/manifest.yaml +3 -0
- data/stdlib/psych/0/psych.rbs +402 -0
- data/stdlib/psych/0/store.rbs +59 -0
- data/stdlib/pty/0/pty.rbs +237 -0
- data/stdlib/rdoc/0/code_object.rbs +51 -0
- data/stdlib/rdoc/0/comment.rbs +59 -0
- data/stdlib/rdoc/0/context.rbs +153 -0
- data/stdlib/rdoc/0/markup.rbs +117 -0
- data/stdlib/rdoc/0/parser.rbs +56 -0
- data/stdlib/rdoc/0/rdoc.rbs +391 -0
- data/stdlib/rdoc/0/ri.rbs +17 -0
- data/stdlib/rdoc/0/store.rbs +48 -0
- data/stdlib/rdoc/0/top_level.rbs +97 -0
- data/stdlib/resolv/0/manifest.yaml +3 -0
- data/stdlib/resolv/0/resolv.rbs +1830 -0
- data/stdlib/ripper/0/ripper.rbs +1648 -0
- data/stdlib/securerandom/0/securerandom.rbs +62 -0
- data/stdlib/shellwords/0/shellwords.rbs +229 -0
- data/stdlib/singleton/0/singleton.rbs +131 -0
- data/stdlib/socket/0/addrinfo.rbs +666 -0
- data/stdlib/socket/0/basic_socket.rbs +590 -0
- data/stdlib/socket/0/constants.rbs +2295 -0
- data/stdlib/socket/0/ip_socket.rbs +92 -0
- data/stdlib/socket/0/socket.rbs +4157 -0
- data/stdlib/socket/0/socket_error.rbs +5 -0
- data/stdlib/socket/0/tcp_server.rbs +192 -0
- data/stdlib/socket/0/tcp_socket.rbs +79 -0
- data/stdlib/socket/0/udp_socket.rbs +133 -0
- data/stdlib/socket/0/unix_server.rbs +169 -0
- data/stdlib/socket/0/unix_socket.rbs +172 -0
- data/stdlib/stringio/0/stringio.rbs +567 -0
- data/stdlib/strscan/0/string_scanner.rbs +1627 -0
- data/stdlib/tempfile/0/tempfile.rbs +479 -0
- data/stdlib/time/0/time.rbs +432 -0
- data/stdlib/timeout/0/timeout.rbs +81 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +69 -0
- data/stdlib/tsort/0/cyclic.rbs +5 -0
- data/stdlib/tsort/0/interfaces.rbs +20 -0
- data/stdlib/tsort/0/tsort.rbs +409 -0
- data/stdlib/uri/0/common.rbs +582 -0
- data/stdlib/uri/0/file.rbs +118 -0
- data/stdlib/uri/0/ftp.rbs +13 -0
- data/stdlib/uri/0/generic.rbs +1108 -0
- data/stdlib/uri/0/http.rbs +104 -0
- data/stdlib/uri/0/https.rbs +14 -0
- data/stdlib/uri/0/ldap.rbs +230 -0
- data/stdlib/uri/0/ldaps.rbs +14 -0
- data/stdlib/uri/0/mailto.rbs +92 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +189 -0
- data/stdlib/uri/0/rfc3986_parser.rbs +2 -0
- data/stdlib/uri/0/ws.rbs +13 -0
- data/stdlib/uri/0/wss.rbs +9 -0
- data/stdlib/yaml/0/manifest.yaml +2 -0
- data/stdlib/yaml/0/yaml.rbs +1 -0
- data/stdlib/zlib/0/buf_error.rbs +10 -0
- data/stdlib/zlib/0/data_error.rbs +10 -0
- data/stdlib/zlib/0/deflate.rbs +210 -0
- data/stdlib/zlib/0/error.rbs +20 -0
- data/stdlib/zlib/0/gzip_file/crc_error.rbs +12 -0
- data/stdlib/zlib/0/gzip_file/error.rbs +23 -0
- data/stdlib/zlib/0/gzip_file/length_error.rbs +12 -0
- data/stdlib/zlib/0/gzip_file/no_footer.rbs +11 -0
- data/stdlib/zlib/0/gzip_file.rbs +156 -0
- data/stdlib/zlib/0/gzip_reader.rbs +293 -0
- data/stdlib/zlib/0/gzip_writer.rbs +166 -0
- data/stdlib/zlib/0/inflate.rbs +180 -0
- data/stdlib/zlib/0/mem_error.rbs +10 -0
- data/stdlib/zlib/0/need_dict.rbs +13 -0
- data/stdlib/zlib/0/stream_end.rbs +11 -0
- data/stdlib/zlib/0/stream_error.rbs +11 -0
- data/stdlib/zlib/0/version_error.rbs +11 -0
- data/stdlib/zlib/0/zlib.rbs +449 -0
- data/stdlib/zlib/0/zstream.rbs +200 -0
- metadata +532 -0
data/core/io.rbs
ADDED
@@ -0,0 +1,3406 @@
|
|
1
|
+
# <!-- rdoc-file=io.c -->
|
2
|
+
# An instance of class IO (commonly called a *stream*) represents an
|
3
|
+
# input/output stream in the underlying operating system. Class IO is the basis
|
4
|
+
# for input and output in Ruby.
|
5
|
+
#
|
6
|
+
# Class File is the only class in the Ruby core that is a subclass of IO. Some
|
7
|
+
# classes in the Ruby standard library are also subclasses of IO; these include
|
8
|
+
# TCPSocket and UDPSocket.
|
9
|
+
#
|
10
|
+
# The global constant ARGF (also accessible as `$<`) provides an IO-like stream
|
11
|
+
# that allows access to all file paths found in ARGV (or found in STDIN if ARGV
|
12
|
+
# is empty). ARGF is not itself a subclass of IO.
|
13
|
+
#
|
14
|
+
# Class StringIO provides an IO-like stream that handles a String. StringIO is
|
15
|
+
# not itself a subclass of IO.
|
16
|
+
#
|
17
|
+
# Important objects based on IO include:
|
18
|
+
#
|
19
|
+
# * $stdin.
|
20
|
+
# * $stdout.
|
21
|
+
# * $stderr.
|
22
|
+
# * Instances of class File.
|
23
|
+
#
|
24
|
+
# An instance of IO may be created using:
|
25
|
+
#
|
26
|
+
# * IO.new: returns a new IO object for the given integer file descriptor.
|
27
|
+
# * IO.open: passes a new IO object to the given block.
|
28
|
+
# * IO.popen: returns a new IO object that is connected to the $stdin and
|
29
|
+
# $stdout of a newly-launched subprocess.
|
30
|
+
# * Kernel#open: Returns a new IO object connected to a given source: stream,
|
31
|
+
# file, or subprocess.
|
32
|
+
#
|
33
|
+
# Like a File stream, an IO stream has:
|
34
|
+
#
|
35
|
+
# * A read/write mode, which may be read-only, write-only, or read/write; see
|
36
|
+
# [Read/Write Mode](rdoc-ref:File@Read-2FWrite+Mode).
|
37
|
+
# * A data mode, which may be text-only or binary; see [Data
|
38
|
+
# Mode](rdoc-ref:File@Data+Mode).
|
39
|
+
# * Internal and external encodings; see [Encodings](rdoc-ref:File@Encodings).
|
40
|
+
#
|
41
|
+
# And like other IO streams, it has:
|
42
|
+
#
|
43
|
+
# * A position, which determines where in the stream the next read or write is
|
44
|
+
# to occur; see [Position](rdoc-ref:IO@Position).
|
45
|
+
# * A line number, which is a special, line-oriented, "position" (different
|
46
|
+
# from the position mentioned above); see [Line
|
47
|
+
# Number](rdoc-ref:IO@Line+Number).
|
48
|
+
#
|
49
|
+
# ## Extension `io/console`
|
50
|
+
#
|
51
|
+
# Extension `io/console` provides numerous methods for interacting with the
|
52
|
+
# console; requiring it adds numerous methods to class IO.
|
53
|
+
#
|
54
|
+
# ## Example Files
|
55
|
+
#
|
56
|
+
# Many examples here use these variables:
|
57
|
+
#
|
58
|
+
# # English text with newlines.
|
59
|
+
# text = <<~EOT
|
60
|
+
# First line
|
61
|
+
# Second line
|
62
|
+
#
|
63
|
+
# Fourth line
|
64
|
+
# Fifth line
|
65
|
+
# EOT
|
66
|
+
#
|
67
|
+
# # Russian text.
|
68
|
+
# russian = "\u{442 435 441 442}" # => "тест"
|
69
|
+
#
|
70
|
+
# # Binary data.
|
71
|
+
# data = "\u9990\u9991\u9992\u9993\u9994"
|
72
|
+
#
|
73
|
+
# # Text file.
|
74
|
+
# File.write('t.txt', text)
|
75
|
+
#
|
76
|
+
# # File with Russian text.
|
77
|
+
# File.write('t.rus', russian)
|
78
|
+
#
|
79
|
+
# # File with binary data.
|
80
|
+
# f = File.new('t.dat', 'wb:UTF-16')
|
81
|
+
# f.write(data)
|
82
|
+
# f.close
|
83
|
+
#
|
84
|
+
# ## Open Options
|
85
|
+
#
|
86
|
+
# A number of IO methods accept optional keyword arguments that determine how a
|
87
|
+
# new stream is to be opened:
|
88
|
+
#
|
89
|
+
# * `:mode`: Stream mode.
|
90
|
+
# * `:flags`: Integer file open flags; If `mode` is also given, the two are
|
91
|
+
# bitwise-ORed.
|
92
|
+
# * `:external_encoding`: External encoding for the stream.
|
93
|
+
# * `:internal_encoding`: Internal encoding for the stream. `'-'` is a synonym
|
94
|
+
# for the default internal encoding. If the value is `nil` no conversion
|
95
|
+
# occurs.
|
96
|
+
# * `:encoding`: Specifies external and internal encodings as
|
97
|
+
# `'extern:intern'`.
|
98
|
+
# * `:textmode`: If a truthy value, specifies the mode as text-only, binary
|
99
|
+
# otherwise.
|
100
|
+
# * `:binmode`: If a truthy value, specifies the mode as binary, text-only
|
101
|
+
# otherwise.
|
102
|
+
# * `:autoclose`: If a truthy value, specifies that the `fd` will close when
|
103
|
+
# the stream closes; otherwise it remains open.
|
104
|
+
# * `:path:` If a string value is provided, it is used in #inspect and is
|
105
|
+
# available as #path method.
|
106
|
+
#
|
107
|
+
# Also available are the options offered in String#encode, which may control
|
108
|
+
# conversion between external and internal encoding.
|
109
|
+
#
|
110
|
+
# ## Basic IO
|
111
|
+
#
|
112
|
+
# You can perform basic stream IO with these methods, which typically operate on
|
113
|
+
# multi-byte strings:
|
114
|
+
#
|
115
|
+
# * IO#read: Reads and returns some or all of the remaining bytes from the
|
116
|
+
# stream.
|
117
|
+
# * IO#write: Writes zero or more strings to the stream; each given object
|
118
|
+
# that is not already a string is converted via `to_s`.
|
119
|
+
#
|
120
|
+
# ### Position
|
121
|
+
#
|
122
|
+
# An IO stream has a nonnegative integer *position*, which is the byte offset at
|
123
|
+
# which the next read or write is to occur. A new stream has position zero (and
|
124
|
+
# line number zero); method `rewind` resets the position (and line number) to
|
125
|
+
# zero.
|
126
|
+
#
|
127
|
+
# These methods discard [buffers](rdoc-ref:IO@Buffering) and the
|
128
|
+
# Encoding::Converter instances used for that IO.
|
129
|
+
#
|
130
|
+
# The relevant methods:
|
131
|
+
#
|
132
|
+
# * IO#tell (aliased as `#pos`): Returns the current position (in bytes) in
|
133
|
+
# the stream.
|
134
|
+
# * IO#pos=: Sets the position of the stream to a given integer `new_position`
|
135
|
+
# (in bytes).
|
136
|
+
# * IO#seek: Sets the position of the stream to a given integer `offset` (in
|
137
|
+
# bytes), relative to a given position `whence` (indicating the beginning,
|
138
|
+
# end, or current position).
|
139
|
+
# * IO#rewind: Positions the stream at the beginning (also resetting the line
|
140
|
+
# number).
|
141
|
+
#
|
142
|
+
# ### Open and Closed Streams
|
143
|
+
#
|
144
|
+
# A new IO stream may be open for reading, open for writing, or both.
|
145
|
+
#
|
146
|
+
# A stream is automatically closed when claimed by the garbage collector.
|
147
|
+
#
|
148
|
+
# Attempted reading or writing on a closed stream raises an exception.
|
149
|
+
#
|
150
|
+
# The relevant methods:
|
151
|
+
#
|
152
|
+
# * IO#close: Closes the stream for both reading and writing.
|
153
|
+
# * IO#close_read: Closes the stream for reading.
|
154
|
+
# * IO#close_write: Closes the stream for writing.
|
155
|
+
# * IO#closed?: Returns whether the stream is closed.
|
156
|
+
#
|
157
|
+
# ### End-of-Stream
|
158
|
+
#
|
159
|
+
# You can query whether a stream is positioned at its end:
|
160
|
+
#
|
161
|
+
# * IO#eof? (also aliased as `#eof`): Returns whether the stream is at
|
162
|
+
# end-of-stream.
|
163
|
+
#
|
164
|
+
# You can reposition to end-of-stream by using method IO#seek:
|
165
|
+
#
|
166
|
+
# f = File.new('t.txt')
|
167
|
+
# f.eof? # => false
|
168
|
+
# f.seek(0, :END)
|
169
|
+
# f.eof? # => true
|
170
|
+
# f.close
|
171
|
+
#
|
172
|
+
# Or by reading all stream content (which is slower than using IO#seek):
|
173
|
+
#
|
174
|
+
# f.rewind
|
175
|
+
# f.eof? # => false
|
176
|
+
# f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
177
|
+
# f.eof? # => true
|
178
|
+
#
|
179
|
+
# ## Line IO
|
180
|
+
#
|
181
|
+
# Class IO supports line-oriented [input](rdoc-ref:IO@Line+Input) and
|
182
|
+
# [output](rdoc-ref:IO@Line+Output)
|
183
|
+
#
|
184
|
+
# ### Line Input
|
185
|
+
#
|
186
|
+
# Class IO supports line-oriented input for [files](rdoc-ref:IO@File+Line+Input)
|
187
|
+
# and [IO streams](rdoc-ref:IO@Stream+Line+Input)
|
188
|
+
#
|
189
|
+
# #### File Line Input
|
190
|
+
#
|
191
|
+
# You can read lines from a file using these methods:
|
192
|
+
#
|
193
|
+
# * IO.foreach: Reads each line and passes it to the given block.
|
194
|
+
# * IO.readlines: Reads and returns all lines in an array.
|
195
|
+
#
|
196
|
+
# For each of these methods:
|
197
|
+
#
|
198
|
+
# * You can specify [open options](rdoc-ref:IO@Open+Options).
|
199
|
+
# * Line parsing depends on the effective *line separator*; see [Line
|
200
|
+
# Separator](rdoc-ref:IO@Line+Separator).
|
201
|
+
# * The length of each returned line depends on the effective *line limit*;
|
202
|
+
# see [Line Limit](rdoc-ref:IO@Line+Limit).
|
203
|
+
#
|
204
|
+
# #### Stream Line Input
|
205
|
+
#
|
206
|
+
# You can read lines from an IO stream using these methods:
|
207
|
+
#
|
208
|
+
# * IO#each_line: Reads each remaining line, passing it to the given block.
|
209
|
+
# * IO#gets: Returns the next line.
|
210
|
+
# * IO#readline: Like #gets, but raises an exception at end-of-stream.
|
211
|
+
# * IO#readlines: Returns all remaining lines in an array.
|
212
|
+
#
|
213
|
+
# For each of these methods:
|
214
|
+
#
|
215
|
+
# * Reading may begin mid-line, depending on the stream's *position*; see
|
216
|
+
# [Position](rdoc-ref:IO@Position).
|
217
|
+
# * Line parsing depends on the effective *line separator*; see [Line
|
218
|
+
# Separator](rdoc-ref:IO@Line+Separator).
|
219
|
+
# * The length of each returned line depends on the effective *line limit*;
|
220
|
+
# see [Line Limit](rdoc-ref:IO@Line+Limit).
|
221
|
+
#
|
222
|
+
# ##### Line Separator
|
223
|
+
#
|
224
|
+
# Each of the [line input methods](rdoc-ref:IO@Line+Input) uses a *line
|
225
|
+
# separator*: the string that determines what is considered a line; it is
|
226
|
+
# sometimes called the *input record separator*.
|
227
|
+
#
|
228
|
+
# The default line separator is taken from global variable `$/`, whose initial
|
229
|
+
# value is `"\n"`.
|
230
|
+
#
|
231
|
+
# Generally, the line to be read next is all data from the current
|
232
|
+
# [position](rdoc-ref:IO@Position) to the next line separator (but see [Special
|
233
|
+
# Line Separator Values](rdoc-ref:IO@Special+Line+Separator+Values)):
|
234
|
+
#
|
235
|
+
# f = File.new('t.txt')
|
236
|
+
# # Method gets with no sep argument returns the next line, according to $/.
|
237
|
+
# f.gets # => "First line\n"
|
238
|
+
# f.gets # => "Second line\n"
|
239
|
+
# f.gets # => "\n"
|
240
|
+
# f.gets # => "Fourth line\n"
|
241
|
+
# f.gets # => "Fifth line\n"
|
242
|
+
# f.close
|
243
|
+
#
|
244
|
+
# You can use a different line separator by passing argument `sep`:
|
245
|
+
#
|
246
|
+
# f = File.new('t.txt')
|
247
|
+
# f.gets('l') # => "First l"
|
248
|
+
# f.gets('li') # => "ine\nSecond li"
|
249
|
+
# f.gets('lin') # => "ne\n\nFourth lin"
|
250
|
+
# f.gets # => "e\n"
|
251
|
+
# f.close
|
252
|
+
#
|
253
|
+
# Or by setting global variable `$/`:
|
254
|
+
#
|
255
|
+
# f = File.new('t.txt')
|
256
|
+
# $/ = 'l'
|
257
|
+
# f.gets # => "First l"
|
258
|
+
# f.gets # => "ine\nSecond l"
|
259
|
+
# f.gets # => "ine\n\nFourth l"
|
260
|
+
# f.close
|
261
|
+
#
|
262
|
+
# ##### Special Line Separator Values
|
263
|
+
#
|
264
|
+
# Each of the [line input methods](rdoc-ref:IO@Line+Input) accepts two special
|
265
|
+
# values for parameter `sep`:
|
266
|
+
#
|
267
|
+
# * `nil`: The entire stream is to be read ("slurped") into a single string:
|
268
|
+
#
|
269
|
+
# f = File.new('t.txt')
|
270
|
+
# f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
271
|
+
# f.close
|
272
|
+
#
|
273
|
+
# * `''` (the empty string): The next "paragraph" is to be read (paragraphs
|
274
|
+
# being separated by two consecutive line separators):
|
275
|
+
#
|
276
|
+
# f = File.new('t.txt')
|
277
|
+
# f.gets('') # => "First line\nSecond line\n\n"
|
278
|
+
# f.gets('') # => "Fourth line\nFifth line\n"
|
279
|
+
# f.close
|
280
|
+
#
|
281
|
+
# ##### Line Limit
|
282
|
+
#
|
283
|
+
# Each of the [line input methods](rdoc-ref:IO@Line+Input) uses an integer *line
|
284
|
+
# limit*, which restricts the number of bytes that may be returned. (A
|
285
|
+
# multi-byte character will not be split, and so a returned line may be slightly
|
286
|
+
# longer than the limit).
|
287
|
+
#
|
288
|
+
# The default limit value is `-1`; any negative limit value means that there is
|
289
|
+
# no limit.
|
290
|
+
#
|
291
|
+
# If there is no limit, the line is determined only by `sep`.
|
292
|
+
#
|
293
|
+
# # Text with 1-byte characters.
|
294
|
+
# File.open('t.txt') {|f| f.gets(1) } # => "F"
|
295
|
+
# File.open('t.txt') {|f| f.gets(2) } # => "Fi"
|
296
|
+
# File.open('t.txt') {|f| f.gets(3) } # => "Fir"
|
297
|
+
# File.open('t.txt') {|f| f.gets(4) } # => "Firs"
|
298
|
+
# # No more than one line.
|
299
|
+
# File.open('t.txt') {|f| f.gets(10) } # => "First line"
|
300
|
+
# File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
|
301
|
+
# File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
|
302
|
+
#
|
303
|
+
# # Text with 2-byte characters, which will not be split.
|
304
|
+
# File.open('t.rus') {|f| f.gets(1).size } # => 1
|
305
|
+
# File.open('t.rus') {|f| f.gets(2).size } # => 1
|
306
|
+
# File.open('t.rus') {|f| f.gets(3).size } # => 2
|
307
|
+
# File.open('t.rus') {|f| f.gets(4).size } # => 2
|
308
|
+
#
|
309
|
+
# ##### Line Separator and Line Limit
|
310
|
+
#
|
311
|
+
# With arguments `sep` and `limit` given, combines the two behaviors:
|
312
|
+
#
|
313
|
+
# * Returns the next line as determined by line separator `sep`.
|
314
|
+
# * But returns no more bytes than are allowed by the limit `limit`.
|
315
|
+
#
|
316
|
+
# Example:
|
317
|
+
#
|
318
|
+
# File.open('t.txt') {|f| f.gets('li', 20) } # => "First li"
|
319
|
+
# File.open('t.txt') {|f| f.gets('li', 2) } # => "Fi"
|
320
|
+
#
|
321
|
+
# ##### Line Number
|
322
|
+
#
|
323
|
+
# A readable IO stream has a non-negative integer *line number*:
|
324
|
+
#
|
325
|
+
# * IO#lineno: Returns the line number.
|
326
|
+
# * IO#lineno=: Resets and returns the line number.
|
327
|
+
#
|
328
|
+
# Unless modified by a call to method IO#lineno=, the line number is the number
|
329
|
+
# of lines read by certain line-oriented methods, according to the effective
|
330
|
+
# [line separator](rdoc-ref:IO@Line+Separator):
|
331
|
+
#
|
332
|
+
# * IO.foreach: Increments the line number on each call to the block.
|
333
|
+
# * IO#each_line: Increments the line number on each call to the block.
|
334
|
+
# * IO#gets: Increments the line number.
|
335
|
+
# * IO#readline: Increments the line number.
|
336
|
+
# * IO#readlines: Increments the line number for each line read.
|
337
|
+
#
|
338
|
+
# A new stream is initially has line number zero (and position zero); method
|
339
|
+
# `rewind` resets the line number (and position) to zero:
|
340
|
+
#
|
341
|
+
# f = File.new('t.txt')
|
342
|
+
# f.lineno # => 0
|
343
|
+
# f.gets # => "First line\n"
|
344
|
+
# f.lineno # => 1
|
345
|
+
# f.rewind
|
346
|
+
# f.lineno # => 0
|
347
|
+
# f.close
|
348
|
+
#
|
349
|
+
# Reading lines from a stream usually changes its line number:
|
350
|
+
#
|
351
|
+
# f = File.new('t.txt', 'r')
|
352
|
+
# f.lineno # => 0
|
353
|
+
# f.readline # => "This is line one.\n"
|
354
|
+
# f.lineno # => 1
|
355
|
+
# f.readline # => "This is the second line.\n"
|
356
|
+
# f.lineno # => 2
|
357
|
+
# f.readline # => "Here's the third line.\n"
|
358
|
+
# f.lineno # => 3
|
359
|
+
# f.eof? # => true
|
360
|
+
# f.close
|
361
|
+
#
|
362
|
+
# Iterating over lines in a stream usually changes its line number:
|
363
|
+
#
|
364
|
+
# File.open('t.txt') do |f|
|
365
|
+
# f.each_line do |line|
|
366
|
+
# p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}"
|
367
|
+
# end
|
368
|
+
# end
|
369
|
+
#
|
370
|
+
# Output:
|
371
|
+
#
|
372
|
+
# "position=11 eof?=false lineno=1"
|
373
|
+
# "position=23 eof?=false lineno=2"
|
374
|
+
# "position=24 eof?=false lineno=3"
|
375
|
+
# "position=36 eof?=false lineno=4"
|
376
|
+
# "position=47 eof?=true lineno=5"
|
377
|
+
#
|
378
|
+
# Unlike the stream's [position](rdoc-ref:IO@Position), the line number does not
|
379
|
+
# affect where the next read or write will occur:
|
380
|
+
#
|
381
|
+
# f = File.new('t.txt')
|
382
|
+
# f.lineno = 1000
|
383
|
+
# f.lineno # => 1000
|
384
|
+
# f.gets # => "First line\n"
|
385
|
+
# f.lineno # => 1001
|
386
|
+
# f.close
|
387
|
+
#
|
388
|
+
# Associated with the line number is the global variable `$.`:
|
389
|
+
#
|
390
|
+
# * When a stream is opened, `$.` is not set; its value is left over from
|
391
|
+
# previous activity in the process:
|
392
|
+
#
|
393
|
+
# $. = 41
|
394
|
+
# f = File.new('t.txt')
|
395
|
+
# $. = 41
|
396
|
+
# # => 41
|
397
|
+
# f.close
|
398
|
+
#
|
399
|
+
# * When a stream is read, `$.` is set to the line number for that stream:
|
400
|
+
#
|
401
|
+
# f0 = File.new('t.txt')
|
402
|
+
# f1 = File.new('t.dat')
|
403
|
+
# f0.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
|
404
|
+
# $. # => 5
|
405
|
+
# f1.readlines # => ["\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"]
|
406
|
+
# $. # => 1
|
407
|
+
# f0.close
|
408
|
+
# f1.close
|
409
|
+
#
|
410
|
+
# * Methods IO#rewind and IO#seek do not affect `$.`:
|
411
|
+
#
|
412
|
+
# f = File.new('t.txt')
|
413
|
+
# f.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
|
414
|
+
# $. # => 5
|
415
|
+
# f.rewind
|
416
|
+
# f.seek(0, :SET)
|
417
|
+
# $. # => 5
|
418
|
+
# f.close
|
419
|
+
#
|
420
|
+
# ### Line Output
|
421
|
+
#
|
422
|
+
# You can write to an IO stream line-by-line using this method:
|
423
|
+
#
|
424
|
+
# * IO#puts: Writes objects to the stream.
|
425
|
+
#
|
426
|
+
# ## Character IO
|
427
|
+
#
|
428
|
+
# You can process an IO stream character-by-character using these methods:
|
429
|
+
#
|
430
|
+
# * IO#getc: Reads and returns the next character from the stream.
|
431
|
+
# * IO#readchar: Like #getc, but raises an exception at end-of-stream.
|
432
|
+
# * IO#ungetc: Pushes back ("unshifts") a character or integer onto the
|
433
|
+
# stream.
|
434
|
+
# * IO#putc: Writes a character to the stream.
|
435
|
+
# * IO#each_char: Reads each remaining character in the stream, passing the
|
436
|
+
# character to the given block.
|
437
|
+
#
|
438
|
+
# ## Byte IO
|
439
|
+
#
|
440
|
+
# You can process an IO stream byte-by-byte using these methods:
|
441
|
+
#
|
442
|
+
# * IO#getbyte: Returns the next 8-bit byte as an integer in range 0..255.
|
443
|
+
# * IO#readbyte: Like #getbyte, but raises an exception if at end-of-stream.
|
444
|
+
# * IO#ungetbyte: Pushes back ("unshifts") a byte back onto the stream.
|
445
|
+
# * IO#each_byte: Reads each remaining byte in the stream, passing the byte to
|
446
|
+
# the given block.
|
447
|
+
#
|
448
|
+
# ## Codepoint IO
|
449
|
+
#
|
450
|
+
# You can process an IO stream codepoint-by-codepoint:
|
451
|
+
#
|
452
|
+
# * IO#each_codepoint: Reads each remaining codepoint, passing it to the given
|
453
|
+
# block.
|
454
|
+
#
|
455
|
+
# ## What's Here
|
456
|
+
#
|
457
|
+
# First, what's elsewhere. Class IO:
|
458
|
+
#
|
459
|
+
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
|
460
|
+
# * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
|
461
|
+
# provides dozens of additional methods.
|
462
|
+
#
|
463
|
+
# Here, class IO provides methods that are useful for:
|
464
|
+
#
|
465
|
+
# * [Creating](rdoc-ref:IO@Creating)
|
466
|
+
# * [Reading](rdoc-ref:IO@Reading)
|
467
|
+
# * [Writing](rdoc-ref:IO@Writing)
|
468
|
+
# * [Positioning](rdoc-ref:IO@Positioning)
|
469
|
+
# * [Iterating](rdoc-ref:IO@Iterating)
|
470
|
+
# * [Settings](rdoc-ref:IO@Settings)
|
471
|
+
# * [Querying](rdoc-ref:IO@Querying)
|
472
|
+
# * [Buffering](rdoc-ref:IO@Buffering)
|
473
|
+
# * [Low-Level Access](rdoc-ref:IO@Low-Level+Access)
|
474
|
+
# * [Other](rdoc-ref:IO@Other)
|
475
|
+
#
|
476
|
+
# ### Creating
|
477
|
+
#
|
478
|
+
# * ::new (aliased as ::for_fd): Creates and returns a new IO object for the
|
479
|
+
# given integer file descriptor.
|
480
|
+
# * ::open: Creates a new IO object.
|
481
|
+
# * ::pipe: Creates a connected pair of reader and writer IO objects.
|
482
|
+
# * ::popen: Creates an IO object to interact with a subprocess.
|
483
|
+
# * ::select: Selects which given IO instances are ready for reading, writing,
|
484
|
+
# or have pending exceptions.
|
485
|
+
#
|
486
|
+
# ### Reading
|
487
|
+
#
|
488
|
+
# * ::binread: Returns a binary string with all or a subset of bytes from the
|
489
|
+
# given file.
|
490
|
+
# * ::read: Returns a string with all or a subset of bytes from the given
|
491
|
+
# file.
|
492
|
+
# * ::readlines: Returns an array of strings, which are the lines from the
|
493
|
+
# given file.
|
494
|
+
# * #getbyte: Returns the next 8-bit byte read from `self` as an integer.
|
495
|
+
# * #getc: Returns the next character read from `self` as a string.
|
496
|
+
# * #gets: Returns the line read from `self`.
|
497
|
+
# * #pread: Returns all or the next *n* bytes read from `self`, not updating
|
498
|
+
# the receiver's offset.
|
499
|
+
# * #read: Returns all remaining or the next *n* bytes read from `self` for a
|
500
|
+
# given *n*.
|
501
|
+
# * #read_nonblock: the next *n* bytes read from `self` for a given *n*, in
|
502
|
+
# non-block mode.
|
503
|
+
# * #readbyte: Returns the next byte read from `self`; same as #getbyte, but
|
504
|
+
# raises an exception on end-of-stream.
|
505
|
+
# * #readchar: Returns the next character read from `self`; same as #getc, but
|
506
|
+
# raises an exception on end-of-stream.
|
507
|
+
# * #readline: Returns the next line read from `self`; same as #getline, but
|
508
|
+
# raises an exception of end-of-stream.
|
509
|
+
# * #readlines: Returns an array of all lines read read from `self`.
|
510
|
+
# * #readpartial: Returns up to the given number of bytes from `self`.
|
511
|
+
#
|
512
|
+
# ### Writing
|
513
|
+
#
|
514
|
+
# * ::binwrite: Writes the given string to the file at the given filepath, in
|
515
|
+
# binary mode.
|
516
|
+
# * ::write: Writes the given string to `self`.
|
517
|
+
# * #<<: Appends the given string to `self`.
|
518
|
+
# * #print: Prints last read line or given objects to `self`.
|
519
|
+
# * #printf: Writes to `self` based on the given format string and objects.
|
520
|
+
# * #putc: Writes a character to `self`.
|
521
|
+
# * #puts: Writes lines to `self`, making sure line ends with a newline.
|
522
|
+
# * #pwrite: Writes the given string at the given offset, not updating the
|
523
|
+
# receiver's offset.
|
524
|
+
# * #write: Writes one or more given strings to `self`.
|
525
|
+
# * #write_nonblock: Writes one or more given strings to `self` in
|
526
|
+
# non-blocking mode.
|
527
|
+
#
|
528
|
+
# ### Positioning
|
529
|
+
#
|
530
|
+
# * #lineno: Returns the current line number in `self`.
|
531
|
+
# * #lineno=: Sets the line number is `self`.
|
532
|
+
# * #pos (aliased as #tell): Returns the current byte offset in `self`.
|
533
|
+
# * #pos=: Sets the byte offset in `self`.
|
534
|
+
# * #reopen: Reassociates `self` with a new or existing IO stream.
|
535
|
+
# * #rewind: Positions `self` to the beginning of input.
|
536
|
+
# * #seek: Sets the offset for `self` relative to given position.
|
537
|
+
#
|
538
|
+
# ### Iterating
|
539
|
+
#
|
540
|
+
# * ::foreach: Yields each line of given file to the block.
|
541
|
+
# * #each (aliased as #each_line): Calls the given block with each successive
|
542
|
+
# line in `self`.
|
543
|
+
# * #each_byte: Calls the given block with each successive byte in `self` as
|
544
|
+
# an integer.
|
545
|
+
# * #each_char: Calls the given block with each successive character in `self`
|
546
|
+
# as a string.
|
547
|
+
# * #each_codepoint: Calls the given block with each successive codepoint in
|
548
|
+
# `self` as an integer.
|
549
|
+
#
|
550
|
+
# ### Settings
|
551
|
+
#
|
552
|
+
# * #autoclose=: Sets whether `self` auto-closes.
|
553
|
+
# * #binmode: Sets `self` to binary mode.
|
554
|
+
# * #close: Closes `self`.
|
555
|
+
# * #close_on_exec=: Sets the close-on-exec flag.
|
556
|
+
# * #close_read: Closes `self` for reading.
|
557
|
+
# * #close_write: Closes `self` for writing.
|
558
|
+
# * #set_encoding: Sets the encoding for `self`.
|
559
|
+
# * #set_encoding_by_bom: Sets the encoding for `self`, based on its Unicode
|
560
|
+
# byte-order-mark.
|
561
|
+
# * #sync=: Sets the sync-mode to the given value.
|
562
|
+
#
|
563
|
+
# ### Querying
|
564
|
+
#
|
565
|
+
# * #autoclose?: Returns whether `self` auto-closes.
|
566
|
+
# * #binmode?: Returns whether `self` is in binary mode.
|
567
|
+
# * #close_on_exec?: Returns the close-on-exec flag for `self`.
|
568
|
+
# * #closed?: Returns whether `self` is closed.
|
569
|
+
# * #eof? (aliased as #eof): Returns whether `self` is at end-of-stream.
|
570
|
+
# * #external_encoding: Returns the external encoding object for `self`.
|
571
|
+
# * #fileno (aliased as #to_i): Returns the integer file descriptor for `self`
|
572
|
+
# * #internal_encoding: Returns the internal encoding object for `self`.
|
573
|
+
# * #pid: Returns the process ID of a child process associated with `self`, if
|
574
|
+
# `self` was created by ::popen.
|
575
|
+
# * #stat: Returns the File::Stat object containing status information for
|
576
|
+
# `self`.
|
577
|
+
# * #sync: Returns whether `self` is in sync-mode.
|
578
|
+
# * #tty? (aliased as #isatty): Returns whether `self` is a terminal.
|
579
|
+
#
|
580
|
+
# ### Buffering
|
581
|
+
#
|
582
|
+
# * #fdatasync: Immediately writes all buffered data in `self` to disk.
|
583
|
+
# * #flush: Flushes any buffered data within `self` to the underlying
|
584
|
+
# operating system.
|
585
|
+
# * #fsync: Immediately writes all buffered data and attributes in `self` to
|
586
|
+
# disk.
|
587
|
+
# * #ungetbyte: Prepends buffer for `self` with given integer byte or string.
|
588
|
+
# * #ungetc: Prepends buffer for `self` with given string.
|
589
|
+
#
|
590
|
+
# ### Low-Level Access
|
591
|
+
#
|
592
|
+
# * ::sysopen: Opens the file given by its path, returning the integer file
|
593
|
+
# descriptor.
|
594
|
+
# * #advise: Announces the intention to access data from `self` in a specific
|
595
|
+
# way.
|
596
|
+
# * #fcntl: Passes a low-level command to the file specified by the given file
|
597
|
+
# descriptor.
|
598
|
+
# * #ioctl: Passes a low-level command to the device specified by the given
|
599
|
+
# file descriptor.
|
600
|
+
# * #sysread: Returns up to the next *n* bytes read from self using a
|
601
|
+
# low-level read.
|
602
|
+
# * #sysseek: Sets the offset for `self`.
|
603
|
+
# * #syswrite: Writes the given string to `self` using a low-level write.
|
604
|
+
#
|
605
|
+
# ### Other
|
606
|
+
#
|
607
|
+
# * ::copy_stream: Copies data from a source to a destination, each of which
|
608
|
+
# is a filepath or an IO-like object.
|
609
|
+
# * ::try_convert: Returns a new IO object resulting from converting the given
|
610
|
+
# object.
|
611
|
+
# * #inspect: Returns the string representation of `self`.
|
612
|
+
#
|
613
|
+
%a{annotate:rdoc:source:from=io.c}
|
614
|
+
class IO < Object
|
615
|
+
include File::Constants
|
616
|
+
|
617
|
+
include Enumerable[String]
|
618
|
+
|
619
|
+
# <!--
|
620
|
+
# rdoc-file=io.c
|
621
|
+
# - self << object -> self
|
622
|
+
# -->
|
623
|
+
# Writes the given `object` to `self`, which must be opened for writing (see
|
624
|
+
# [Access Modes](rdoc-ref:File@Access+Modes)); returns `self`; if `object` is
|
625
|
+
# not a string, it is converted via method `to_s`:
|
626
|
+
#
|
627
|
+
# $stdout << 'Hello' << ', ' << 'World!' << "\n"
|
628
|
+
# $stdout << 'foo' << :bar << 2 << "\n"
|
629
|
+
#
|
630
|
+
# Output:
|
631
|
+
#
|
632
|
+
# Hello, World!
|
633
|
+
# foobar2
|
634
|
+
#
|
635
|
+
def <<: (_ToS obj) -> self
|
636
|
+
|
637
|
+
# <!--
|
638
|
+
# rdoc-file=io.c
|
639
|
+
# - advise(advice, offset = 0, len = 0) -> nil
|
640
|
+
# -->
|
641
|
+
# Invokes Posix system call
|
642
|
+
# [posix_fadvise(2)](https://linux.die.net/man/2/posix_fadvise), which announces
|
643
|
+
# an intention to access data from the current file in a particular manner.
|
644
|
+
#
|
645
|
+
# The arguments and results are platform-dependent.
|
646
|
+
#
|
647
|
+
# The relevant data is specified by:
|
648
|
+
#
|
649
|
+
# * `offset`: The offset of the first byte of data.
|
650
|
+
# * `len`: The number of bytes to be accessed; if `len` is zero, or is larger
|
651
|
+
# than the number of bytes remaining, all remaining bytes will be accessed.
|
652
|
+
#
|
653
|
+
# Argument `advice` is one of the following symbols:
|
654
|
+
#
|
655
|
+
# * `:normal`: The application has no advice to give about its access pattern
|
656
|
+
# for the specified data. If no advice is given for an open file, this is
|
657
|
+
# the default assumption.
|
658
|
+
# * `:sequential`: The application expects to access the specified data
|
659
|
+
# sequentially (with lower offsets read before higher ones).
|
660
|
+
# * `:random`: The specified data will be accessed in random order.
|
661
|
+
# * `:noreuse`: The specified data will be accessed only once.
|
662
|
+
# * `:willneed`: The specified data will be accessed in the near future.
|
663
|
+
# * `:dontneed`: The specified data will not be accessed in the near future.
|
664
|
+
#
|
665
|
+
# Not implemented on all platforms.
|
666
|
+
#
|
667
|
+
def advise: (:normal | :sequential | :random | :willneed | :dontneed | :noreuse advise, ?Integer offset, ?Integer len) -> nil
|
668
|
+
|
669
|
+
# <!--
|
670
|
+
# rdoc-file=io.c
|
671
|
+
# - io.autoclose = bool -> true or false
|
672
|
+
# -->
|
673
|
+
# Sets auto-close flag.
|
674
|
+
#
|
675
|
+
# f = File.open(File::NULL)
|
676
|
+
# IO.for_fd(f.fileno).close
|
677
|
+
# f.gets # raises Errno::EBADF
|
678
|
+
#
|
679
|
+
# f = File.open(File::NULL)
|
680
|
+
# g = IO.for_fd(f.fileno)
|
681
|
+
# g.autoclose = false
|
682
|
+
# g.close
|
683
|
+
# f.gets # won't cause Errno::EBADF
|
684
|
+
#
|
685
|
+
def autoclose=: (boolish bool) -> boolish
|
686
|
+
|
687
|
+
# <!--
|
688
|
+
# rdoc-file=io.c
|
689
|
+
# - ios.autoclose? -> true or false
|
690
|
+
# -->
|
691
|
+
# Returns `true` if the underlying file descriptor of *ios* will be closed at
|
692
|
+
# its finalization or at calling #close, otherwise `false`.
|
693
|
+
#
|
694
|
+
def autoclose?: () -> bool
|
695
|
+
|
696
|
+
# <!--
|
697
|
+
# rdoc-file=io.c
|
698
|
+
# - binmode -> self
|
699
|
+
# -->
|
700
|
+
# Sets the stream's data mode as binary (see [Data
|
701
|
+
# Mode](rdoc-ref:File@Data+Mode)).
|
702
|
+
#
|
703
|
+
# A stream's data mode may not be changed from binary to text.
|
704
|
+
#
|
705
|
+
def binmode: () -> self
|
706
|
+
|
707
|
+
# <!--
|
708
|
+
# rdoc-file=io.c
|
709
|
+
# - binmode? -> true or false
|
710
|
+
# -->
|
711
|
+
# Returns `true` if the stream is on binary mode, `false` otherwise. See [Data
|
712
|
+
# Mode](rdoc-ref:File@Data+Mode).
|
713
|
+
#
|
714
|
+
def binmode?: () -> bool
|
715
|
+
|
716
|
+
# <!--
|
717
|
+
# rdoc-file=io.c
|
718
|
+
# - close -> nil
|
719
|
+
# -->
|
720
|
+
# Closes the stream for both reading and writing if open for either or both;
|
721
|
+
# returns `nil`. See [Open and Closed
|
722
|
+
# Streams](rdoc-ref:IO@Open+and+Closed+Streams).
|
723
|
+
#
|
724
|
+
# If the stream is open for writing, flushes any buffered writes to the
|
725
|
+
# operating system before closing.
|
726
|
+
#
|
727
|
+
# If the stream was opened by IO.popen, sets global variable `$?` (child exit
|
728
|
+
# status).
|
729
|
+
#
|
730
|
+
# It is not an error to close an IO object that has already been closed. It just
|
731
|
+
# returns nil.
|
732
|
+
#
|
733
|
+
# Example:
|
734
|
+
#
|
735
|
+
# IO.popen('ruby', 'r+') do |pipe|
|
736
|
+
# puts pipe.closed?
|
737
|
+
# pipe.close
|
738
|
+
# puts $?
|
739
|
+
# puts pipe.closed?
|
740
|
+
# end
|
741
|
+
#
|
742
|
+
# Output:
|
743
|
+
#
|
744
|
+
# false
|
745
|
+
# pid 13760 exit 0
|
746
|
+
# true
|
747
|
+
#
|
748
|
+
# Related: IO#close_read, IO#close_write, IO#closed?.
|
749
|
+
#
|
750
|
+
def close: () -> nil
|
751
|
+
|
752
|
+
# <!--
|
753
|
+
# rdoc-file=io.c
|
754
|
+
# - self.close_on_exec = bool -> true or false
|
755
|
+
# -->
|
756
|
+
# Sets a close-on-exec flag.
|
757
|
+
#
|
758
|
+
# f = File.open(File::NULL)
|
759
|
+
# f.close_on_exec = true
|
760
|
+
# system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
|
761
|
+
# f.closed? #=> false
|
762
|
+
#
|
763
|
+
# Ruby sets close-on-exec flags of all file descriptors by default since Ruby
|
764
|
+
# 2.0.0. So you don't need to set by yourself. Also, unsetting a close-on-exec
|
765
|
+
# flag can cause file descriptor leak if another thread use fork() and exec()
|
766
|
+
# (via system() method for example). If you really needs file descriptor
|
767
|
+
# inheritance to child process, use spawn()'s argument such as fd=>fd.
|
768
|
+
#
|
769
|
+
def close_on_exec=: (boolish bool) -> nil
|
770
|
+
|
771
|
+
# <!--
|
772
|
+
# rdoc-file=io.c
|
773
|
+
# - close_on_exec? -> true or false
|
774
|
+
# -->
|
775
|
+
# Returns `true` if the stream will be closed on exec, `false` otherwise:
|
776
|
+
#
|
777
|
+
# f = File.open('t.txt')
|
778
|
+
# f.close_on_exec? # => true
|
779
|
+
# f.close_on_exec = false
|
780
|
+
# f.close_on_exec? # => false
|
781
|
+
# f.close
|
782
|
+
#
|
783
|
+
def close_on_exec?: () -> bool
|
784
|
+
|
785
|
+
# <!--
|
786
|
+
# rdoc-file=io.c
|
787
|
+
# - close_read -> nil
|
788
|
+
# -->
|
789
|
+
# Closes the stream for reading if open for reading; returns `nil`. See [Open
|
790
|
+
# and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
|
791
|
+
#
|
792
|
+
# If the stream was opened by IO.popen and is also closed for writing, sets
|
793
|
+
# global variable `$?` (child exit status).
|
794
|
+
#
|
795
|
+
# Example:
|
796
|
+
#
|
797
|
+
# IO.popen('ruby', 'r+') do |pipe|
|
798
|
+
# puts pipe.closed?
|
799
|
+
# pipe.close_write
|
800
|
+
# puts pipe.closed?
|
801
|
+
# pipe.close_read
|
802
|
+
# puts $?
|
803
|
+
# puts pipe.closed?
|
804
|
+
# end
|
805
|
+
#
|
806
|
+
# Output:
|
807
|
+
#
|
808
|
+
# false
|
809
|
+
# false
|
810
|
+
# pid 14748 exit 0
|
811
|
+
# true
|
812
|
+
#
|
813
|
+
# Related: IO#close, IO#close_write, IO#closed?.
|
814
|
+
#
|
815
|
+
def close_read: () -> nil
|
816
|
+
|
817
|
+
# <!--
|
818
|
+
# rdoc-file=io.c
|
819
|
+
# - close_write -> nil
|
820
|
+
# -->
|
821
|
+
# Closes the stream for writing if open for writing; returns `nil`. See [Open
|
822
|
+
# and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
|
823
|
+
#
|
824
|
+
# Flushes any buffered writes to the operating system before closing.
|
825
|
+
#
|
826
|
+
# If the stream was opened by IO.popen and is also closed for reading, sets
|
827
|
+
# global variable `$?` (child exit status).
|
828
|
+
#
|
829
|
+
# IO.popen('ruby', 'r+') do |pipe|
|
830
|
+
# puts pipe.closed?
|
831
|
+
# pipe.close_read
|
832
|
+
# puts pipe.closed?
|
833
|
+
# pipe.close_write
|
834
|
+
# puts $?
|
835
|
+
# puts pipe.closed?
|
836
|
+
# end
|
837
|
+
#
|
838
|
+
# Output:
|
839
|
+
#
|
840
|
+
# false
|
841
|
+
# false
|
842
|
+
# pid 15044 exit 0
|
843
|
+
# true
|
844
|
+
#
|
845
|
+
# Related: IO#close, IO#close_read, IO#closed?.
|
846
|
+
#
|
847
|
+
def close_write: () -> nil
|
848
|
+
|
849
|
+
# <!--
|
850
|
+
# rdoc-file=io.c
|
851
|
+
# - closed? -> true or false
|
852
|
+
# -->
|
853
|
+
# Returns `true` if the stream is closed for both reading and writing, `false`
|
854
|
+
# otherwise. See [Open and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
|
855
|
+
#
|
856
|
+
# IO.popen('ruby', 'r+') do |pipe|
|
857
|
+
# puts pipe.closed?
|
858
|
+
# pipe.close_read
|
859
|
+
# puts pipe.closed?
|
860
|
+
# pipe.close_write
|
861
|
+
# puts pipe.closed?
|
862
|
+
# end
|
863
|
+
#
|
864
|
+
# Output:
|
865
|
+
#
|
866
|
+
# false
|
867
|
+
# false
|
868
|
+
# true
|
869
|
+
#
|
870
|
+
# Related: IO#close_read, IO#close_write, IO#close.
|
871
|
+
#
|
872
|
+
def closed?: () -> bool
|
873
|
+
|
874
|
+
# <!--
|
875
|
+
# rdoc-file=io.c
|
876
|
+
# - each_byte {|byte| ... } -> self
|
877
|
+
# - each_byte -> enumerator
|
878
|
+
# -->
|
879
|
+
# Calls the given block with each byte (0..255) in the stream; returns `self`.
|
880
|
+
# See [Byte IO](rdoc-ref:IO@Byte+IO).
|
881
|
+
#
|
882
|
+
# f = File.new('t.rus')
|
883
|
+
# a = []
|
884
|
+
# f.each_byte {|b| a << b }
|
885
|
+
# a # => [209, 130, 208, 181, 209, 129, 209, 130]
|
886
|
+
# f.close
|
887
|
+
#
|
888
|
+
# Returns an Enumerator if no block is given.
|
889
|
+
#
|
890
|
+
# Related: IO#each_char, IO#each_codepoint.
|
891
|
+
#
|
892
|
+
def each_byte: () { (Integer byte) -> void } -> self
|
893
|
+
| () -> ::Enumerator[Integer, self]
|
894
|
+
|
895
|
+
# <!--
|
896
|
+
# rdoc-file=io.c
|
897
|
+
# - each_char {|c| ... } -> self
|
898
|
+
# - each_char -> enumerator
|
899
|
+
# -->
|
900
|
+
# Calls the given block with each character in the stream; returns `self`. See
|
901
|
+
# [Character IO](rdoc-ref:IO@Character+IO).
|
902
|
+
#
|
903
|
+
# f = File.new('t.rus')
|
904
|
+
# a = []
|
905
|
+
# f.each_char {|c| a << c.ord }
|
906
|
+
# a # => [1090, 1077, 1089, 1090]
|
907
|
+
# f.close
|
908
|
+
#
|
909
|
+
# Returns an Enumerator if no block is given.
|
910
|
+
#
|
911
|
+
# Related: IO#each_byte, IO#each_codepoint.
|
912
|
+
#
|
913
|
+
def each_char: () { (String c) -> void } -> self
|
914
|
+
| () -> ::Enumerator[String, self]
|
915
|
+
|
916
|
+
# <!--
|
917
|
+
# rdoc-file=io.c
|
918
|
+
# - each_codepoint {|c| ... } -> self
|
919
|
+
# - each_codepoint -> enumerator
|
920
|
+
# -->
|
921
|
+
# Calls the given block with each codepoint in the stream; returns `self`:
|
922
|
+
#
|
923
|
+
# f = File.new('t.rus')
|
924
|
+
# a = []
|
925
|
+
# f.each_codepoint {|c| a << c }
|
926
|
+
# a # => [1090, 1077, 1089, 1090]
|
927
|
+
# f.close
|
928
|
+
#
|
929
|
+
# Returns an Enumerator if no block is given.
|
930
|
+
#
|
931
|
+
# Related: IO#each_byte, IO#each_char.
|
932
|
+
#
|
933
|
+
def each_codepoint: () { (Integer c) -> void } -> self
|
934
|
+
| () -> ::Enumerator[Integer, self]
|
935
|
+
|
936
|
+
# <!--
|
937
|
+
# rdoc-file=io.c
|
938
|
+
# - eof -> true or false
|
939
|
+
# -->
|
940
|
+
# Returns `true` if the stream is positioned at its end, `false` otherwise; see
|
941
|
+
# [Position](rdoc-ref:IO@Position):
|
942
|
+
#
|
943
|
+
# f = File.open('t.txt')
|
944
|
+
# f.eof # => false
|
945
|
+
# f.seek(0, :END) # => 0
|
946
|
+
# f.eof # => true
|
947
|
+
# f.close
|
948
|
+
#
|
949
|
+
# Raises an exception unless the stream is opened for reading; see
|
950
|
+
# [Mode](rdoc-ref:File@Access+Modes).
|
951
|
+
#
|
952
|
+
# If `self` is a stream such as pipe or socket, this method blocks until the
|
953
|
+
# other end sends some data or closes it:
|
954
|
+
#
|
955
|
+
# r, w = IO.pipe
|
956
|
+
# Thread.new { sleep 1; w.close }
|
957
|
+
# r.eof? # => true # After 1-second wait.
|
958
|
+
#
|
959
|
+
# r, w = IO.pipe
|
960
|
+
# Thread.new { sleep 1; w.puts "a" }
|
961
|
+
# r.eof? # => false # After 1-second wait.
|
962
|
+
#
|
963
|
+
# r, w = IO.pipe
|
964
|
+
# r.eof? # blocks forever
|
965
|
+
#
|
966
|
+
# Note that this method reads data to the input byte buffer. So IO#sysread may
|
967
|
+
# not behave as you intend with IO#eof?, unless you call IO#rewind first (which
|
968
|
+
# is not available for some streams).
|
969
|
+
#
|
970
|
+
def eof: () -> bool
|
971
|
+
|
972
|
+
# <!--
|
973
|
+
# rdoc-file=io.c
|
974
|
+
# - fcntl(integer_cmd, argument) -> integer
|
975
|
+
# -->
|
976
|
+
# Invokes Posix system call [fcntl(2)](https://linux.die.net/man/2/fcntl), which
|
977
|
+
# provides a mechanism for issuing low-level commands to control or query a
|
978
|
+
# file-oriented I/O stream. Arguments and results are platform dependent.
|
979
|
+
#
|
980
|
+
# If `argument` is a number, its value is passed directly; if it is a string, it
|
981
|
+
# is interpreted as a binary sequence of bytes. (Array#pack might be a useful
|
982
|
+
# way to build this string.)
|
983
|
+
#
|
984
|
+
# Not implemented on all platforms.
|
985
|
+
#
|
986
|
+
def fcntl: (Integer integer_cmd, String | Integer argument) -> Integer
|
987
|
+
|
988
|
+
# <!--
|
989
|
+
# rdoc-file=io.c
|
990
|
+
# - fdatasync -> 0
|
991
|
+
# -->
|
992
|
+
# Immediately writes to disk all data buffered in the stream, via the operating
|
993
|
+
# system's: `fdatasync(2)`, if supported, otherwise via `fsync(2)`, if
|
994
|
+
# supported; otherwise raises an exception.
|
995
|
+
#
|
996
|
+
def fdatasync: () -> Integer?
|
997
|
+
|
998
|
+
# <!--
|
999
|
+
# rdoc-file=io.c
|
1000
|
+
# - fileno -> integer
|
1001
|
+
# -->
|
1002
|
+
# Returns the integer file descriptor for the stream:
|
1003
|
+
#
|
1004
|
+
# $stdin.fileno # => 0
|
1005
|
+
# $stdout.fileno # => 1
|
1006
|
+
# $stderr.fileno # => 2
|
1007
|
+
# File.open('t.txt').fileno # => 10
|
1008
|
+
# f.close
|
1009
|
+
#
|
1010
|
+
def fileno: () -> Integer
|
1011
|
+
|
1012
|
+
# <!--
|
1013
|
+
# rdoc-file=io.c
|
1014
|
+
# - flush -> self
|
1015
|
+
# -->
|
1016
|
+
# Flushes data buffered in `self` to the operating system (but does not
|
1017
|
+
# necessarily flush data buffered in the operating system):
|
1018
|
+
#
|
1019
|
+
# $stdout.print 'no newline' # Not necessarily flushed.
|
1020
|
+
# $stdout.flush # Flushed.
|
1021
|
+
#
|
1022
|
+
def flush: () -> self
|
1023
|
+
|
1024
|
+
# <!--
|
1025
|
+
# rdoc-file=io.c
|
1026
|
+
# - fsync -> 0
|
1027
|
+
# -->
|
1028
|
+
# Immediately writes to disk all data buffered in the stream, via the operating
|
1029
|
+
# system's `fsync(2)`.
|
1030
|
+
#
|
1031
|
+
# Note this difference:
|
1032
|
+
#
|
1033
|
+
# * IO#sync=: Ensures that data is flushed from the stream's internal buffers,
|
1034
|
+
# but does not guarantee that the operating system actually writes the data
|
1035
|
+
# to disk.
|
1036
|
+
# * IO#fsync: Ensures both that data is flushed from internal buffers, and
|
1037
|
+
# that data is written to disk.
|
1038
|
+
#
|
1039
|
+
# Raises an exception if the operating system does not support `fsync(2)`.
|
1040
|
+
#
|
1041
|
+
def fsync: () -> Integer?
|
1042
|
+
|
1043
|
+
# <!--
|
1044
|
+
# rdoc-file=io.c
|
1045
|
+
# - getbyte -> integer or nil
|
1046
|
+
# -->
|
1047
|
+
# Reads and returns the next byte (in range 0..255) from the stream; returns
|
1048
|
+
# `nil` if already at end-of-stream. See [Byte IO](rdoc-ref:IO@Byte+IO).
|
1049
|
+
#
|
1050
|
+
# f = File.open('t.txt')
|
1051
|
+
# f.getbyte # => 70
|
1052
|
+
# f.close
|
1053
|
+
# f = File.open('t.rus')
|
1054
|
+
# f.getbyte # => 209
|
1055
|
+
# f.close
|
1056
|
+
#
|
1057
|
+
# Related: IO#readbyte (may raise EOFError).
|
1058
|
+
#
|
1059
|
+
def getbyte: () -> Integer?
|
1060
|
+
|
1061
|
+
# <!--
|
1062
|
+
# rdoc-file=io.c
|
1063
|
+
# - getc -> character or nil
|
1064
|
+
# -->
|
1065
|
+
# Reads and returns the next 1-character string from the stream; returns `nil`
|
1066
|
+
# if already at end-of-stream. See [Character IO](rdoc-ref:IO@Character+IO).
|
1067
|
+
#
|
1068
|
+
# f = File.open('t.txt')
|
1069
|
+
# f.getc # => "F"
|
1070
|
+
# f.close
|
1071
|
+
# f = File.open('t.rus')
|
1072
|
+
# f.getc.ord # => 1090
|
1073
|
+
# f.close
|
1074
|
+
#
|
1075
|
+
# Related: IO#readchar (may raise EOFError).
|
1076
|
+
#
|
1077
|
+
def getc: () -> String?
|
1078
|
+
|
1079
|
+
# <!--
|
1080
|
+
# rdoc-file=io.c
|
1081
|
+
# - gets(sep = $/, chomp: false) -> string or nil
|
1082
|
+
# - gets(limit, chomp: false) -> string or nil
|
1083
|
+
# - gets(sep, limit, chomp: false) -> string or nil
|
1084
|
+
# -->
|
1085
|
+
# Reads and returns a line from the stream; assigns the return value to `$_`.
|
1086
|
+
# See [Line IO](rdoc-ref:IO@Line+IO).
|
1087
|
+
#
|
1088
|
+
# With no arguments given, returns the next line as determined by line separator
|
1089
|
+
# `$/`, or `nil` if none:
|
1090
|
+
#
|
1091
|
+
# f = File.open('t.txt')
|
1092
|
+
# f.gets # => "First line\n"
|
1093
|
+
# $_ # => "First line\n"
|
1094
|
+
# f.gets # => "\n"
|
1095
|
+
# f.gets # => "Fourth line\n"
|
1096
|
+
# f.gets # => "Fifth line\n"
|
1097
|
+
# f.gets # => nil
|
1098
|
+
# f.close
|
1099
|
+
#
|
1100
|
+
# With only string argument `sep` given, returns the next line as determined by
|
1101
|
+
# line separator `sep`, or `nil` if none; see [Line
|
1102
|
+
# Separator](rdoc-ref:IO@Line+Separator):
|
1103
|
+
#
|
1104
|
+
# f = File.new('t.txt')
|
1105
|
+
# f.gets('l') # => "First l"
|
1106
|
+
# f.gets('li') # => "ine\nSecond li"
|
1107
|
+
# f.gets('lin') # => "ne\n\nFourth lin"
|
1108
|
+
# f.gets # => "e\n"
|
1109
|
+
# f.close
|
1110
|
+
#
|
1111
|
+
# The two special values for `sep` are honored:
|
1112
|
+
#
|
1113
|
+
# f = File.new('t.txt')
|
1114
|
+
# # Get all.
|
1115
|
+
# f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
1116
|
+
# f.rewind
|
1117
|
+
# # Get paragraph (up to two line separators).
|
1118
|
+
# f.gets('') # => "First line\nSecond line\n\n"
|
1119
|
+
# f.close
|
1120
|
+
#
|
1121
|
+
# With only integer argument `limit` given, limits the number of bytes in the
|
1122
|
+
# line; see [Line Limit](rdoc-ref:IO@Line+Limit):
|
1123
|
+
#
|
1124
|
+
# # No more than one line.
|
1125
|
+
# File.open('t.txt') {|f| f.gets(10) } # => "First line"
|
1126
|
+
# File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
|
1127
|
+
# File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
|
1128
|
+
#
|
1129
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
1130
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
1131
|
+
#
|
1132
|
+
# Optional keyword argument `chomp` specifies whether line separators are to be
|
1133
|
+
# omitted:
|
1134
|
+
#
|
1135
|
+
# f = File.open('t.txt')
|
1136
|
+
# # Chomp the lines.
|
1137
|
+
# f.gets(chomp: true) # => "First line"
|
1138
|
+
# f.gets(chomp: true) # => "Second line"
|
1139
|
+
# f.gets(chomp: true) # => ""
|
1140
|
+
# f.gets(chomp: true) # => "Fourth line"
|
1141
|
+
# f.gets(chomp: true) # => "Fifth line"
|
1142
|
+
# f.gets(chomp: true) # => nil
|
1143
|
+
# f.close
|
1144
|
+
#
|
1145
|
+
def gets: (string? sep, ?int limit, ?chomp: boolish) -> String?
|
1146
|
+
| (?int limit, ?chomp: boolish) -> String?
|
1147
|
+
|
1148
|
+
# <!--
|
1149
|
+
# rdoc-file=io.c
|
1150
|
+
# - IO.new(fd, mode = 'r', **opts) -> io
|
1151
|
+
# -->
|
1152
|
+
# Creates and returns a new IO object (file stream) from a file descriptor.
|
1153
|
+
#
|
1154
|
+
# IO.new may be useful for interaction with low-level libraries. For
|
1155
|
+
# higher-level interactions, it may be simpler to create the file stream using
|
1156
|
+
# File.open.
|
1157
|
+
#
|
1158
|
+
# Argument `fd` must be a valid file descriptor (integer):
|
1159
|
+
#
|
1160
|
+
# path = 't.tmp'
|
1161
|
+
# fd = IO.sysopen(path) # => 3
|
1162
|
+
# IO.new(fd) # => #<IO:fd 3>
|
1163
|
+
#
|
1164
|
+
# The new IO object does not inherit encoding (because the integer file
|
1165
|
+
# descriptor does not have an encoding):
|
1166
|
+
#
|
1167
|
+
# fd = IO.sysopen('t.rus', 'rb')
|
1168
|
+
# io = IO.new(fd)
|
1169
|
+
# io.external_encoding # => #<Encoding:UTF-8> # Not ASCII-8BIT.
|
1170
|
+
#
|
1171
|
+
# Optional argument `mode` (defaults to 'r') must specify a valid mode; see
|
1172
|
+
# [Access Modes](rdoc-ref:File@Access+Modes):
|
1173
|
+
#
|
1174
|
+
# IO.new(fd, 'w') # => #<IO:fd 3>
|
1175
|
+
# IO.new(fd, File::WRONLY) # => #<IO:fd 3>
|
1176
|
+
#
|
1177
|
+
# Optional keyword arguments `opts` specify:
|
1178
|
+
#
|
1179
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
1180
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
1181
|
+
#
|
1182
|
+
# Examples:
|
1183
|
+
#
|
1184
|
+
# IO.new(fd, internal_encoding: nil) # => #<IO:fd 3>
|
1185
|
+
# IO.new(fd, autoclose: true) # => #<IO:fd 3>
|
1186
|
+
#
|
1187
|
+
def initialize: ( int fd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> void
|
1188
|
+
|
1189
|
+
# <!--
|
1190
|
+
# rdoc-file=io.c
|
1191
|
+
# - inspect -> string
|
1192
|
+
# -->
|
1193
|
+
# Returns a string representation of `self`:
|
1194
|
+
#
|
1195
|
+
# f = File.open('t.txt')
|
1196
|
+
# f.inspect # => "#<File:t.txt>"
|
1197
|
+
# f.close
|
1198
|
+
#
|
1199
|
+
def inspect: () -> String
|
1200
|
+
|
1201
|
+
# <!--
|
1202
|
+
# rdoc-file=io.c
|
1203
|
+
# - internal_encoding -> encoding or nil
|
1204
|
+
# -->
|
1205
|
+
# Returns the Encoding object that represents the encoding of the internal
|
1206
|
+
# string, if conversion is specified, or `nil` otherwise.
|
1207
|
+
#
|
1208
|
+
# See [Encodings](rdoc-ref:File@Encodings).
|
1209
|
+
#
|
1210
|
+
def internal_encoding: () -> Encoding
|
1211
|
+
|
1212
|
+
# <!--
|
1213
|
+
# rdoc-file=io.c
|
1214
|
+
# - ioctl(integer_cmd, argument) -> integer
|
1215
|
+
# -->
|
1216
|
+
# Invokes Posix system call [ioctl(2)](https://linux.die.net/man/2/ioctl), which
|
1217
|
+
# issues a low-level command to an I/O device.
|
1218
|
+
#
|
1219
|
+
# Issues a low-level command to an I/O device. The arguments and returned value
|
1220
|
+
# are platform-dependent. The effect of the call is platform-dependent.
|
1221
|
+
#
|
1222
|
+
# If argument `argument` is an integer, it is passed directly; if it is a
|
1223
|
+
# string, it is interpreted as a binary sequence of bytes.
|
1224
|
+
#
|
1225
|
+
# Not implemented on all platforms.
|
1226
|
+
#
|
1227
|
+
def ioctl: (Integer integer_cmd, String | Integer argument) -> Integer
|
1228
|
+
|
1229
|
+
# <!--
|
1230
|
+
# rdoc-file=io.c
|
1231
|
+
# - isatty -> true or false
|
1232
|
+
# -->
|
1233
|
+
# Returns `true` if the stream is associated with a terminal device (tty),
|
1234
|
+
# `false` otherwise:
|
1235
|
+
#
|
1236
|
+
# f = File.new('t.txt').isatty #=> false
|
1237
|
+
# f.close
|
1238
|
+
# f = File.new('/dev/tty').isatty #=> true
|
1239
|
+
# f.close
|
1240
|
+
#
|
1241
|
+
def isatty: () -> bool
|
1242
|
+
|
1243
|
+
# <!--
|
1244
|
+
# rdoc-file=io.c
|
1245
|
+
# - lineno -> integer
|
1246
|
+
# -->
|
1247
|
+
# Returns the current line number for the stream; see [Line
|
1248
|
+
# Number](rdoc-ref:IO@Line+Number).
|
1249
|
+
#
|
1250
|
+
def lineno: () -> Integer
|
1251
|
+
|
1252
|
+
# <!--
|
1253
|
+
# rdoc-file=io.c
|
1254
|
+
# - lineno = integer -> integer
|
1255
|
+
# -->
|
1256
|
+
# Sets and returns the line number for the stream; see [Line
|
1257
|
+
# Number](rdoc-ref:IO@Line+Number).
|
1258
|
+
#
|
1259
|
+
def lineno=: (Integer integer) -> Integer
|
1260
|
+
|
1261
|
+
# <!--
|
1262
|
+
# rdoc-file=io.c
|
1263
|
+
# - path -> string or nil
|
1264
|
+
# -->
|
1265
|
+
# Returns the path associated with the IO, or `nil` if there is no path
|
1266
|
+
# associated with the IO. It is not guaranteed that the path exists on the
|
1267
|
+
# filesystem.
|
1268
|
+
#
|
1269
|
+
# $stdin.path # => "<STDIN>"
|
1270
|
+
#
|
1271
|
+
# File.open("testfile") {|f| f.path} # => "testfile"
|
1272
|
+
#
|
1273
|
+
def path: () -> String?
|
1274
|
+
|
1275
|
+
# <!--
|
1276
|
+
# rdoc-file=io.c
|
1277
|
+
# - pid -> integer or nil
|
1278
|
+
# -->
|
1279
|
+
# Returns the process ID of a child process associated with the stream, which
|
1280
|
+
# will have been set by IO#popen, or `nil` if the stream was not created by
|
1281
|
+
# IO#popen:
|
1282
|
+
#
|
1283
|
+
# pipe = IO.popen("-")
|
1284
|
+
# if pipe
|
1285
|
+
# $stderr.puts "In parent, child pid is #{pipe.pid}"
|
1286
|
+
# else
|
1287
|
+
# $stderr.puts "In child, pid is #{$$}"
|
1288
|
+
# end
|
1289
|
+
#
|
1290
|
+
# Output:
|
1291
|
+
#
|
1292
|
+
# In child, pid is 26209
|
1293
|
+
# In parent, child pid is 26209
|
1294
|
+
#
|
1295
|
+
def pid: () -> Integer
|
1296
|
+
|
1297
|
+
# <!--
|
1298
|
+
# rdoc-file=io.c
|
1299
|
+
# - pos = new_position -> new_position
|
1300
|
+
# -->
|
1301
|
+
# Seeks to the given `new_position` (in bytes); see
|
1302
|
+
# [Position](rdoc-ref:IO@Position):
|
1303
|
+
#
|
1304
|
+
# f = File.open('t.txt')
|
1305
|
+
# f.tell # => 0
|
1306
|
+
# f.pos = 20 # => 20
|
1307
|
+
# f.tell # => 20
|
1308
|
+
# f.close
|
1309
|
+
#
|
1310
|
+
# Related: IO#seek, IO#tell.
|
1311
|
+
#
|
1312
|
+
def pos=: (Integer new_position) -> Integer
|
1313
|
+
|
1314
|
+
# <!--
|
1315
|
+
# rdoc-file=io.c
|
1316
|
+
# - print(*objects) -> nil
|
1317
|
+
# -->
|
1318
|
+
# Writes the given objects to the stream; returns `nil`. Appends the output
|
1319
|
+
# record separator `$OUTPUT_RECORD_SEPARATOR` (`$\`), if it is not `nil`. See
|
1320
|
+
# [Line IO](rdoc-ref:IO@Line+IO).
|
1321
|
+
#
|
1322
|
+
# With argument `objects` given, for each object:
|
1323
|
+
#
|
1324
|
+
# * Converts via its method `to_s` if not a string.
|
1325
|
+
# * Writes to the stream.
|
1326
|
+
# * If not the last object, writes the output field separator
|
1327
|
+
# `$OUTPUT_FIELD_SEPARATOR` (`$,`) if it is not `nil`.
|
1328
|
+
#
|
1329
|
+
# With default separators:
|
1330
|
+
#
|
1331
|
+
# f = File.open('t.tmp', 'w+')
|
1332
|
+
# objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero']
|
1333
|
+
# p $OUTPUT_RECORD_SEPARATOR
|
1334
|
+
# p $OUTPUT_FIELD_SEPARATOR
|
1335
|
+
# f.print(*objects)
|
1336
|
+
# f.rewind
|
1337
|
+
# p f.read
|
1338
|
+
# f.close
|
1339
|
+
#
|
1340
|
+
# Output:
|
1341
|
+
#
|
1342
|
+
# nil
|
1343
|
+
# nil
|
1344
|
+
# "00.00/10+0izerozero"
|
1345
|
+
#
|
1346
|
+
# With specified separators:
|
1347
|
+
#
|
1348
|
+
# $\ = "\n"
|
1349
|
+
# $, = ','
|
1350
|
+
# f.rewind
|
1351
|
+
# f.print(*objects)
|
1352
|
+
# f.rewind
|
1353
|
+
# p f.read
|
1354
|
+
#
|
1355
|
+
# Output:
|
1356
|
+
#
|
1357
|
+
# "0,0.0,0/1,0+0i,zero,zero\n"
|
1358
|
+
#
|
1359
|
+
# With no argument given, writes the content of `$_` (which is usually the most
|
1360
|
+
# recent user input):
|
1361
|
+
#
|
1362
|
+
# f = File.open('t.tmp', 'w+')
|
1363
|
+
# gets # Sets $_ to the most recent user input.
|
1364
|
+
# f.print
|
1365
|
+
# f.close
|
1366
|
+
#
|
1367
|
+
def print: (*untyped objects) -> nil
|
1368
|
+
|
1369
|
+
# <!--
|
1370
|
+
# rdoc-file=io.c
|
1371
|
+
# - printf(format_string, *objects) -> nil
|
1372
|
+
# -->
|
1373
|
+
# Formats and writes `objects` to the stream.
|
1374
|
+
#
|
1375
|
+
# For details on `format_string`, see [Format
|
1376
|
+
# Specifications](rdoc-ref:format_specifications.rdoc).
|
1377
|
+
#
|
1378
|
+
def printf: (String format_string, *untyped objects) -> nil
|
1379
|
+
|
1380
|
+
# <!--
|
1381
|
+
# rdoc-file=io.c
|
1382
|
+
# - putc(object) -> object
|
1383
|
+
# -->
|
1384
|
+
# Writes a character to the stream. See [Character
|
1385
|
+
# IO](rdoc-ref:IO@Character+IO).
|
1386
|
+
#
|
1387
|
+
# If `object` is numeric, converts to integer if necessary, then writes the
|
1388
|
+
# character whose code is the least significant byte; if `object` is a string,
|
1389
|
+
# writes the first character:
|
1390
|
+
#
|
1391
|
+
# $stdout.putc "A"
|
1392
|
+
# $stdout.putc 65
|
1393
|
+
#
|
1394
|
+
# Output:
|
1395
|
+
#
|
1396
|
+
# AA
|
1397
|
+
#
|
1398
|
+
def putc: (Numeric | String object) -> (Numeric | String)
|
1399
|
+
|
1400
|
+
# <!--
|
1401
|
+
# rdoc-file=io.c
|
1402
|
+
# - puts(*objects) -> nil
|
1403
|
+
# -->
|
1404
|
+
# Writes the given `objects` to the stream, which must be open for writing;
|
1405
|
+
# returns `nil`.\ Writes a newline after each that does not already end with a
|
1406
|
+
# newline sequence. If called without arguments, writes a newline. See [Line
|
1407
|
+
# IO](rdoc-ref:IO@Line+IO).
|
1408
|
+
#
|
1409
|
+
# Note that each added newline is the character `"\n"<//tt>, not the output
|
1410
|
+
# record separator (<tt>$\`).
|
1411
|
+
#
|
1412
|
+
# Treatment for each object:
|
1413
|
+
#
|
1414
|
+
# * String: writes the string.
|
1415
|
+
# * Neither string nor array: writes `object.to_s`.
|
1416
|
+
# * Array: writes each element of the array; arrays may be nested.
|
1417
|
+
#
|
1418
|
+
# To keep these examples brief, we define this helper method:
|
1419
|
+
#
|
1420
|
+
# def show(*objects)
|
1421
|
+
# # Puts objects to file.
|
1422
|
+
# f = File.new('t.tmp', 'w+')
|
1423
|
+
# f.puts(objects)
|
1424
|
+
# # Return file content.
|
1425
|
+
# f.rewind
|
1426
|
+
# p f.read
|
1427
|
+
# f.close
|
1428
|
+
# end
|
1429
|
+
#
|
1430
|
+
# # Strings without newlines.
|
1431
|
+
# show('foo', 'bar', 'baz') # => "foo\nbar\nbaz\n"
|
1432
|
+
# # Strings, some with newlines.
|
1433
|
+
# show("foo\n", 'bar', "baz\n") # => "foo\nbar\nbaz\n"
|
1434
|
+
#
|
1435
|
+
# # Neither strings nor arrays:
|
1436
|
+
# show(0, 0.0, Rational(0, 1), Complex(9, 0), :zero)
|
1437
|
+
# # => "0\n0.0\n0/1\n9+0i\nzero\n"
|
1438
|
+
#
|
1439
|
+
# # Array of strings.
|
1440
|
+
# show(['foo', "bar\n", 'baz']) # => "foo\nbar\nbaz\n"
|
1441
|
+
# # Nested arrays.
|
1442
|
+
# show([[[0, 1], 2, 3], 4, 5]) # => "0\n1\n2\n3\n4\n5\n"
|
1443
|
+
#
|
1444
|
+
def puts: (*untyped objects) -> nil
|
1445
|
+
|
1446
|
+
# <!--
|
1447
|
+
# rdoc-file=io.c
|
1448
|
+
# - read(maxlen = nil, out_string = nil) -> new_string, out_string, or nil
|
1449
|
+
# -->
|
1450
|
+
# Reads bytes from the stream; the stream must be opened for reading (see
|
1451
|
+
# [Access Modes](rdoc-ref:File@Access+Modes)):
|
1452
|
+
#
|
1453
|
+
# * If `maxlen` is `nil`, reads all bytes using the stream's data mode.
|
1454
|
+
# * Otherwise reads up to `maxlen` bytes in binary mode.
|
1455
|
+
#
|
1456
|
+
# Returns a string (either a new string or the given `out_string`) containing
|
1457
|
+
# the bytes read. The encoding of the string depends on both `maxLen` and
|
1458
|
+
# `out_string`:
|
1459
|
+
#
|
1460
|
+
# * `maxlen` is `nil`: uses internal encoding of `self` (regardless of whether
|
1461
|
+
# `out_string` was given).
|
1462
|
+
# * `maxlen` not `nil`:
|
1463
|
+
#
|
1464
|
+
# * `out_string` given: encoding of `out_string` not modified.
|
1465
|
+
# * `out_string` not given: ASCII-8BIT is used.
|
1466
|
+
#
|
1467
|
+
# **Without Argument `out_string`**
|
1468
|
+
#
|
1469
|
+
# When argument `out_string` is omitted, the returned value is a new string:
|
1470
|
+
#
|
1471
|
+
# f = File.new('t.txt')
|
1472
|
+
# f.read
|
1473
|
+
# # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
1474
|
+
# f.rewind
|
1475
|
+
# f.read(30) # => "First line\r\nSecond line\r\n\r\nFou"
|
1476
|
+
# f.read(30) # => "rth line\r\nFifth line\r\n"
|
1477
|
+
# f.read(30) # => nil
|
1478
|
+
# f.close
|
1479
|
+
#
|
1480
|
+
# If `maxlen` is zero, returns an empty string.
|
1481
|
+
#
|
1482
|
+
# ** With Argument `out_string`**
|
1483
|
+
#
|
1484
|
+
# When argument `out_string` is given, the returned value is `out_string`, whose
|
1485
|
+
# content is replaced:
|
1486
|
+
#
|
1487
|
+
# f = File.new('t.txt')
|
1488
|
+
# s = 'foo' # => "foo"
|
1489
|
+
# f.read(nil, s) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
1490
|
+
# s # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
1491
|
+
# f.rewind
|
1492
|
+
# s = 'bar'
|
1493
|
+
# f.read(30, s) # => "First line\r\nSecond line\r\n\r\nFou"
|
1494
|
+
# s # => "First line\r\nSecond line\r\n\r\nFou"
|
1495
|
+
# s = 'baz'
|
1496
|
+
# f.read(30, s) # => "rth line\r\nFifth line\r\n"
|
1497
|
+
# s # => "rth line\r\nFifth line\r\n"
|
1498
|
+
# s = 'bat'
|
1499
|
+
# f.read(30, s) # => nil
|
1500
|
+
# s # => ""
|
1501
|
+
# f.close
|
1502
|
+
#
|
1503
|
+
# Note that this method behaves like the fread() function in C. This means it
|
1504
|
+
# retries to invoke read(2) system calls to read data with the specified maxlen
|
1505
|
+
# (or until EOF).
|
1506
|
+
#
|
1507
|
+
# This behavior is preserved even if the stream is in non-blocking mode. (This
|
1508
|
+
# method is non-blocking-flag insensitive as other methods.)
|
1509
|
+
#
|
1510
|
+
# If you need the behavior like a single read(2) system call, consider
|
1511
|
+
# #readpartial, #read_nonblock, and #sysread.
|
1512
|
+
#
|
1513
|
+
# Related: IO#write.
|
1514
|
+
#
|
1515
|
+
def read: (?nil, ?string outbuf) -> String
|
1516
|
+
| (int? length, ?string outbuf) -> String?
|
1517
|
+
|
1518
|
+
# <!--
|
1519
|
+
# rdoc-file=io.rb
|
1520
|
+
# - ios.read_nonblock(maxlen [, options]) -> string
|
1521
|
+
# - ios.read_nonblock(maxlen, outbuf [, options]) -> outbuf
|
1522
|
+
# -->
|
1523
|
+
# Reads at most *maxlen* bytes from *ios* using the read(2) system call after
|
1524
|
+
# O_NONBLOCK is set for the underlying file descriptor.
|
1525
|
+
#
|
1526
|
+
# If the optional *outbuf* argument is present, it must reference a String,
|
1527
|
+
# which will receive the data. The *outbuf* will contain only the received data
|
1528
|
+
# after the method call even if it is not empty at the beginning.
|
1529
|
+
#
|
1530
|
+
# read_nonblock just calls the read(2) system call. It causes all errors the
|
1531
|
+
# read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The caller
|
1532
|
+
# should care such errors.
|
1533
|
+
#
|
1534
|
+
# If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by
|
1535
|
+
# IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for
|
1536
|
+
# retrying read_nonblock.
|
1537
|
+
#
|
1538
|
+
# read_nonblock causes EOFError on EOF.
|
1539
|
+
#
|
1540
|
+
# On some platforms, such as Windows, non-blocking mode is not supported on IO
|
1541
|
+
# objects other than sockets. In such cases, Errno::EBADF will be raised.
|
1542
|
+
#
|
1543
|
+
# If the read byte buffer is not empty, read_nonblock reads from the buffer like
|
1544
|
+
# readpartial. In this case, the read(2) system call is not called.
|
1545
|
+
#
|
1546
|
+
# When read_nonblock raises an exception kind of IO::WaitReadable, read_nonblock
|
1547
|
+
# should not be called until io is readable for avoiding busy loop. This can be
|
1548
|
+
# done as follows.
|
1549
|
+
#
|
1550
|
+
# # emulates blocking read (readpartial).
|
1551
|
+
# begin
|
1552
|
+
# result = io.read_nonblock(maxlen)
|
1553
|
+
# rescue IO::WaitReadable
|
1554
|
+
# IO.select([io])
|
1555
|
+
# retry
|
1556
|
+
# end
|
1557
|
+
#
|
1558
|
+
# Although IO#read_nonblock doesn't raise IO::WaitWritable.
|
1559
|
+
# OpenSSL::Buffering#read_nonblock can raise IO::WaitWritable. If IO and SSL
|
1560
|
+
# should be used polymorphically, IO::WaitWritable should be rescued too. See
|
1561
|
+
# the document of OpenSSL::Buffering#read_nonblock for sample code.
|
1562
|
+
#
|
1563
|
+
# Note that this method is identical to readpartial except the non-blocking flag
|
1564
|
+
# is set.
|
1565
|
+
#
|
1566
|
+
# By specifying a keyword argument *exception* to `false`, you can indicate that
|
1567
|
+
# read_nonblock should not raise an IO::WaitReadable exception, but return the
|
1568
|
+
# symbol `:wait_readable` instead. At EOF, it will return nil instead of raising
|
1569
|
+
# EOFError.
|
1570
|
+
#
|
1571
|
+
def read_nonblock: (int len, ?string buf, ?exception: true) -> String
|
1572
|
+
| (int len, ?string buf, exception: false) -> (String | :wait_readable | nil)
|
1573
|
+
|
1574
|
+
# <!--
|
1575
|
+
# rdoc-file=io.c
|
1576
|
+
# - readbyte -> integer
|
1577
|
+
# -->
|
1578
|
+
# Reads and returns the next byte (in range 0..255) from the stream; raises
|
1579
|
+
# EOFError if already at end-of-stream. See [Byte IO](rdoc-ref:IO@Byte+IO).
|
1580
|
+
#
|
1581
|
+
# f = File.open('t.txt')
|
1582
|
+
# f.readbyte # => 70
|
1583
|
+
# f.close
|
1584
|
+
# f = File.open('t.rus')
|
1585
|
+
# f.readbyte # => 209
|
1586
|
+
# f.close
|
1587
|
+
#
|
1588
|
+
# Related: IO#getbyte (will not raise EOFError).
|
1589
|
+
#
|
1590
|
+
def readbyte: () -> Integer
|
1591
|
+
|
1592
|
+
# <!--
|
1593
|
+
# rdoc-file=io.c
|
1594
|
+
# - readchar -> string
|
1595
|
+
# -->
|
1596
|
+
# Reads and returns the next 1-character string from the stream; raises EOFError
|
1597
|
+
# if already at end-of-stream. See [Character IO](rdoc-ref:IO@Character+IO).
|
1598
|
+
#
|
1599
|
+
# f = File.open('t.txt')
|
1600
|
+
# f.readchar # => "F"
|
1601
|
+
# f.close
|
1602
|
+
# f = File.open('t.rus')
|
1603
|
+
# f.readchar.ord # => 1090
|
1604
|
+
# f.close
|
1605
|
+
#
|
1606
|
+
# Related: IO#getc (will not raise EOFError).
|
1607
|
+
#
|
1608
|
+
def readchar: () -> String
|
1609
|
+
|
1610
|
+
# <!--
|
1611
|
+
# rdoc-file=io.rb
|
1612
|
+
# - readline(sep = $/, chomp: false) -> string
|
1613
|
+
# - readline(limit, chomp: false) -> string
|
1614
|
+
# - readline(sep, limit, chomp: false) -> string
|
1615
|
+
# -->
|
1616
|
+
# Reads a line as with IO#gets, but raises EOFError if already at end-of-stream.
|
1617
|
+
#
|
1618
|
+
# Optional keyword argument `chomp` specifies whether line separators are to be
|
1619
|
+
# omitted.
|
1620
|
+
#
|
1621
|
+
def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String
|
1622
|
+
|
1623
|
+
# <!--
|
1624
|
+
# rdoc-file=io.c
|
1625
|
+
# - readlines(sep = $/, chomp: false) -> array
|
1626
|
+
# - readlines(limit, chomp: false) -> array
|
1627
|
+
# - readlines(sep, limit, chomp: false) -> array
|
1628
|
+
# -->
|
1629
|
+
# Reads and returns all remaining line from the stream; does not modify `$_`.
|
1630
|
+
# See [Line IO](rdoc-ref:IO@Line+IO).
|
1631
|
+
#
|
1632
|
+
# With no arguments given, returns lines as determined by line separator `$/`,
|
1633
|
+
# or `nil` if none:
|
1634
|
+
#
|
1635
|
+
# f = File.new('t.txt')
|
1636
|
+
# f.readlines
|
1637
|
+
# # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
|
1638
|
+
# f.readlines # => []
|
1639
|
+
# f.close
|
1640
|
+
#
|
1641
|
+
# With only string argument `sep` given, returns lines as determined by line
|
1642
|
+
# separator `sep`, or `nil` if none; see [Line
|
1643
|
+
# Separator](rdoc-ref:IO@Line+Separator):
|
1644
|
+
#
|
1645
|
+
# f = File.new('t.txt')
|
1646
|
+
# f.readlines('li')
|
1647
|
+
# # => ["First li", "ne\nSecond li", "ne\n\nFourth li", "ne\nFifth li", "ne\n"]
|
1648
|
+
# f.close
|
1649
|
+
#
|
1650
|
+
# The two special values for `sep` are honored:
|
1651
|
+
#
|
1652
|
+
# f = File.new('t.txt')
|
1653
|
+
# # Get all into one string.
|
1654
|
+
# f.readlines(nil)
|
1655
|
+
# # => ["First line\nSecond line\n\nFourth line\nFifth line\n"]
|
1656
|
+
# # Get paragraphs (up to two line separators).
|
1657
|
+
# f.rewind
|
1658
|
+
# f.readlines('')
|
1659
|
+
# # => ["First line\nSecond line\n\n", "Fourth line\nFifth line\n"]
|
1660
|
+
# f.close
|
1661
|
+
#
|
1662
|
+
# With only integer argument `limit` given, limits the number of bytes in each
|
1663
|
+
# line; see [Line Limit](rdoc-ref:IO@Line+Limit):
|
1664
|
+
#
|
1665
|
+
# f = File.new('t.txt')
|
1666
|
+
# f.readlines(8)
|
1667
|
+
# # => ["First li", "ne\n", "Second l", "ine\n", "\n", "Fourth l", "ine\n", "Fifth li", "ne\n"]
|
1668
|
+
# f.close
|
1669
|
+
#
|
1670
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
1671
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
1672
|
+
#
|
1673
|
+
# Optional keyword argument `chomp` specifies whether line separators are to be
|
1674
|
+
# omitted:
|
1675
|
+
#
|
1676
|
+
# f = File.new('t.txt')
|
1677
|
+
# f.readlines(chomp: true)
|
1678
|
+
# # => ["First line", "Second line", "", "Fourth line", "Fifth line"]
|
1679
|
+
# f.close
|
1680
|
+
#
|
1681
|
+
def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]
|
1682
|
+
|
1683
|
+
# <!--
|
1684
|
+
# rdoc-file=io.c
|
1685
|
+
# - readpartial(maxlen) -> string
|
1686
|
+
# - readpartial(maxlen, out_string) -> out_string
|
1687
|
+
# -->
|
1688
|
+
# Reads up to `maxlen` bytes from the stream; returns a string (either a new
|
1689
|
+
# string or the given `out_string`). Its encoding is:
|
1690
|
+
#
|
1691
|
+
# * The unchanged encoding of `out_string`, if `out_string` is given.
|
1692
|
+
# * ASCII-8BIT, otherwise.
|
1693
|
+
#
|
1694
|
+
# * Contains `maxlen` bytes from the stream, if available.
|
1695
|
+
# * Otherwise contains all available bytes, if any available.
|
1696
|
+
# * Otherwise is an empty string.
|
1697
|
+
#
|
1698
|
+
# With the single non-negative integer argument `maxlen` given, returns a new
|
1699
|
+
# string:
|
1700
|
+
#
|
1701
|
+
# f = File.new('t.txt')
|
1702
|
+
# f.readpartial(20) # => "First line\nSecond l"
|
1703
|
+
# f.readpartial(20) # => "ine\n\nFourth line\n"
|
1704
|
+
# f.readpartial(20) # => "Fifth line\n"
|
1705
|
+
# f.readpartial(20) # Raises EOFError.
|
1706
|
+
# f.close
|
1707
|
+
#
|
1708
|
+
# With both argument `maxlen` and string argument `out_string` given, returns
|
1709
|
+
# modified `out_string`:
|
1710
|
+
#
|
1711
|
+
# f = File.new('t.txt')
|
1712
|
+
# s = 'foo'
|
1713
|
+
# f.readpartial(20, s) # => "First line\nSecond l"
|
1714
|
+
# s = 'bar'
|
1715
|
+
# f.readpartial(0, s) # => ""
|
1716
|
+
# f.close
|
1717
|
+
#
|
1718
|
+
# This method is useful for a stream such as a pipe, a socket, or a tty. It
|
1719
|
+
# blocks only when no data is immediately available. This means that it blocks
|
1720
|
+
# only when *all* of the following are true:
|
1721
|
+
#
|
1722
|
+
# * The byte buffer in the stream is empty.
|
1723
|
+
# * The content of the stream is empty.
|
1724
|
+
# * The stream is not at EOF.
|
1725
|
+
#
|
1726
|
+
# When blocked, the method waits for either more data or EOF on the stream:
|
1727
|
+
#
|
1728
|
+
# * If more data is read, the method returns the data.
|
1729
|
+
# * If EOF is reached, the method raises EOFError.
|
1730
|
+
#
|
1731
|
+
# When not blocked, the method responds immediately:
|
1732
|
+
#
|
1733
|
+
# * Returns data from the buffer if there is any.
|
1734
|
+
# * Otherwise returns data from the stream if there is any.
|
1735
|
+
# * Otherwise raises EOFError if the stream has reached EOF.
|
1736
|
+
#
|
1737
|
+
# Note that this method is similar to sysread. The differences are:
|
1738
|
+
#
|
1739
|
+
# * If the byte buffer is not empty, read from the byte buffer instead of
|
1740
|
+
# "sysread for buffered IO (IOError)".
|
1741
|
+
# * It doesn't cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial
|
1742
|
+
# meets EWOULDBLOCK and EINTR by read system call, readpartial retries the
|
1743
|
+
# system call.
|
1744
|
+
#
|
1745
|
+
# The latter means that readpartial is non-blocking-flag insensitive. It blocks
|
1746
|
+
# on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking
|
1747
|
+
# mode.
|
1748
|
+
#
|
1749
|
+
# Examples:
|
1750
|
+
#
|
1751
|
+
# # # Returned Buffer Content Pipe Content
|
1752
|
+
# r, w = IO.pipe #
|
1753
|
+
# w << 'abc' # "" "abc".
|
1754
|
+
# r.readpartial(4096) # => "abc" "" ""
|
1755
|
+
# r.readpartial(4096) # (Blocks because buffer and pipe are empty.)
|
1756
|
+
#
|
1757
|
+
# # # Returned Buffer Content Pipe Content
|
1758
|
+
# r, w = IO.pipe #
|
1759
|
+
# w << 'abc' # "" "abc"
|
1760
|
+
# w.close # "" "abc" EOF
|
1761
|
+
# r.readpartial(4096) # => "abc" "" EOF
|
1762
|
+
# r.readpartial(4096) # raises EOFError
|
1763
|
+
#
|
1764
|
+
# # # Returned Buffer Content Pipe Content
|
1765
|
+
# r, w = IO.pipe #
|
1766
|
+
# w << "abc\ndef\n" # "" "abc\ndef\n"
|
1767
|
+
# r.gets # => "abc\n" "def\n" ""
|
1768
|
+
# w << "ghi\n" # "def\n" "ghi\n"
|
1769
|
+
# r.readpartial(4096) # => "def\n" "" "ghi\n"
|
1770
|
+
# r.readpartial(4096) # => "ghi\n" "" ""
|
1771
|
+
#
|
1772
|
+
def readpartial: (int maxlen, ?string outbuf) -> String
|
1773
|
+
|
1774
|
+
# <!--
|
1775
|
+
# rdoc-file=io.c
|
1776
|
+
# - reopen(other_io) -> self
|
1777
|
+
# - reopen(path, mode = 'r', **opts) -> self
|
1778
|
+
# -->
|
1779
|
+
# Reassociates the stream with another stream, which may be of a different
|
1780
|
+
# class. This method may be used to redirect an existing stream to a new
|
1781
|
+
# destination.
|
1782
|
+
#
|
1783
|
+
# With argument `other_io` given, reassociates with that stream:
|
1784
|
+
#
|
1785
|
+
# # Redirect $stdin from a file.
|
1786
|
+
# f = File.open('t.txt')
|
1787
|
+
# $stdin.reopen(f)
|
1788
|
+
# f.close
|
1789
|
+
#
|
1790
|
+
# # Redirect $stdout to a file.
|
1791
|
+
# f = File.open('t.tmp', 'w')
|
1792
|
+
# $stdout.reopen(f)
|
1793
|
+
# f.close
|
1794
|
+
#
|
1795
|
+
# With argument `path` given, reassociates with a new stream to that file path:
|
1796
|
+
#
|
1797
|
+
# $stdin.reopen('t.txt')
|
1798
|
+
# $stdout.reopen('t.tmp', 'w')
|
1799
|
+
#
|
1800
|
+
# Optional keyword arguments `opts` specify:
|
1801
|
+
#
|
1802
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
1803
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
1804
|
+
#
|
1805
|
+
def reopen: (IO other_IO_or_path) -> IO
|
1806
|
+
| (String other_IO_or_path, ?String mode_str) -> IO
|
1807
|
+
|
1808
|
+
# <!--
|
1809
|
+
# rdoc-file=io.c
|
1810
|
+
# - rewind -> 0
|
1811
|
+
# -->
|
1812
|
+
# Repositions the stream to its beginning, setting both the position and the
|
1813
|
+
# line number to zero; see [Position](rdoc-ref:IO@Position) and [Line
|
1814
|
+
# Number](rdoc-ref:IO@Line+Number):
|
1815
|
+
#
|
1816
|
+
# f = File.open('t.txt')
|
1817
|
+
# f.tell # => 0
|
1818
|
+
# f.lineno # => 0
|
1819
|
+
# f.gets # => "First line\n"
|
1820
|
+
# f.tell # => 12
|
1821
|
+
# f.lineno # => 1
|
1822
|
+
# f.rewind # => 0
|
1823
|
+
# f.tell # => 0
|
1824
|
+
# f.lineno # => 0
|
1825
|
+
# f.close
|
1826
|
+
#
|
1827
|
+
# Note that this method cannot be used with streams such as pipes, ttys, and
|
1828
|
+
# sockets.
|
1829
|
+
#
|
1830
|
+
def rewind: () -> Integer
|
1831
|
+
|
1832
|
+
# <!--
|
1833
|
+
# rdoc-file=io.c
|
1834
|
+
# - seek(offset, whence = IO::SEEK_SET) -> 0
|
1835
|
+
# -->
|
1836
|
+
# Seeks to the position given by integer `offset` (see
|
1837
|
+
# [Position](rdoc-ref:IO@Position)) and constant `whence`, which is one of:
|
1838
|
+
#
|
1839
|
+
# * `:CUR` or `IO::SEEK_CUR`: Repositions the stream to its current position
|
1840
|
+
# plus the given `offset`:
|
1841
|
+
#
|
1842
|
+
# f = File.open('t.txt')
|
1843
|
+
# f.tell # => 0
|
1844
|
+
# f.seek(20, :CUR) # => 0
|
1845
|
+
# f.tell # => 20
|
1846
|
+
# f.seek(-10, :CUR) # => 0
|
1847
|
+
# f.tell # => 10
|
1848
|
+
# f.close
|
1849
|
+
#
|
1850
|
+
# * `:END` or `IO::SEEK_END`: Repositions the stream to its end plus the given
|
1851
|
+
# `offset`:
|
1852
|
+
#
|
1853
|
+
# f = File.open('t.txt')
|
1854
|
+
# f.tell # => 0
|
1855
|
+
# f.seek(0, :END) # => 0 # Repositions to stream end.
|
1856
|
+
# f.tell # => 52
|
1857
|
+
# f.seek(-20, :END) # => 0
|
1858
|
+
# f.tell # => 32
|
1859
|
+
# f.seek(-40, :END) # => 0
|
1860
|
+
# f.tell # => 12
|
1861
|
+
# f.close
|
1862
|
+
#
|
1863
|
+
# * `:SET` or `IO:SEEK_SET`: Repositions the stream to the given `offset`:
|
1864
|
+
#
|
1865
|
+
# f = File.open('t.txt')
|
1866
|
+
# f.tell # => 0
|
1867
|
+
# f.seek(20, :SET) # => 0
|
1868
|
+
# f.tell # => 20
|
1869
|
+
# f.seek(40, :SET) # => 0
|
1870
|
+
# f.tell # => 40
|
1871
|
+
# f.close
|
1872
|
+
#
|
1873
|
+
# Related: IO#pos=, IO#tell.
|
1874
|
+
#
|
1875
|
+
def seek: (Integer amount, ?Integer whence) -> Integer
|
1876
|
+
|
1877
|
+
# <!--
|
1878
|
+
# rdoc-file=io.c
|
1879
|
+
# - set_encoding(ext_enc) -> self
|
1880
|
+
# - set_encoding(ext_enc, int_enc, **enc_opts) -> self
|
1881
|
+
# - set_encoding('ext_enc:int_enc', **enc_opts) -> self
|
1882
|
+
# -->
|
1883
|
+
# See [Encodings](rdoc-ref:File@Encodings).
|
1884
|
+
#
|
1885
|
+
# Argument `ext_enc`, if given, must be an Encoding object or a String with the
|
1886
|
+
# encoding name; it is assigned as the encoding for the stream.
|
1887
|
+
#
|
1888
|
+
# Argument `int_enc`, if given, must be an Encoding object or a String with the
|
1889
|
+
# encoding name; it is assigned as the encoding for the internal string.
|
1890
|
+
#
|
1891
|
+
# Argument `'ext_enc:int_enc'`, if given, is a string containing two
|
1892
|
+
# colon-separated encoding names; corresponding Encoding objects are assigned as
|
1893
|
+
# the external and internal encodings for the stream.
|
1894
|
+
#
|
1895
|
+
# If the external encoding of a string is binary/ASCII-8BIT, the internal
|
1896
|
+
# encoding of the string is set to nil, since no transcoding is needed.
|
1897
|
+
#
|
1898
|
+
# Optional keyword arguments `enc_opts` specify [Encoding
|
1899
|
+
# options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
1900
|
+
#
|
1901
|
+
def set_encoding: (?String | Encoding ext_or_ext_int_enc) -> self
|
1902
|
+
| (?String | Encoding ext_or_ext_int_enc, ?String | Encoding int_enc) -> self
|
1903
|
+
|
1904
|
+
# <!--
|
1905
|
+
# rdoc-file=io.c
|
1906
|
+
# - set_encoding_by_bom -> encoding or nil
|
1907
|
+
# -->
|
1908
|
+
# If the stream begins with a BOM ([byte order
|
1909
|
+
# marker](https://en.wikipedia.org/wiki/Byte_order_mark)), consumes the BOM and
|
1910
|
+
# sets the external encoding accordingly; returns the result encoding if found,
|
1911
|
+
# or `nil` otherwise:
|
1912
|
+
#
|
1913
|
+
# File.write('t.tmp', "\u{FEFF}abc")
|
1914
|
+
# io = File.open('t.tmp', 'rb')
|
1915
|
+
# io.set_encoding_by_bom # => #<Encoding:UTF-8>
|
1916
|
+
# io.close
|
1917
|
+
#
|
1918
|
+
# File.write('t.tmp', 'abc')
|
1919
|
+
# io = File.open('t.tmp', 'rb')
|
1920
|
+
# io.set_encoding_by_bom # => nil
|
1921
|
+
# io.close
|
1922
|
+
#
|
1923
|
+
# Raises an exception if the stream is not binmode or its encoding has already
|
1924
|
+
# been set.
|
1925
|
+
#
|
1926
|
+
def set_encoding_by_bom: () -> Encoding?
|
1927
|
+
|
1928
|
+
# <!--
|
1929
|
+
# rdoc-file=file.c
|
1930
|
+
# - ios.stat -> stat
|
1931
|
+
# -->
|
1932
|
+
# Returns status information for *ios* as an object of type File::Stat.
|
1933
|
+
#
|
1934
|
+
# f = File.new("testfile")
|
1935
|
+
# s = f.stat
|
1936
|
+
# "%o" % s.mode #=> "100644"
|
1937
|
+
# s.blksize #=> 4096
|
1938
|
+
# s.atime #=> Wed Apr 09 08:53:54 CDT 2003
|
1939
|
+
#
|
1940
|
+
def stat: () -> File::Stat
|
1941
|
+
|
1942
|
+
# <!--
|
1943
|
+
# rdoc-file=io.c
|
1944
|
+
# - sync -> true or false
|
1945
|
+
# -->
|
1946
|
+
# Returns the current sync mode of the stream. When sync mode is true, all
|
1947
|
+
# output is immediately flushed to the underlying operating system and is not
|
1948
|
+
# buffered by Ruby internally. See also #fsync.
|
1949
|
+
#
|
1950
|
+
# f = File.open('t.tmp', 'w')
|
1951
|
+
# f.sync # => false
|
1952
|
+
# f.sync = true
|
1953
|
+
# f.sync # => true
|
1954
|
+
# f.close
|
1955
|
+
#
|
1956
|
+
def sync: () -> bool
|
1957
|
+
|
1958
|
+
# <!--
|
1959
|
+
# rdoc-file=io.c
|
1960
|
+
# - sync = boolean -> boolean
|
1961
|
+
# -->
|
1962
|
+
# Sets the *sync* *mode* for the stream to the given value; returns the given
|
1963
|
+
# value.
|
1964
|
+
#
|
1965
|
+
# Values for the sync mode:
|
1966
|
+
#
|
1967
|
+
# * `true`: All output is immediately flushed to the underlying operating
|
1968
|
+
# system and is not buffered internally.
|
1969
|
+
# * `false`: Output may be buffered internally.
|
1970
|
+
#
|
1971
|
+
# Example;
|
1972
|
+
#
|
1973
|
+
# f = File.open('t.tmp', 'w')
|
1974
|
+
# f.sync # => false
|
1975
|
+
# f.sync = true
|
1976
|
+
# f.sync # => true
|
1977
|
+
# f.close
|
1978
|
+
#
|
1979
|
+
# Related: IO#fsync.
|
1980
|
+
#
|
1981
|
+
def sync=: (boolish boolean) -> boolish
|
1982
|
+
|
1983
|
+
# <!--
|
1984
|
+
# rdoc-file=io.c
|
1985
|
+
# - sysread(maxlen) -> string
|
1986
|
+
# - sysread(maxlen, out_string) -> string
|
1987
|
+
# -->
|
1988
|
+
# Behaves like IO#readpartial, except that it uses low-level system functions.
|
1989
|
+
#
|
1990
|
+
# This method should not be used with other stream-reader methods.
|
1991
|
+
#
|
1992
|
+
def sysread: (Integer maxlen, String outbuf) -> String
|
1993
|
+
|
1994
|
+
# <!--
|
1995
|
+
# rdoc-file=io.c
|
1996
|
+
# - sysseek(offset, whence = IO::SEEK_SET) -> integer
|
1997
|
+
# -->
|
1998
|
+
# Behaves like IO#seek, except that it:
|
1999
|
+
#
|
2000
|
+
# * Uses low-level system functions.
|
2001
|
+
# * Returns the new position.
|
2002
|
+
#
|
2003
|
+
def sysseek: (Integer amount, ?Integer whence) -> Integer
|
2004
|
+
|
2005
|
+
# <!--
|
2006
|
+
# rdoc-file=io.c
|
2007
|
+
# - syswrite(object) -> integer
|
2008
|
+
# -->
|
2009
|
+
# Writes the given `object` to self, which must be opened for writing (see
|
2010
|
+
# Modes); returns the number bytes written. If `object` is not a string is
|
2011
|
+
# converted via method to_s:
|
2012
|
+
#
|
2013
|
+
# f = File.new('t.tmp', 'w')
|
2014
|
+
# f.syswrite('foo') # => 3
|
2015
|
+
# f.syswrite(30) # => 2
|
2016
|
+
# f.syswrite(:foo) # => 3
|
2017
|
+
# f.close
|
2018
|
+
#
|
2019
|
+
# This methods should not be used with other stream-writer methods.
|
2020
|
+
#
|
2021
|
+
def syswrite: (_ToS object) -> Integer
|
2022
|
+
|
2023
|
+
# <!--
|
2024
|
+
# rdoc-file=io.c
|
2025
|
+
# - tell -> integer
|
2026
|
+
# -->
|
2027
|
+
# Returns the current position (in bytes) in `self` (see
|
2028
|
+
# [Position](rdoc-ref:IO@Position)):
|
2029
|
+
#
|
2030
|
+
# f = File.open('t.txt')
|
2031
|
+
# f.tell # => 0
|
2032
|
+
# f.gets # => "First line\n"
|
2033
|
+
# f.tell # => 12
|
2034
|
+
# f.close
|
2035
|
+
#
|
2036
|
+
# Related: IO#pos=, IO#seek.
|
2037
|
+
#
|
2038
|
+
def tell: () -> Integer
|
2039
|
+
|
2040
|
+
# <!-- rdoc-file=io.c -->
|
2041
|
+
# Returns the current position (in bytes) in `self` (see
|
2042
|
+
# [Position](rdoc-ref:IO@Position)):
|
2043
|
+
#
|
2044
|
+
# f = File.open('t.txt')
|
2045
|
+
# f.tell # => 0
|
2046
|
+
# f.gets # => "First line\n"
|
2047
|
+
# f.tell # => 12
|
2048
|
+
# f.close
|
2049
|
+
#
|
2050
|
+
# Related: IO#pos=, IO#seek.
|
2051
|
+
#
|
2052
|
+
alias pos tell
|
2053
|
+
|
2054
|
+
# <!--
|
2055
|
+
# rdoc-file=io.c
|
2056
|
+
# - timeout -> duration or nil
|
2057
|
+
# -->
|
2058
|
+
# Get the internal timeout duration or nil if it was not set.
|
2059
|
+
#
|
2060
|
+
def timeout: () -> io_timeout
|
2061
|
+
|
2062
|
+
# The type used for timeouts in `IO`.
|
2063
|
+
#
|
2064
|
+
# Technically, this type should be `Time::_Timeout?`. However, in the vast majority of use-cases,
|
2065
|
+
# people aren't going to pass their own `_Timeout` in, so `Numeric` is returned for ergonomics
|
2066
|
+
# (eg `io.timeout += 10`).
|
2067
|
+
type io_timeout = Numeric?
|
2068
|
+
|
2069
|
+
# <!--
|
2070
|
+
# rdoc-file=io.c
|
2071
|
+
# - timeout = duration -> duration
|
2072
|
+
# - timeout = nil -> nil
|
2073
|
+
# -->
|
2074
|
+
# Sets the internal timeout to the specified duration or nil. The timeout
|
2075
|
+
# applies to all blocking operations where possible.
|
2076
|
+
#
|
2077
|
+
# When the operation performs longer than the timeout set, IO::TimeoutError is
|
2078
|
+
# raised.
|
2079
|
+
#
|
2080
|
+
# This affects the following methods (but is not limited to): #gets, #puts,
|
2081
|
+
# #read, #write, #wait_readable and #wait_writable. This also affects blocking
|
2082
|
+
# socket operations like Socket#accept and Socket#connect.
|
2083
|
+
#
|
2084
|
+
# Some operations like File#open and IO#close are not affected by the timeout. A
|
2085
|
+
# timeout during a write operation may leave the IO in an inconsistent state,
|
2086
|
+
# e.g. data was partially written. Generally speaking, a timeout is a last ditch
|
2087
|
+
# effort to prevent an application from hanging on slow I/O operations, such as
|
2088
|
+
# those that occur during a slowloris attack.
|
2089
|
+
#
|
2090
|
+
def timeout=: (io_timeout duration) -> void
|
2091
|
+
|
2092
|
+
# <!--
|
2093
|
+
# rdoc-file=io.c
|
2094
|
+
# - to_io -> self
|
2095
|
+
# -->
|
2096
|
+
# Returns `self`.
|
2097
|
+
#
|
2098
|
+
def to_io: () -> self
|
2099
|
+
|
2100
|
+
# <!-- rdoc-file=io.c -->
|
2101
|
+
# Returns `true` if the stream is associated with a terminal device (tty),
|
2102
|
+
# `false` otherwise:
|
2103
|
+
#
|
2104
|
+
# f = File.new('t.txt').isatty #=> false
|
2105
|
+
# f.close
|
2106
|
+
# f = File.new('/dev/tty').isatty #=> true
|
2107
|
+
# f.close
|
2108
|
+
#
|
2109
|
+
alias tty? isatty
|
2110
|
+
|
2111
|
+
# <!--
|
2112
|
+
# rdoc-file=io.c
|
2113
|
+
# - ungetbyte(integer) -> nil
|
2114
|
+
# - ungetbyte(string) -> nil
|
2115
|
+
# -->
|
2116
|
+
# Pushes back ("unshifts") the given data onto the stream's buffer, placing the
|
2117
|
+
# data so that it is next to be read; returns `nil`. See [Byte
|
2118
|
+
# IO](rdoc-ref:IO@Byte+IO).
|
2119
|
+
#
|
2120
|
+
# Note that:
|
2121
|
+
#
|
2122
|
+
# * Calling the method has no effect with unbuffered reads (such as
|
2123
|
+
# IO#sysread).
|
2124
|
+
# * Calling #rewind on the stream discards the pushed-back data.
|
2125
|
+
#
|
2126
|
+
# When argument `integer` is given, uses only its low-order byte:
|
2127
|
+
#
|
2128
|
+
# File.write('t.tmp', '012')
|
2129
|
+
# f = File.open('t.tmp')
|
2130
|
+
# f.ungetbyte(0x41) # => nil
|
2131
|
+
# f.read # => "A012"
|
2132
|
+
# f.rewind
|
2133
|
+
# f.ungetbyte(0x4243) # => nil
|
2134
|
+
# f.read # => "C012"
|
2135
|
+
# f.close
|
2136
|
+
#
|
2137
|
+
# When argument `string` is given, uses all bytes:
|
2138
|
+
#
|
2139
|
+
# File.write('t.tmp', '012')
|
2140
|
+
# f = File.open('t.tmp')
|
2141
|
+
# f.ungetbyte('A') # => nil
|
2142
|
+
# f.read # => "A012"
|
2143
|
+
# f.rewind
|
2144
|
+
# f.ungetbyte('BCDE') # => nil
|
2145
|
+
# f.read # => "BCDE012"
|
2146
|
+
# f.close
|
2147
|
+
#
|
2148
|
+
def ungetbyte: (String | Integer object) -> nil
|
2149
|
+
|
2150
|
+
# <!--
|
2151
|
+
# rdoc-file=io.c
|
2152
|
+
# - ungetc(integer) -> nil
|
2153
|
+
# - ungetc(string) -> nil
|
2154
|
+
# -->
|
2155
|
+
# Pushes back ("unshifts") the given data onto the stream's buffer, placing the
|
2156
|
+
# data so that it is next to be read; returns `nil`. See [Character
|
2157
|
+
# IO](rdoc-ref:IO@Character+IO).
|
2158
|
+
#
|
2159
|
+
# Note that:
|
2160
|
+
#
|
2161
|
+
# * Calling the method has no effect with unbuffered reads (such as
|
2162
|
+
# IO#sysread).
|
2163
|
+
# * Calling #rewind on the stream discards the pushed-back data.
|
2164
|
+
#
|
2165
|
+
# When argument `integer` is given, interprets the integer as a character:
|
2166
|
+
#
|
2167
|
+
# File.write('t.tmp', '012')
|
2168
|
+
# f = File.open('t.tmp')
|
2169
|
+
# f.ungetc(0x41) # => nil
|
2170
|
+
# f.read # => "A012"
|
2171
|
+
# f.rewind
|
2172
|
+
# f.ungetc(0x0442) # => nil
|
2173
|
+
# f.getc.ord # => 1090
|
2174
|
+
# f.close
|
2175
|
+
#
|
2176
|
+
# When argument `string` is given, uses all characters:
|
2177
|
+
#
|
2178
|
+
# File.write('t.tmp', '012')
|
2179
|
+
# f = File.open('t.tmp')
|
2180
|
+
# f.ungetc('A') # => nil
|
2181
|
+
# f.read # => "A012"
|
2182
|
+
# f.rewind
|
2183
|
+
# f.ungetc("\u0442\u0435\u0441\u0442") # => nil
|
2184
|
+
# f.getc.ord # => 1090
|
2185
|
+
# f.getc.ord # => 1077
|
2186
|
+
# f.getc.ord # => 1089
|
2187
|
+
# f.getc.ord # => 1090
|
2188
|
+
# f.close
|
2189
|
+
#
|
2190
|
+
def ungetc: (String object) -> nil
|
2191
|
+
|
2192
|
+
# <!--
|
2193
|
+
# rdoc-file=io.c
|
2194
|
+
# - write(*objects) -> integer
|
2195
|
+
# -->
|
2196
|
+
# Writes each of the given `objects` to `self`, which must be opened for writing
|
2197
|
+
# (see [Access Modes](rdoc-ref:File@Access+Modes)); returns the total number
|
2198
|
+
# bytes written; each of `objects` that is not a string is converted via method
|
2199
|
+
# `to_s`:
|
2200
|
+
#
|
2201
|
+
# $stdout.write('Hello', ', ', 'World!', "\n") # => 14
|
2202
|
+
# $stdout.write('foo', :bar, 2, "\n") # => 8
|
2203
|
+
#
|
2204
|
+
# Output:
|
2205
|
+
#
|
2206
|
+
# Hello, World!
|
2207
|
+
# foobar2
|
2208
|
+
#
|
2209
|
+
# Related: IO#read.
|
2210
|
+
#
|
2211
|
+
def write: (*_ToS string) -> Integer
|
2212
|
+
|
2213
|
+
# <!--
|
2214
|
+
# rdoc-file=io.rb
|
2215
|
+
# - ios.write_nonblock(string) -> integer
|
2216
|
+
# - ios.write_nonblock(string [, options]) -> integer
|
2217
|
+
# -->
|
2218
|
+
# Writes the given string to *ios* using the write(2) system call after
|
2219
|
+
# O_NONBLOCK is set for the underlying file descriptor.
|
2220
|
+
#
|
2221
|
+
# It returns the number of bytes written.
|
2222
|
+
#
|
2223
|
+
# write_nonblock just calls the write(2) system call. It causes all errors the
|
2224
|
+
# write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result
|
2225
|
+
# may also be smaller than string.length (partial write). The caller should care
|
2226
|
+
# such errors and partial write.
|
2227
|
+
#
|
2228
|
+
# If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by
|
2229
|
+
# IO::WaitWritable. So IO::WaitWritable can be used to rescue the exceptions for
|
2230
|
+
# retrying write_nonblock.
|
2231
|
+
#
|
2232
|
+
# # Creates a pipe.
|
2233
|
+
# r, w = IO.pipe
|
2234
|
+
#
|
2235
|
+
# # write_nonblock writes only 65536 bytes and return 65536.
|
2236
|
+
# # (The pipe size is 65536 bytes on this environment.)
|
2237
|
+
# s = "a" * 100000
|
2238
|
+
# p w.write_nonblock(s) #=> 65536
|
2239
|
+
#
|
2240
|
+
# # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN).
|
2241
|
+
# p w.write_nonblock("b") # Resource temporarily unavailable (Errno::EAGAIN)
|
2242
|
+
#
|
2243
|
+
# If the write buffer is not empty, it is flushed at first.
|
2244
|
+
#
|
2245
|
+
# When write_nonblock raises an exception kind of IO::WaitWritable,
|
2246
|
+
# write_nonblock should not be called until io is writable for avoiding busy
|
2247
|
+
# loop. This can be done as follows.
|
2248
|
+
#
|
2249
|
+
# begin
|
2250
|
+
# result = io.write_nonblock(string)
|
2251
|
+
# rescue IO::WaitWritable, Errno::EINTR
|
2252
|
+
# IO.select(nil, [io])
|
2253
|
+
# retry
|
2254
|
+
# end
|
2255
|
+
#
|
2256
|
+
# Note that this doesn't guarantee to write all data in string. The length
|
2257
|
+
# written is reported as result and it should be checked later.
|
2258
|
+
#
|
2259
|
+
# On some platforms such as Windows, write_nonblock is not supported according
|
2260
|
+
# to the kind of the IO object. In such cases, write_nonblock raises
|
2261
|
+
# `Errno::EBADF`.
|
2262
|
+
#
|
2263
|
+
# By specifying a keyword argument *exception* to `false`, you can indicate that
|
2264
|
+
# write_nonblock should not raise an IO::WaitWritable exception, but return the
|
2265
|
+
# symbol `:wait_writable` instead.
|
2266
|
+
#
|
2267
|
+
def write_nonblock: (_ToS s, ?exception: true) -> Integer
|
2268
|
+
| (_ToS s, exception: false) -> (Integer | :wait_writable | nil)
|
2269
|
+
|
2270
|
+
# <!--
|
2271
|
+
# rdoc-file=io.c
|
2272
|
+
# - IO.binread(path, length = nil, offset = 0) -> string or nil
|
2273
|
+
# -->
|
2274
|
+
# Behaves like IO.read, except that the stream is opened in binary mode with
|
2275
|
+
# ASCII-8BIT encoding.
|
2276
|
+
#
|
2277
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2278
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2279
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2280
|
+
#
|
2281
|
+
def self.binread: (String name, ?Integer? length, ?Integer offset) -> String
|
2282
|
+
|
2283
|
+
# <!--
|
2284
|
+
# rdoc-file=io.c
|
2285
|
+
# - IO.binwrite(path, string, offset = 0) -> integer
|
2286
|
+
# -->
|
2287
|
+
# Behaves like IO.write, except that the stream is opened in binary mode with
|
2288
|
+
# ASCII-8BIT encoding.
|
2289
|
+
#
|
2290
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2291
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2292
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2293
|
+
#
|
2294
|
+
def self.binwrite: (String name, _ToS string, ?Integer offset, ?mode: String mode) -> Integer
|
2295
|
+
|
2296
|
+
# <!--
|
2297
|
+
# rdoc-file=io.c
|
2298
|
+
# - IO.copy_stream(src, dst, src_length = nil, src_offset = 0) -> integer
|
2299
|
+
# -->
|
2300
|
+
# Copies from the given `src` to the given `dst`, returning the number of bytes
|
2301
|
+
# copied.
|
2302
|
+
#
|
2303
|
+
# * The given `src` must be one of the following:
|
2304
|
+
#
|
2305
|
+
# * The path to a readable file, from which source data is to be read.
|
2306
|
+
# * An IO-like object, opened for reading and capable of responding to
|
2307
|
+
# method `:readpartial` or method `:read`.
|
2308
|
+
#
|
2309
|
+
# * The given `dst` must be one of the following:
|
2310
|
+
#
|
2311
|
+
# * The path to a writable file, to which data is to be written.
|
2312
|
+
# * An IO-like object, opened for writing and capable of responding to
|
2313
|
+
# method `:write`.
|
2314
|
+
#
|
2315
|
+
# The examples here use file `t.txt` as source:
|
2316
|
+
#
|
2317
|
+
# File.read('t.txt')
|
2318
|
+
# # => "First line\nSecond line\n\nThird line\nFourth line\n"
|
2319
|
+
# File.read('t.txt').size # => 47
|
2320
|
+
#
|
2321
|
+
# If only arguments `src` and `dst` are given, the entire source stream is
|
2322
|
+
# copied:
|
2323
|
+
#
|
2324
|
+
# # Paths.
|
2325
|
+
# IO.copy_stream('t.txt', 't.tmp') # => 47
|
2326
|
+
#
|
2327
|
+
# # IOs (recall that a File is also an IO).
|
2328
|
+
# src_io = File.open('t.txt', 'r') # => #<File:t.txt>
|
2329
|
+
# dst_io = File.open('t.tmp', 'w') # => #<File:t.tmp>
|
2330
|
+
# IO.copy_stream(src_io, dst_io) # => 47
|
2331
|
+
# src_io.close
|
2332
|
+
# dst_io.close
|
2333
|
+
#
|
2334
|
+
# With argument `src_length` a non-negative integer, no more than that many
|
2335
|
+
# bytes are copied:
|
2336
|
+
#
|
2337
|
+
# IO.copy_stream('t.txt', 't.tmp', 10) # => 10
|
2338
|
+
# File.read('t.tmp') # => "First line"
|
2339
|
+
#
|
2340
|
+
# With argument `src_offset` also given, the source stream is read beginning at
|
2341
|
+
# that offset:
|
2342
|
+
#
|
2343
|
+
# IO.copy_stream('t.txt', 't.tmp', 11, 11) # => 11
|
2344
|
+
# IO.read('t.tmp') # => "Second line"
|
2345
|
+
#
|
2346
|
+
def self.copy_stream: (String | _Reader | _ReaderPartial src, String | _Writer dst, ?Integer? copy_length, ?Integer src_offset) -> Integer
|
2347
|
+
|
2348
|
+
# <!--
|
2349
|
+
# rdoc-file=io.c
|
2350
|
+
# - IO.popen(env = {}, cmd, mode = 'r', **opts) -> io
|
2351
|
+
# - IO.popen(env = {}, cmd, mode = 'r', **opts) {|io| ... } -> object
|
2352
|
+
# -->
|
2353
|
+
# Executes the given command `cmd` as a subprocess whose $stdin and $stdout are
|
2354
|
+
# connected to a new stream `io`.
|
2355
|
+
#
|
2356
|
+
# This method has potential security vulnerabilities if called with untrusted
|
2357
|
+
# input; see [Command Injection](rdoc-ref:command_injection.rdoc).
|
2358
|
+
#
|
2359
|
+
# If no block is given, returns the new stream, which depending on given `mode`
|
2360
|
+
# may be open for reading, writing, or both. The stream should be explicitly
|
2361
|
+
# closed (eventually) to avoid resource leaks.
|
2362
|
+
#
|
2363
|
+
# If a block is given, the stream is passed to the block (again, open for
|
2364
|
+
# reading, writing, or both); when the block exits, the stream is closed, and
|
2365
|
+
# the block's value is assigned to global variable `$?` and returned.
|
2366
|
+
#
|
2367
|
+
# Optional argument `mode` may be any valid IO mode. See [Access
|
2368
|
+
# Modes](rdoc-ref:File@Access+Modes).
|
2369
|
+
#
|
2370
|
+
# Required argument `cmd` determines which of the following occurs:
|
2371
|
+
#
|
2372
|
+
# * The process forks.
|
2373
|
+
# * A specified program runs in a shell.
|
2374
|
+
# * A specified program runs with specified arguments.
|
2375
|
+
# * A specified program runs with specified arguments and a specified `argv0`.
|
2376
|
+
#
|
2377
|
+
# Each of these is detailed below.
|
2378
|
+
#
|
2379
|
+
# The optional hash argument `env` specifies name/value pairs that are to be
|
2380
|
+
# added to the environment variables for the subprocess:
|
2381
|
+
#
|
2382
|
+
# IO.popen({'FOO' => 'bar'}, 'ruby', 'r+') do |pipe|
|
2383
|
+
# pipe.puts 'puts ENV["FOO"]'
|
2384
|
+
# pipe.close_write
|
2385
|
+
# pipe.gets
|
2386
|
+
# end => "bar\n"
|
2387
|
+
#
|
2388
|
+
# Optional keyword arguments `opts` specify:
|
2389
|
+
#
|
2390
|
+
# * [Open options](rdoc-ref:IO@Open+Options).
|
2391
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2392
|
+
# * Options for Kernel#spawn.
|
2393
|
+
#
|
2394
|
+
# **Forked \Process**
|
2395
|
+
#
|
2396
|
+
# When argument `cmd` is the 1-character string `'-'`, causes the process to
|
2397
|
+
# fork:
|
2398
|
+
# IO.popen('-') do |pipe|
|
2399
|
+
# if pipe
|
2400
|
+
# $stderr.puts "In parent, child pid is #{pipe.pid}\n"
|
2401
|
+
# else
|
2402
|
+
# $stderr.puts "In child, pid is #{$$}\n"
|
2403
|
+
# end
|
2404
|
+
# end
|
2405
|
+
#
|
2406
|
+
# Output:
|
2407
|
+
#
|
2408
|
+
# In parent, child pid is 26253
|
2409
|
+
# In child, pid is 26253
|
2410
|
+
#
|
2411
|
+
# Note that this is not supported on all platforms.
|
2412
|
+
#
|
2413
|
+
# **Shell Subprocess**
|
2414
|
+
#
|
2415
|
+
# When argument `cmd` is a single string (but not `'-'`), the program named
|
2416
|
+
# `cmd` is run as a shell command:
|
2417
|
+
#
|
2418
|
+
# IO.popen('uname') do |pipe|
|
2419
|
+
# pipe.readlines
|
2420
|
+
# end
|
2421
|
+
#
|
2422
|
+
# Output:
|
2423
|
+
#
|
2424
|
+
# ["Linux\n"]
|
2425
|
+
#
|
2426
|
+
# Another example:
|
2427
|
+
#
|
2428
|
+
# IO.popen('/bin/sh', 'r+') do |pipe|
|
2429
|
+
# pipe.puts('ls')
|
2430
|
+
# pipe.close_write
|
2431
|
+
# $stderr.puts pipe.readlines.size
|
2432
|
+
# end
|
2433
|
+
#
|
2434
|
+
# Output:
|
2435
|
+
#
|
2436
|
+
# 213
|
2437
|
+
#
|
2438
|
+
# **Program Subprocess**
|
2439
|
+
#
|
2440
|
+
# When argument `cmd` is an array of strings, the program named `cmd[0]` is run
|
2441
|
+
# with all elements of `cmd` as its arguments:
|
2442
|
+
#
|
2443
|
+
# IO.popen(['du', '..', '.']) do |pipe|
|
2444
|
+
# $stderr.puts pipe.readlines.size
|
2445
|
+
# end
|
2446
|
+
#
|
2447
|
+
# Output:
|
2448
|
+
#
|
2449
|
+
# 1111
|
2450
|
+
#
|
2451
|
+
# **Program Subprocess with `argv0`**
|
2452
|
+
#
|
2453
|
+
# When argument `cmd` is an array whose first element is a 2-element string
|
2454
|
+
# array and whose remaining elements (if any) are strings:
|
2455
|
+
#
|
2456
|
+
# * `cmd[0][0]` (the first string in the nested array) is the name of a
|
2457
|
+
# program that is run.
|
2458
|
+
# * `cmd[0][1]` (the second string in the nested array) is set as the
|
2459
|
+
# program's `argv[0]`.
|
2460
|
+
# * `cmd[1..-1]` (the strings in the outer array) are the program's arguments.
|
2461
|
+
#
|
2462
|
+
# Example (sets `$0` to 'foo'):
|
2463
|
+
#
|
2464
|
+
# IO.popen([['/bin/sh', 'foo'], '-c', 'echo $0']).read # => "foo\n"
|
2465
|
+
#
|
2466
|
+
# **Some Special Examples**
|
2467
|
+
#
|
2468
|
+
# # Set IO encoding.
|
2469
|
+
# IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
|
2470
|
+
# euc_jp_string = nkf_io.read
|
2471
|
+
# }
|
2472
|
+
#
|
2473
|
+
# # Merge standard output and standard error using Kernel#spawn option. See Kernel#spawn.
|
2474
|
+
# IO.popen(["ls", "/", :err=>[:child, :out]]) do |io|
|
2475
|
+
# ls_result_with_error = io.read
|
2476
|
+
# end
|
2477
|
+
#
|
2478
|
+
# # Use mixture of spawn options and IO options.
|
2479
|
+
# IO.popen(["ls", "/"], :err=>[:child, :out]) do |io|
|
2480
|
+
# ls_result_with_error = io.read
|
2481
|
+
# end
|
2482
|
+
#
|
2483
|
+
# f = IO.popen("uname")
|
2484
|
+
# p f.readlines
|
2485
|
+
# f.close
|
2486
|
+
# puts "Parent is #{Process.pid}"
|
2487
|
+
# IO.popen("date") {|f| puts f.gets }
|
2488
|
+
# IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
|
2489
|
+
# p $?
|
2490
|
+
# IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
|
2491
|
+
# f.puts "bar"; f.close_write; puts f.gets
|
2492
|
+
# }
|
2493
|
+
#
|
2494
|
+
# Output (from last section):
|
2495
|
+
#
|
2496
|
+
# ["Linux\n"]
|
2497
|
+
# Parent is 21346
|
2498
|
+
# Thu Jan 15 22:41:19 JST 2009
|
2499
|
+
# 21346 is here, f is #<IO:fd 3>
|
2500
|
+
# 21352 is here, f is nil
|
2501
|
+
# #<Process::Status: pid 21352 exit 0>
|
2502
|
+
# <foo>bar;zot;
|
2503
|
+
#
|
2504
|
+
# Raises exceptions that IO.pipe and Kernel.spawn raise.
|
2505
|
+
#
|
2506
|
+
def self.popen: (string | cmd_array cmd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?unsetenv_others: boolish, ?pgroup: true | Integer, ?umask: Integer, ?in: Kernel::redirect_fd, ?out: Kernel::redirect_fd, ?err: Kernel::redirect_fd, ?close_others: boolish, ?chdir: String) -> instance
|
2507
|
+
| (Hash[string, string?] env, string | cmd_array cmd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?unsetenv_others: boolish, ?pgroup: true | Integer, ?umask: Integer, ?in: Kernel::redirect_fd, ?out: Kernel::redirect_fd, ?err: Kernel::redirect_fd, ?close_others: boolish, ?chdir: String) -> instance
|
2508
|
+
| [X] (string | cmd_array cmd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?unsetenv_others: boolish, ?pgroup: true | Integer, ?umask: Integer, ?in: Kernel::redirect_fd, ?out: Kernel::redirect_fd, ?err: Kernel::redirect_fd, ?close_others: boolish, ?chdir: String) { (instance) -> X } -> X
|
2509
|
+
| [X] (Hash[string, string?] env, string | cmd_array cmd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?unsetenv_others: boolish, ?pgroup: true | Integer, ?umask: Integer, ?in: Kernel::redirect_fd, ?out: Kernel::redirect_fd, ?err: Kernel::redirect_fd, ?close_others: boolish, ?chdir: String) { (instance) -> X } -> X
|
2510
|
+
|
2511
|
+
# The command can be given as:
|
2512
|
+
#
|
2513
|
+
# * Array of string `["ruby", "-v"]`, or
|
2514
|
+
# * Array of string with the first element of array `[["ruby", "RUBY"], "-v"]`
|
2515
|
+
#
|
2516
|
+
# But RBS cannot define such a type. So this is simply a union of `string` or `[String, String]`.
|
2517
|
+
#
|
2518
|
+
type cmd_array = array[string | [String, String]]
|
2519
|
+
|
2520
|
+
# <!--
|
2521
|
+
# rdoc-file=io.c
|
2522
|
+
# - IO.foreach(path, sep = $/, **opts) {|line| block } -> nil
|
2523
|
+
# - IO.foreach(path, limit, **opts) {|line| block } -> nil
|
2524
|
+
# - IO.foreach(path, sep, limit, **opts) {|line| block } -> nil
|
2525
|
+
# - IO.foreach(...) -> an_enumerator
|
2526
|
+
# -->
|
2527
|
+
# Calls the block with each successive line read from the stream.
|
2528
|
+
#
|
2529
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2530
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2531
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2532
|
+
#
|
2533
|
+
# The first argument must be a string that is the path to a file.
|
2534
|
+
#
|
2535
|
+
# With only argument `path` given, parses lines from the file at the given
|
2536
|
+
# `path`, as determined by the default line separator, and calls the block with
|
2537
|
+
# each successive line:
|
2538
|
+
#
|
2539
|
+
# File.foreach('t.txt') {|line| p line }
|
2540
|
+
#
|
2541
|
+
# Output: the same as above.
|
2542
|
+
#
|
2543
|
+
# For both forms, command and path, the remaining arguments are the same.
|
2544
|
+
#
|
2545
|
+
# With argument `sep` given, parses lines as determined by that line separator
|
2546
|
+
# (see [Line Separator](rdoc-ref:IO@Line+Separator)):
|
2547
|
+
#
|
2548
|
+
# File.foreach('t.txt', 'li') {|line| p line }
|
2549
|
+
#
|
2550
|
+
# Output:
|
2551
|
+
#
|
2552
|
+
# "First li"
|
2553
|
+
# "ne\nSecond li"
|
2554
|
+
# "ne\n\nThird li"
|
2555
|
+
# "ne\nFourth li"
|
2556
|
+
# "ne\n"
|
2557
|
+
#
|
2558
|
+
# Each paragraph:
|
2559
|
+
#
|
2560
|
+
# File.foreach('t.txt', '') {|paragraph| p paragraph }
|
2561
|
+
#
|
2562
|
+
# Output:
|
2563
|
+
#
|
2564
|
+
# "First line\nSecond line\n\n"
|
2565
|
+
# "Third line\nFourth line\n"
|
2566
|
+
#
|
2567
|
+
# With argument `limit` given, parses lines as determined by the default line
|
2568
|
+
# separator and the given line-length limit (see [Line
|
2569
|
+
# Separator](rdoc-ref:IO@Line+Separator) and [Line
|
2570
|
+
# Limit](rdoc-ref:IO@Line+Limit)):
|
2571
|
+
#
|
2572
|
+
# File.foreach('t.txt', 7) {|line| p line }
|
2573
|
+
#
|
2574
|
+
# Output:
|
2575
|
+
#
|
2576
|
+
# "First l"
|
2577
|
+
# "ine\n"
|
2578
|
+
# "Second "
|
2579
|
+
# "line\n"
|
2580
|
+
# "\n"
|
2581
|
+
# "Third l"
|
2582
|
+
# "ine\n"
|
2583
|
+
# "Fourth l"
|
2584
|
+
# "line\n"
|
2585
|
+
#
|
2586
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
2587
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
2588
|
+
#
|
2589
|
+
# Optional keyword arguments `opts` specify:
|
2590
|
+
#
|
2591
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
2592
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2593
|
+
# * [Line Options](rdoc-ref:IO@Line+IO).
|
2594
|
+
#
|
2595
|
+
# Returns an Enumerator if no block is given.
|
2596
|
+
#
|
2597
|
+
def self.foreach: (string | _ToPath path, ?String sep, ?Integer limit, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?chomp: boolish) { (String line) -> void } -> nil
|
2598
|
+
| (string | _ToPath path, ?String sep, ?Integer limit, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?chomp: boolish) -> ::Enumerator[String, nil]
|
2599
|
+
|
2600
|
+
# <!--
|
2601
|
+
# rdoc-file=io.c
|
2602
|
+
# - IO.pipe(**opts) -> [read_io, write_io]
|
2603
|
+
# - IO.pipe(enc, **opts) -> [read_io, write_io]
|
2604
|
+
# - IO.pipe(ext_enc, int_enc, **opts) -> [read_io, write_io]
|
2605
|
+
# - IO.pipe(**opts) {|read_io, write_io| ...} -> object
|
2606
|
+
# - IO.pipe(enc, **opts) {|read_io, write_io| ...} -> object
|
2607
|
+
# - IO.pipe(ext_enc, int_enc, **opts) {|read_io, write_io| ...} -> object
|
2608
|
+
# -->
|
2609
|
+
# Creates a pair of pipe endpoints, `read_io` and `write_io`, connected to each
|
2610
|
+
# other.
|
2611
|
+
#
|
2612
|
+
# If argument `enc_string` is given, it must be a string containing one of:
|
2613
|
+
#
|
2614
|
+
# * The name of the encoding to be used as the external encoding.
|
2615
|
+
# * The colon-separated names of two encodings to be used as the external and
|
2616
|
+
# internal encodings.
|
2617
|
+
#
|
2618
|
+
# If argument `int_enc` is given, it must be an Encoding object or encoding name
|
2619
|
+
# string that specifies the internal encoding to be used; if argument `ext_enc`
|
2620
|
+
# is also given, it must be an Encoding object or encoding name string that
|
2621
|
+
# specifies the external encoding to be used.
|
2622
|
+
#
|
2623
|
+
# The string read from `read_io` is tagged with the external encoding; if an
|
2624
|
+
# internal encoding is also specified, the string is converted to, and tagged
|
2625
|
+
# with, that encoding.
|
2626
|
+
#
|
2627
|
+
# If any encoding is specified, optional hash arguments specify the conversion
|
2628
|
+
# option.
|
2629
|
+
#
|
2630
|
+
# Optional keyword arguments `opts` specify:
|
2631
|
+
#
|
2632
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
2633
|
+
# * [Encoding Options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2634
|
+
#
|
2635
|
+
# With no block given, returns the two endpoints in an array:
|
2636
|
+
#
|
2637
|
+
# IO.pipe # => [#<IO:fd 4>, #<IO:fd 5>]
|
2638
|
+
#
|
2639
|
+
# With a block given, calls the block with the two endpoints; closes both
|
2640
|
+
# endpoints and returns the value of the block:
|
2641
|
+
#
|
2642
|
+
# IO.pipe {|read_io, write_io| p read_io; p write_io }
|
2643
|
+
#
|
2644
|
+
# Output:
|
2645
|
+
#
|
2646
|
+
# #<IO:fd 6>
|
2647
|
+
# #<IO:fd 7>
|
2648
|
+
#
|
2649
|
+
# Not available on all platforms.
|
2650
|
+
#
|
2651
|
+
# In the example below, the two processes close the ends of the pipe that they
|
2652
|
+
# are not using. This is not just a cosmetic nicety. The read end of a pipe will
|
2653
|
+
# not generate an end of file condition if there are any writers with the pipe
|
2654
|
+
# still open. In the case of the parent process, the `rd.read` will never return
|
2655
|
+
# if it does not first issue a `wr.close`:
|
2656
|
+
#
|
2657
|
+
# rd, wr = IO.pipe
|
2658
|
+
#
|
2659
|
+
# if fork
|
2660
|
+
# wr.close
|
2661
|
+
# puts "Parent got: <#{rd.read}>"
|
2662
|
+
# rd.close
|
2663
|
+
# Process.wait
|
2664
|
+
# else
|
2665
|
+
# rd.close
|
2666
|
+
# puts 'Sending message to parent'
|
2667
|
+
# wr.write "Hi Dad"
|
2668
|
+
# wr.close
|
2669
|
+
# end
|
2670
|
+
#
|
2671
|
+
# *produces:*
|
2672
|
+
#
|
2673
|
+
# Sending message to parent
|
2674
|
+
# Parent got: <Hi Dad>
|
2675
|
+
#
|
2676
|
+
def self.pipe: (?String | Encoding | nil ext_or_ext_int_enc, ?String | Encoding | nil int_enc, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?chomp: boolish) -> [IO, IO]
|
2677
|
+
| [X] (?String | Encoding | nil ext_or_ext_int_enc, ?String | Encoding | nil int_enc, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?chomp: boolish) { (IO read_io, IO write_io) -> X } -> X
|
2678
|
+
|
2679
|
+
# <!--
|
2680
|
+
# rdoc-file=io.c
|
2681
|
+
# - IO.read(path, length = nil, offset = 0, **opts) -> string or nil
|
2682
|
+
# -->
|
2683
|
+
# Opens the stream, reads and returns some or all of its content, and closes the
|
2684
|
+
# stream; returns `nil` if no bytes were read.
|
2685
|
+
#
|
2686
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2687
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2688
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2689
|
+
#
|
2690
|
+
# The first argument must be a string that is the path to a file.
|
2691
|
+
#
|
2692
|
+
# With only argument `path` given, reads in text mode and returns the entire
|
2693
|
+
# content of the file at the given path:
|
2694
|
+
#
|
2695
|
+
# IO.read('t.txt')
|
2696
|
+
# # => "First line\nSecond line\n\nThird line\nFourth line\n"
|
2697
|
+
#
|
2698
|
+
# On Windows, text mode can terminate reading and leave bytes in the file unread
|
2699
|
+
# when encountering certain special bytes. Consider using IO.binread if all
|
2700
|
+
# bytes in the file should be read.
|
2701
|
+
#
|
2702
|
+
# With argument `length`, returns `length` bytes if available:
|
2703
|
+
#
|
2704
|
+
# IO.read('t.txt', 7) # => "First l"
|
2705
|
+
# IO.read('t.txt', 700)
|
2706
|
+
# # => "First line\r\nSecond line\r\n\r\nFourth line\r\nFifth line\r\n"
|
2707
|
+
#
|
2708
|
+
# With arguments `length` and `offset`, returns `length` bytes if available,
|
2709
|
+
# beginning at the given `offset`:
|
2710
|
+
#
|
2711
|
+
# IO.read('t.txt', 10, 2) # => "rst line\nS"
|
2712
|
+
# IO.read('t.txt', 10, 200) # => nil
|
2713
|
+
#
|
2714
|
+
# Optional keyword arguments `opts` specify:
|
2715
|
+
#
|
2716
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
2717
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2718
|
+
#
|
2719
|
+
def self.read: (String name, ?Integer? length, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> String
|
2720
|
+
|
2721
|
+
# <!--
|
2722
|
+
# rdoc-file=io.c
|
2723
|
+
# - IO.readlines(path, sep = $/, **opts) -> array
|
2724
|
+
# - IO.readlines(path, limit, **opts) -> array
|
2725
|
+
# - IO.readlines(path, sep, limit, **opts) -> array
|
2726
|
+
# -->
|
2727
|
+
# Returns an array of all lines read from the stream.
|
2728
|
+
#
|
2729
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2730
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2731
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2732
|
+
#
|
2733
|
+
# The first argument must be a string that is the path to a file.
|
2734
|
+
#
|
2735
|
+
# With only argument `path` given, parses lines from the file at the given
|
2736
|
+
# `path`, as determined by the default line separator, and returns those lines
|
2737
|
+
# in an array:
|
2738
|
+
#
|
2739
|
+
# IO.readlines('t.txt')
|
2740
|
+
# # => ["First line\n", "Second line\n", "\n", "Third line\n", "Fourth line\n"]
|
2741
|
+
#
|
2742
|
+
# With argument `sep` given, parses lines as determined by that line separator
|
2743
|
+
# (see [Line Separator](rdoc-ref:IO@Line+Separator)):
|
2744
|
+
#
|
2745
|
+
# # Ordinary separator.
|
2746
|
+
# IO.readlines('t.txt', 'li')
|
2747
|
+
# # =>["First li", "ne\nSecond li", "ne\n\nThird li", "ne\nFourth li", "ne\n"]
|
2748
|
+
# # Get-paragraphs separator.
|
2749
|
+
# IO.readlines('t.txt', '')
|
2750
|
+
# # => ["First line\nSecond line\n\n", "Third line\nFourth line\n"]
|
2751
|
+
# # Get-all separator.
|
2752
|
+
# IO.readlines('t.txt', nil)
|
2753
|
+
# # => ["First line\nSecond line\n\nThird line\nFourth line\n"]
|
2754
|
+
#
|
2755
|
+
# With argument `limit` given, parses lines as determined by the default line
|
2756
|
+
# separator and the given line-length limit (see [Line
|
2757
|
+
# Separator](rdoc-ref:IO@Line+Separator) and [Line
|
2758
|
+
# Limit](rdoc-ref:IO@Line+Limit):
|
2759
|
+
#
|
2760
|
+
# IO.readlines('t.txt', 7)
|
2761
|
+
# # => ["First l", "ine\n", "Second ", "line\n", "\n", "Third l", "ine\n", "Fourth ", "line\n"]
|
2762
|
+
#
|
2763
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
2764
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
2765
|
+
#
|
2766
|
+
# Optional keyword arguments `opts` specify:
|
2767
|
+
#
|
2768
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
2769
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2770
|
+
# * [Line Options](rdoc-ref:IO@Line+IO).
|
2771
|
+
#
|
2772
|
+
def self.readlines: (String | _ToPath name, ?String sep, ?Integer limit, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String, ?chomp: boolish) -> ::Array[String]
|
2773
|
+
|
2774
|
+
# <!--
|
2775
|
+
# rdoc-file=io.c
|
2776
|
+
# - IO.select(read_ios, write_ios = [], error_ios = [], timeout = nil) -> array or nil
|
2777
|
+
# -->
|
2778
|
+
# Invokes system call [select(2)](https://linux.die.net/man/2/select), which
|
2779
|
+
# monitors multiple file descriptors, waiting until one or more of the file
|
2780
|
+
# descriptors becomes ready for some class of I/O operation.
|
2781
|
+
#
|
2782
|
+
# Not implemented on all platforms.
|
2783
|
+
#
|
2784
|
+
# Each of the arguments `read_ios`, `write_ios`, and `error_ios` is an array of
|
2785
|
+
# IO objects.
|
2786
|
+
#
|
2787
|
+
# Argument `timeout` is a numeric value (such as integer or float) timeout
|
2788
|
+
# interval in seconds.
|
2789
|
+
#
|
2790
|
+
# The method monitors the IO objects given in all three arrays, waiting for some
|
2791
|
+
# to be ready; returns a 3-element array whose elements are:
|
2792
|
+
#
|
2793
|
+
# * An array of the objects in `read_ios` that are ready for reading.
|
2794
|
+
# * An array of the objects in `write_ios` that are ready for writing.
|
2795
|
+
# * An array of the objects in `error_ios` have pending exceptions.
|
2796
|
+
#
|
2797
|
+
# If no object becomes ready within the given `timeout`, `nil` is returned.
|
2798
|
+
#
|
2799
|
+
# IO.select peeks the buffer of IO objects for testing readability. If the IO
|
2800
|
+
# buffer is not empty, IO.select immediately notifies readability. This "peek"
|
2801
|
+
# only happens for IO objects. It does not happen for IO-like objects such as
|
2802
|
+
# OpenSSL::SSL::SSLSocket.
|
2803
|
+
#
|
2804
|
+
# The best way to use IO.select is invoking it after non-blocking methods such
|
2805
|
+
# as #read_nonblock, #write_nonblock, etc. The methods raise an exception which
|
2806
|
+
# is extended by IO::WaitReadable or IO::WaitWritable. The modules notify how
|
2807
|
+
# the caller should wait with IO.select. If IO::WaitReadable is raised, the
|
2808
|
+
# caller should wait for reading. If IO::WaitWritable is raised, the caller
|
2809
|
+
# should wait for writing.
|
2810
|
+
#
|
2811
|
+
# So, blocking read (#readpartial) can be emulated using #read_nonblock and
|
2812
|
+
# IO.select as follows:
|
2813
|
+
#
|
2814
|
+
# begin
|
2815
|
+
# result = io_like.read_nonblock(maxlen)
|
2816
|
+
# rescue IO::WaitReadable
|
2817
|
+
# IO.select([io_like])
|
2818
|
+
# retry
|
2819
|
+
# rescue IO::WaitWritable
|
2820
|
+
# IO.select(nil, [io_like])
|
2821
|
+
# retry
|
2822
|
+
# end
|
2823
|
+
#
|
2824
|
+
# Especially, the combination of non-blocking methods and IO.select is preferred
|
2825
|
+
# for IO like objects such as OpenSSL::SSL::SSLSocket. It has #to_io method to
|
2826
|
+
# return underlying IO object. IO.select calls #to_io to obtain the file
|
2827
|
+
# descriptor to wait.
|
2828
|
+
#
|
2829
|
+
# This means that readability notified by IO.select doesn't mean readability
|
2830
|
+
# from OpenSSL::SSL::SSLSocket object.
|
2831
|
+
#
|
2832
|
+
# The most likely situation is that OpenSSL::SSL::SSLSocket buffers some data.
|
2833
|
+
# IO.select doesn't see the buffer. So IO.select can block when
|
2834
|
+
# OpenSSL::SSL::SSLSocket#readpartial doesn't block.
|
2835
|
+
#
|
2836
|
+
# However, several more complicated situations exist.
|
2837
|
+
#
|
2838
|
+
# SSL is a protocol which is sequence of records. The record consists of
|
2839
|
+
# multiple bytes. So, the remote side of SSL sends a partial record, IO.select
|
2840
|
+
# notifies readability but OpenSSL::SSL::SSLSocket cannot decrypt a byte and
|
2841
|
+
# OpenSSL::SSL::SSLSocket#readpartial will block.
|
2842
|
+
#
|
2843
|
+
# Also, the remote side can request SSL renegotiation which forces the local SSL
|
2844
|
+
# engine to write some data. This means OpenSSL::SSL::SSLSocket#readpartial may
|
2845
|
+
# invoke #write system call and it can block. In such a situation,
|
2846
|
+
# OpenSSL::SSL::SSLSocket#read_nonblock raises IO::WaitWritable instead of
|
2847
|
+
# blocking. So, the caller should wait for ready for writability as above
|
2848
|
+
# example.
|
2849
|
+
#
|
2850
|
+
# The combination of non-blocking methods and IO.select is also useful for
|
2851
|
+
# streams such as tty, pipe socket socket when multiple processes read from a
|
2852
|
+
# stream.
|
2853
|
+
#
|
2854
|
+
# Finally, Linux kernel developers don't guarantee that readability of select(2)
|
2855
|
+
# means readability of following read(2) even for a single process; see
|
2856
|
+
# [select(2)](https://linux.die.net/man/2/select)
|
2857
|
+
#
|
2858
|
+
# Invoking IO.select before IO#readpartial works well as usual. However it is
|
2859
|
+
# not the best way to use IO.select.
|
2860
|
+
#
|
2861
|
+
# The writability notified by select(2) doesn't show how many bytes are
|
2862
|
+
# writable. IO#write method blocks until given whole string is written. So,
|
2863
|
+
# `IO#write(two or more bytes)` can block after writability is notified by
|
2864
|
+
# IO.select. IO#write_nonblock is required to avoid the blocking.
|
2865
|
+
#
|
2866
|
+
# Blocking write (#write) can be emulated using #write_nonblock and IO.select as
|
2867
|
+
# follows: IO::WaitReadable should also be rescued for SSL renegotiation in
|
2868
|
+
# OpenSSL::SSL::SSLSocket.
|
2869
|
+
#
|
2870
|
+
# while 0 < string.bytesize
|
2871
|
+
# begin
|
2872
|
+
# written = io_like.write_nonblock(string)
|
2873
|
+
# rescue IO::WaitReadable
|
2874
|
+
# IO.select([io_like])
|
2875
|
+
# retry
|
2876
|
+
# rescue IO::WaitWritable
|
2877
|
+
# IO.select(nil, [io_like])
|
2878
|
+
# retry
|
2879
|
+
# end
|
2880
|
+
# string = string.byteslice(written..-1)
|
2881
|
+
# end
|
2882
|
+
#
|
2883
|
+
# Example:
|
2884
|
+
#
|
2885
|
+
# rp, wp = IO.pipe
|
2886
|
+
# mesg = "ping "
|
2887
|
+
# 100.times {
|
2888
|
+
# # IO.select follows IO#read. Not the best way to use IO.select.
|
2889
|
+
# rs, ws, = IO.select([rp], [wp])
|
2890
|
+
# if r = rs[0]
|
2891
|
+
# ret = r.read(5)
|
2892
|
+
# print ret
|
2893
|
+
# case ret
|
2894
|
+
# when /ping/
|
2895
|
+
# mesg = "pong\n"
|
2896
|
+
# when /pong/
|
2897
|
+
# mesg = "ping "
|
2898
|
+
# end
|
2899
|
+
# end
|
2900
|
+
# if w = ws[0]
|
2901
|
+
# w.write(mesg)
|
2902
|
+
# end
|
2903
|
+
# }
|
2904
|
+
#
|
2905
|
+
# Output:
|
2906
|
+
#
|
2907
|
+
# ping pong
|
2908
|
+
# ping pong
|
2909
|
+
# ping pong
|
2910
|
+
# (snipped)
|
2911
|
+
# ping
|
2912
|
+
#
|
2913
|
+
def self.select: [X, Y, Z] (::Array[X & io]? read_array, ?::Array[Y & io]? write_array, ?::Array[Z & io]? error_array) -> [ Array[X], Array[Y], Array[Z] ]
|
2914
|
+
| [X, Y, Z] (::Array[X & io]? read_array, ?::Array[Y & io]? write_array, ?::Array[Z & io]? error_array, Time::_Timeout? timeout) -> [ Array[X], Array[Y], Array[Z] ]?
|
2915
|
+
|
2916
|
+
# <!--
|
2917
|
+
# rdoc-file=io.c
|
2918
|
+
# - IO.sysopen(path, mode = 'r', perm = 0666) -> integer
|
2919
|
+
# -->
|
2920
|
+
# Opens the file at the given path with the given mode and permissions; returns
|
2921
|
+
# the integer file descriptor.
|
2922
|
+
#
|
2923
|
+
# If the file is to be readable, it must exist; if the file is to be writable
|
2924
|
+
# and does not exist, it is created with the given permissions:
|
2925
|
+
#
|
2926
|
+
# File.write('t.tmp', '') # => 0
|
2927
|
+
# IO.sysopen('t.tmp') # => 8
|
2928
|
+
# IO.sysopen('t.tmp', 'w') # => 9
|
2929
|
+
#
|
2930
|
+
def self.sysopen: (String path, ?String mode, ?String perm) -> Integer
|
2931
|
+
|
2932
|
+
# <!--
|
2933
|
+
# rdoc-file=io.c
|
2934
|
+
# - IO.try_convert(object) -> new_io or nil
|
2935
|
+
# -->
|
2936
|
+
# Attempts to convert `object` into an IO object via method `to_io`; returns the
|
2937
|
+
# new IO object if successful, or `nil` otherwise:
|
2938
|
+
#
|
2939
|
+
# IO.try_convert(STDOUT) # => #<IO:<STDOUT>>
|
2940
|
+
# IO.try_convert(ARGF) # => #<IO:<STDIN>>
|
2941
|
+
# IO.try_convert('STDOUT') # => nil
|
2942
|
+
#
|
2943
|
+
def self.try_convert: (_ToIO obj) -> IO
|
2944
|
+
| (untyped obj) -> IO?
|
2945
|
+
|
2946
|
+
# <!--
|
2947
|
+
# rdoc-file=io.c
|
2948
|
+
# - IO.write(path, data, offset = 0, **opts) -> integer
|
2949
|
+
# -->
|
2950
|
+
# Opens the stream, writes the given `data` to it, and closes the stream;
|
2951
|
+
# returns the number of bytes written.
|
2952
|
+
#
|
2953
|
+
# When called from class IO (but not subclasses of IO), this method has
|
2954
|
+
# potential security vulnerabilities if called with untrusted input; see
|
2955
|
+
# [Command Injection](rdoc-ref:command_injection.rdoc).
|
2956
|
+
#
|
2957
|
+
# The first argument must be a string that is the path to a file.
|
2958
|
+
#
|
2959
|
+
# With only argument `path` given, writes the given `data` to the file at that
|
2960
|
+
# path:
|
2961
|
+
#
|
2962
|
+
# IO.write('t.tmp', 'abc') # => 3
|
2963
|
+
# File.read('t.tmp') # => "abc"
|
2964
|
+
#
|
2965
|
+
# If `offset` is zero (the default), the file is overwritten:
|
2966
|
+
#
|
2967
|
+
# IO.write('t.tmp', 'A') # => 1
|
2968
|
+
# File.read('t.tmp') # => "A"
|
2969
|
+
#
|
2970
|
+
# If `offset` in within the file content, the file is partly overwritten:
|
2971
|
+
#
|
2972
|
+
# IO.write('t.tmp', 'abcdef') # => 3
|
2973
|
+
# File.read('t.tmp') # => "abcdef"
|
2974
|
+
# # Offset within content.
|
2975
|
+
# IO.write('t.tmp', '012', 2) # => 3
|
2976
|
+
# File.read('t.tmp') # => "ab012f"
|
2977
|
+
#
|
2978
|
+
# If `offset` is outside the file content, the file is padded with null
|
2979
|
+
# characters `"\u0000"`:
|
2980
|
+
#
|
2981
|
+
# IO.write('t.tmp', 'xyz', 10) # => 3
|
2982
|
+
# File.read('t.tmp') # => "ab012f\u0000\u0000\u0000\u0000xyz"
|
2983
|
+
#
|
2984
|
+
# Optional keyword arguments `opts` specify:
|
2985
|
+
#
|
2986
|
+
# * [Open Options](rdoc-ref:IO@Open+Options).
|
2987
|
+
# * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
|
2988
|
+
#
|
2989
|
+
def self.write: (String path, _ToS data, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> Integer
|
2990
|
+
|
2991
|
+
# <!--
|
2992
|
+
# rdoc-file=io.c
|
2993
|
+
# - IO.for_fd(fd, mode = 'r', **opts) -> io
|
2994
|
+
# -->
|
2995
|
+
# Synonym for IO.new.
|
2996
|
+
#
|
2997
|
+
alias self.for_fd self.new
|
2998
|
+
|
2999
|
+
# <!--
|
3000
|
+
# rdoc-file=io.c
|
3001
|
+
# - IO.open(fd, mode = 'r', **opts) -> io
|
3002
|
+
# - IO.open(fd, mode = 'r', **opts) {|io| ... } -> object
|
3003
|
+
# -->
|
3004
|
+
# Creates a new IO object, via IO.new with the given arguments.
|
3005
|
+
#
|
3006
|
+
# With no block given, returns the IO object.
|
3007
|
+
#
|
3008
|
+
# With a block given, calls the block with the IO object and returns the block's
|
3009
|
+
# value.
|
3010
|
+
#
|
3011
|
+
def self.open: (int fd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> instance
|
3012
|
+
| [X] (int fd, ?string | int mode, ?path: string?, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) { (instance) -> X } -> X
|
3013
|
+
|
3014
|
+
# <!-- rdoc-file=io.c -->
|
3015
|
+
# Calls the block with each remaining line read from the stream; returns `self`.
|
3016
|
+
# Does nothing if already at end-of-stream; See [Line IO](rdoc-ref:IO@Line+IO).
|
3017
|
+
#
|
3018
|
+
# With no arguments given, reads lines as determined by line separator `$/`:
|
3019
|
+
#
|
3020
|
+
# f = File.new('t.txt')
|
3021
|
+
# f.each_line {|line| p line }
|
3022
|
+
# f.each_line {|line| fail 'Cannot happen' }
|
3023
|
+
# f.close
|
3024
|
+
#
|
3025
|
+
# Output:
|
3026
|
+
#
|
3027
|
+
# "First line\n"
|
3028
|
+
# "Second line\n"
|
3029
|
+
# "\n"
|
3030
|
+
# "Fourth line\n"
|
3031
|
+
# "Fifth line\n"
|
3032
|
+
#
|
3033
|
+
# With only string argument `sep` given, reads lines as determined by line
|
3034
|
+
# separator `sep`; see [Line Separator](rdoc-ref:IO@Line+Separator):
|
3035
|
+
#
|
3036
|
+
# f = File.new('t.txt')
|
3037
|
+
# f.each_line('li') {|line| p line }
|
3038
|
+
# f.close
|
3039
|
+
#
|
3040
|
+
# Output:
|
3041
|
+
#
|
3042
|
+
# "First li"
|
3043
|
+
# "ne\nSecond li"
|
3044
|
+
# "ne\n\nFourth li"
|
3045
|
+
# "ne\nFifth li"
|
3046
|
+
# "ne\n"
|
3047
|
+
#
|
3048
|
+
# The two special values for `sep` are honored:
|
3049
|
+
#
|
3050
|
+
# f = File.new('t.txt')
|
3051
|
+
# # Get all into one string.
|
3052
|
+
# f.each_line(nil) {|line| p line }
|
3053
|
+
# f.close
|
3054
|
+
#
|
3055
|
+
# Output:
|
3056
|
+
#
|
3057
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
3058
|
+
#
|
3059
|
+
# f.rewind
|
3060
|
+
# # Get paragraphs (up to two line separators).
|
3061
|
+
# f.each_line('') {|line| p line }
|
3062
|
+
#
|
3063
|
+
# Output:
|
3064
|
+
#
|
3065
|
+
# "First line\nSecond line\n\n"
|
3066
|
+
# "Fourth line\nFifth line\n"
|
3067
|
+
#
|
3068
|
+
# With only integer argument `limit` given, limits the number of bytes in each
|
3069
|
+
# line; see [Line Limit](rdoc-ref:IO@Line+Limit):
|
3070
|
+
#
|
3071
|
+
# f = File.new('t.txt')
|
3072
|
+
# f.each_line(8) {|line| p line }
|
3073
|
+
# f.close
|
3074
|
+
#
|
3075
|
+
# Output:
|
3076
|
+
#
|
3077
|
+
# "First li"
|
3078
|
+
# "ne\n"
|
3079
|
+
# "Second l"
|
3080
|
+
# "ine\n"
|
3081
|
+
# "\n"
|
3082
|
+
# "Fourth l"
|
3083
|
+
# "ine\n"
|
3084
|
+
# "Fifth li"
|
3085
|
+
# "ne\n"
|
3086
|
+
#
|
3087
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
3088
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
3089
|
+
#
|
3090
|
+
# Optional keyword argument `chomp` specifies whether line separators are to be
|
3091
|
+
# omitted:
|
3092
|
+
#
|
3093
|
+
# f = File.new('t.txt')
|
3094
|
+
# f.each_line(chomp: true) {|line| p line }
|
3095
|
+
# f.close
|
3096
|
+
#
|
3097
|
+
# Output:
|
3098
|
+
#
|
3099
|
+
# "First line"
|
3100
|
+
# "Second line"
|
3101
|
+
# ""
|
3102
|
+
# "Fourth line"
|
3103
|
+
# "Fifth line"
|
3104
|
+
#
|
3105
|
+
# Returns an Enumerator if no block is given.
|
3106
|
+
#
|
3107
|
+
def each_line: (?string sep, ?int limit, ?chomp: boolish) { (String line) -> void } -> self
|
3108
|
+
| (?string sep, ?int limit, ?chomp: boolish) -> ::Enumerator[String, self]
|
3109
|
+
|
3110
|
+
# <!--
|
3111
|
+
# rdoc-file=io.c
|
3112
|
+
# - each_line(sep = $/, chomp: false) {|line| ... } -> self
|
3113
|
+
# - each_line(limit, chomp: false) {|line| ... } -> self
|
3114
|
+
# - each_line(sep, limit, chomp: false) {|line| ... } -> self
|
3115
|
+
# - each_line -> enumerator
|
3116
|
+
# -->
|
3117
|
+
# Calls the block with each remaining line read from the stream; returns `self`.
|
3118
|
+
# Does nothing if already at end-of-stream; See [Line IO](rdoc-ref:IO@Line+IO).
|
3119
|
+
#
|
3120
|
+
# With no arguments given, reads lines as determined by line separator `$/`:
|
3121
|
+
#
|
3122
|
+
# f = File.new('t.txt')
|
3123
|
+
# f.each_line {|line| p line }
|
3124
|
+
# f.each_line {|line| fail 'Cannot happen' }
|
3125
|
+
# f.close
|
3126
|
+
#
|
3127
|
+
# Output:
|
3128
|
+
#
|
3129
|
+
# "First line\n"
|
3130
|
+
# "Second line\n"
|
3131
|
+
# "\n"
|
3132
|
+
# "Fourth line\n"
|
3133
|
+
# "Fifth line\n"
|
3134
|
+
#
|
3135
|
+
# With only string argument `sep` given, reads lines as determined by line
|
3136
|
+
# separator `sep`; see [Line Separator](rdoc-ref:IO@Line+Separator):
|
3137
|
+
#
|
3138
|
+
# f = File.new('t.txt')
|
3139
|
+
# f.each_line('li') {|line| p line }
|
3140
|
+
# f.close
|
3141
|
+
#
|
3142
|
+
# Output:
|
3143
|
+
#
|
3144
|
+
# "First li"
|
3145
|
+
# "ne\nSecond li"
|
3146
|
+
# "ne\n\nFourth li"
|
3147
|
+
# "ne\nFifth li"
|
3148
|
+
# "ne\n"
|
3149
|
+
#
|
3150
|
+
# The two special values for `sep` are honored:
|
3151
|
+
#
|
3152
|
+
# f = File.new('t.txt')
|
3153
|
+
# # Get all into one string.
|
3154
|
+
# f.each_line(nil) {|line| p line }
|
3155
|
+
# f.close
|
3156
|
+
#
|
3157
|
+
# Output:
|
3158
|
+
#
|
3159
|
+
# "First line\nSecond line\n\nFourth line\nFifth line\n"
|
3160
|
+
#
|
3161
|
+
# f.rewind
|
3162
|
+
# # Get paragraphs (up to two line separators).
|
3163
|
+
# f.each_line('') {|line| p line }
|
3164
|
+
#
|
3165
|
+
# Output:
|
3166
|
+
#
|
3167
|
+
# "First line\nSecond line\n\n"
|
3168
|
+
# "Fourth line\nFifth line\n"
|
3169
|
+
#
|
3170
|
+
# With only integer argument `limit` given, limits the number of bytes in each
|
3171
|
+
# line; see [Line Limit](rdoc-ref:IO@Line+Limit):
|
3172
|
+
#
|
3173
|
+
# f = File.new('t.txt')
|
3174
|
+
# f.each_line(8) {|line| p line }
|
3175
|
+
# f.close
|
3176
|
+
#
|
3177
|
+
# Output:
|
3178
|
+
#
|
3179
|
+
# "First li"
|
3180
|
+
# "ne\n"
|
3181
|
+
# "Second l"
|
3182
|
+
# "ine\n"
|
3183
|
+
# "\n"
|
3184
|
+
# "Fourth l"
|
3185
|
+
# "ine\n"
|
3186
|
+
# "Fifth li"
|
3187
|
+
# "ne\n"
|
3188
|
+
#
|
3189
|
+
# With arguments `sep` and `limit` given, combines the two behaviors (see [Line
|
3190
|
+
# Separator and Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)).
|
3191
|
+
#
|
3192
|
+
# Optional keyword argument `chomp` specifies whether line separators are to be
|
3193
|
+
# omitted:
|
3194
|
+
#
|
3195
|
+
# f = File.new('t.txt')
|
3196
|
+
# f.each_line(chomp: true) {|line| p line }
|
3197
|
+
# f.close
|
3198
|
+
#
|
3199
|
+
# Output:
|
3200
|
+
#
|
3201
|
+
# "First line"
|
3202
|
+
# "Second line"
|
3203
|
+
# ""
|
3204
|
+
# "Fourth line"
|
3205
|
+
# "Fifth line"
|
3206
|
+
#
|
3207
|
+
# Returns an Enumerator if no block is given.
|
3208
|
+
#
|
3209
|
+
alias each each_line
|
3210
|
+
|
3211
|
+
# <!-- rdoc-file=io.c -->
|
3212
|
+
# Returns `true` if the stream is positioned at its end, `false` otherwise; see
|
3213
|
+
# [Position](rdoc-ref:IO@Position):
|
3214
|
+
#
|
3215
|
+
# f = File.open('t.txt')
|
3216
|
+
# f.eof # => false
|
3217
|
+
# f.seek(0, :END) # => 0
|
3218
|
+
# f.eof # => true
|
3219
|
+
# f.close
|
3220
|
+
#
|
3221
|
+
# Raises an exception unless the stream is opened for reading; see
|
3222
|
+
# [Mode](rdoc-ref:File@Access+Modes).
|
3223
|
+
#
|
3224
|
+
# If `self` is a stream such as pipe or socket, this method blocks until the
|
3225
|
+
# other end sends some data or closes it:
|
3226
|
+
#
|
3227
|
+
# r, w = IO.pipe
|
3228
|
+
# Thread.new { sleep 1; w.close }
|
3229
|
+
# r.eof? # => true # After 1-second wait.
|
3230
|
+
#
|
3231
|
+
# r, w = IO.pipe
|
3232
|
+
# Thread.new { sleep 1; w.puts "a" }
|
3233
|
+
# r.eof? # => false # After 1-second wait.
|
3234
|
+
#
|
3235
|
+
# r, w = IO.pipe
|
3236
|
+
# r.eof? # blocks forever
|
3237
|
+
#
|
3238
|
+
# Note that this method reads data to the input byte buffer. So IO#sysread may
|
3239
|
+
# not behave as you intend with IO#eof?, unless you call IO#rewind first (which
|
3240
|
+
# is not available for some streams).
|
3241
|
+
#
|
3242
|
+
alias eof? eof
|
3243
|
+
|
3244
|
+
# <!-- rdoc-file=io.c -->
|
3245
|
+
# Returns the integer file descriptor for the stream:
|
3246
|
+
#
|
3247
|
+
# $stdin.fileno # => 0
|
3248
|
+
# $stdout.fileno # => 1
|
3249
|
+
# $stderr.fileno # => 2
|
3250
|
+
# File.open('t.txt').fileno # => 10
|
3251
|
+
# f.close
|
3252
|
+
#
|
3253
|
+
alias to_i fileno
|
3254
|
+
|
3255
|
+
interface _ForFd[RET]
|
3256
|
+
def for_fd: (?) -> RET
|
3257
|
+
end
|
3258
|
+
end
|
3259
|
+
|
3260
|
+
IO::APPEND: Integer
|
3261
|
+
|
3262
|
+
IO::BINARY: Integer
|
3263
|
+
|
3264
|
+
IO::CREAT: Integer
|
3265
|
+
|
3266
|
+
IO::DIRECT: Integer
|
3267
|
+
|
3268
|
+
IO::DSYNC: Integer
|
3269
|
+
|
3270
|
+
IO::EXCL: Integer
|
3271
|
+
|
3272
|
+
IO::FNM_CASEFOLD: Integer
|
3273
|
+
|
3274
|
+
IO::FNM_DOTMATCH: Integer
|
3275
|
+
|
3276
|
+
IO::FNM_EXTGLOB: Integer
|
3277
|
+
|
3278
|
+
IO::FNM_NOESCAPE: Integer
|
3279
|
+
|
3280
|
+
IO::FNM_PATHNAME: Integer
|
3281
|
+
|
3282
|
+
IO::FNM_SHORTNAME: Integer
|
3283
|
+
|
3284
|
+
IO::FNM_SYSCASE: Integer
|
3285
|
+
|
3286
|
+
IO::LOCK_EX: Integer
|
3287
|
+
|
3288
|
+
IO::LOCK_NB: Integer
|
3289
|
+
|
3290
|
+
IO::LOCK_SH: Integer
|
3291
|
+
|
3292
|
+
IO::LOCK_UN: Integer
|
3293
|
+
|
3294
|
+
IO::NOATIME: Integer
|
3295
|
+
|
3296
|
+
IO::NOCTTY: Integer
|
3297
|
+
|
3298
|
+
IO::NOFOLLOW: Integer
|
3299
|
+
|
3300
|
+
IO::NONBLOCK: Integer
|
3301
|
+
|
3302
|
+
IO::NULL: String
|
3303
|
+
|
3304
|
+
IO::RDONLY: Integer
|
3305
|
+
|
3306
|
+
IO::RDWR: Integer
|
3307
|
+
|
3308
|
+
IO::RSYNC: Integer
|
3309
|
+
|
3310
|
+
# <!-- rdoc-file=io.c -->
|
3311
|
+
# Set I/O position from the current position
|
3312
|
+
#
|
3313
|
+
IO::SEEK_CUR: Integer
|
3314
|
+
|
3315
|
+
# <!-- rdoc-file=io.c -->
|
3316
|
+
# Set I/O position to the next location containing data
|
3317
|
+
#
|
3318
|
+
IO::SEEK_DATA: Integer
|
3319
|
+
|
3320
|
+
# <!-- rdoc-file=io.c -->
|
3321
|
+
# Set I/O position from the end
|
3322
|
+
#
|
3323
|
+
IO::SEEK_END: Integer
|
3324
|
+
|
3325
|
+
# <!-- rdoc-file=io.c -->
|
3326
|
+
# Set I/O position to the next hole
|
3327
|
+
#
|
3328
|
+
IO::SEEK_HOLE: Integer
|
3329
|
+
|
3330
|
+
# <!-- rdoc-file=io.c -->
|
3331
|
+
# Set I/O position from the beginning
|
3332
|
+
#
|
3333
|
+
IO::SEEK_SET: Integer
|
3334
|
+
|
3335
|
+
IO::SHARE_DELETE: Integer
|
3336
|
+
|
3337
|
+
IO::SYNC: Integer
|
3338
|
+
|
3339
|
+
IO::TMPFILE: Integer
|
3340
|
+
|
3341
|
+
IO::TRUNC: Integer
|
3342
|
+
|
3343
|
+
IO::WRONLY: Integer
|
3344
|
+
|
3345
|
+
# <!-- rdoc-file=io.c -->
|
3346
|
+
# Readable event mask for IO#wait.
|
3347
|
+
#
|
3348
|
+
IO::READABLE: Integer
|
3349
|
+
|
3350
|
+
# <!-- rdoc-file=io.c -->
|
3351
|
+
# Writable event mask for IO#wait.
|
3352
|
+
#
|
3353
|
+
IO::WRITABLE: Integer
|
3354
|
+
|
3355
|
+
# <!-- rdoc-file=io.c -->
|
3356
|
+
# Priority event mask for IO#wait.
|
3357
|
+
#
|
3358
|
+
IO::PRIORITY: Integer
|
3359
|
+
|
3360
|
+
# <!-- rdoc-file=io.c -->
|
3361
|
+
# exception to wait for reading by EAGAIN. see IO.select.
|
3362
|
+
#
|
3363
|
+
class IO::EAGAINWaitReadable < Errno::EAGAIN
|
3364
|
+
include IO::WaitReadable
|
3365
|
+
end
|
3366
|
+
|
3367
|
+
IO::EAGAINWaitReadable::Errno: Integer
|
3368
|
+
|
3369
|
+
# <!-- rdoc-file=io.c -->
|
3370
|
+
# exception to wait for writing by EAGAIN. see IO.select.
|
3371
|
+
#
|
3372
|
+
class IO::EAGAINWaitWritable < Errno::EAGAIN
|
3373
|
+
include IO::WaitWritable
|
3374
|
+
end
|
3375
|
+
|
3376
|
+
IO::EAGAINWaitWritable::Errno: Integer
|
3377
|
+
|
3378
|
+
# <!-- rdoc-file=io.c -->
|
3379
|
+
# exception to wait for reading by EINPROGRESS. see IO.select.
|
3380
|
+
#
|
3381
|
+
class IO::EINPROGRESSWaitReadable < Errno::EINPROGRESS
|
3382
|
+
include IO::WaitReadable
|
3383
|
+
end
|
3384
|
+
|
3385
|
+
IO::EINPROGRESSWaitReadable::Errno: Integer
|
3386
|
+
|
3387
|
+
# <!-- rdoc-file=io.c -->
|
3388
|
+
# exception to wait for writing by EINPROGRESS. see IO.select.
|
3389
|
+
#
|
3390
|
+
class IO::EINPROGRESSWaitWritable < Errno::EINPROGRESS
|
3391
|
+
include IO::WaitWritable
|
3392
|
+
end
|
3393
|
+
|
3394
|
+
IO::EINPROGRESSWaitWritable::Errno: Integer
|
3395
|
+
|
3396
|
+
# <!-- rdoc-file=io.c -->
|
3397
|
+
# exception to wait for reading. see IO.select.
|
3398
|
+
#
|
3399
|
+
module IO::WaitReadable
|
3400
|
+
end
|
3401
|
+
|
3402
|
+
# <!-- rdoc-file=io.c -->
|
3403
|
+
# exception to wait for writing. see IO.select.
|
3404
|
+
#
|
3405
|
+
module IO::WaitWritable
|
3406
|
+
end
|