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
@@ -0,0 +1,3776 @@
|
|
1
|
+
# <!-- rdoc-file=lib/csv.rb -->
|
2
|
+
# ## CSV
|
3
|
+
#
|
4
|
+
# ### CSV Data
|
5
|
+
#
|
6
|
+
# CSV (comma-separated values) data is a text representation of a table:
|
7
|
+
# * A *row* *separator* delimits table rows. A common row separator is the
|
8
|
+
# newline character `"\n"`.
|
9
|
+
# * A *column* *separator* delimits fields in a row. A common column separator
|
10
|
+
# is the comma character `","`.
|
11
|
+
#
|
12
|
+
# This CSV String, with row separator `"\n"` and column separator `","`, has
|
13
|
+
# three rows and two columns:
|
14
|
+
# "foo,0\nbar,1\nbaz,2\n"
|
15
|
+
#
|
16
|
+
# Despite the name CSV, a CSV representation can use different separators.
|
17
|
+
#
|
18
|
+
# For more about tables, see the Wikipedia article "[Table
|
19
|
+
# (information)](https://en.wikipedia.org/wiki/Table_(information))", especially
|
20
|
+
# its section "[Simple
|
21
|
+
# table](https://en.wikipedia.org/wiki/Table_(information)#Simple_table)"
|
22
|
+
#
|
23
|
+
# ## Class CSV
|
24
|
+
#
|
25
|
+
# Class CSV provides methods for:
|
26
|
+
# * Parsing CSV data from a String object, a File (via its file path), or an
|
27
|
+
# IO object.
|
28
|
+
# * Generating CSV data to a String object.
|
29
|
+
#
|
30
|
+
# To make CSV available:
|
31
|
+
# require 'csv'
|
32
|
+
#
|
33
|
+
# All examples here assume that this has been done.
|
34
|
+
#
|
35
|
+
# ## Keeping It Simple
|
36
|
+
#
|
37
|
+
# A CSV object has dozens of instance methods that offer fine-grained control of
|
38
|
+
# parsing and generating CSV data. For many needs, though, simpler approaches
|
39
|
+
# will do.
|
40
|
+
#
|
41
|
+
# This section summarizes the singleton methods in CSV that allow you to parse
|
42
|
+
# and generate without explicitly creating CSV objects. For details, follow the
|
43
|
+
# links.
|
44
|
+
#
|
45
|
+
# ### Simple Parsing
|
46
|
+
#
|
47
|
+
# Parsing methods commonly return either of:
|
48
|
+
# * An Array of Arrays of Strings:
|
49
|
+
# * The outer Array is the entire "table".
|
50
|
+
# * Each inner Array is a row.
|
51
|
+
# * Each String is a field.
|
52
|
+
# * A CSV::Table object. For details, see [\CSV with
|
53
|
+
# Headers](#class-CSV-label-CSV+with+Headers).
|
54
|
+
#
|
55
|
+
# #### Parsing a String
|
56
|
+
#
|
57
|
+
# The input to be parsed can be a string:
|
58
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
59
|
+
#
|
60
|
+
# Method CSV.parse returns the entire CSV data:
|
61
|
+
# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
62
|
+
#
|
63
|
+
# Method CSV.parse_line returns only the first row:
|
64
|
+
# CSV.parse_line(string) # => ["foo", "0"]
|
65
|
+
#
|
66
|
+
# CSV extends class String with instance method String#parse_csv, which also
|
67
|
+
# returns only the first row:
|
68
|
+
# string.parse_csv # => ["foo", "0"]
|
69
|
+
#
|
70
|
+
# #### Parsing Via a File Path
|
71
|
+
#
|
72
|
+
# The input to be parsed can be in a file:
|
73
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
74
|
+
# path = 't.csv'
|
75
|
+
# File.write(path, string)
|
76
|
+
#
|
77
|
+
# Method CSV.read returns the entire CSV data:
|
78
|
+
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
79
|
+
#
|
80
|
+
# Method CSV.foreach iterates, passing each row to the given block:
|
81
|
+
# CSV.foreach(path) do |row|
|
82
|
+
# p row
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# Output:
|
86
|
+
# ["foo", "0"]
|
87
|
+
# ["bar", "1"]
|
88
|
+
# ["baz", "2"]
|
89
|
+
#
|
90
|
+
# Method CSV.table returns the entire CSV data as a CSV::Table object:
|
91
|
+
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3>
|
92
|
+
#
|
93
|
+
# #### Parsing from an Open IO Stream
|
94
|
+
#
|
95
|
+
# The input to be parsed can be in an open IO stream:
|
96
|
+
#
|
97
|
+
# Method CSV.read returns the entire CSV data:
|
98
|
+
# File.open(path) do |file|
|
99
|
+
# CSV.read(file)
|
100
|
+
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
101
|
+
#
|
102
|
+
# As does method CSV.parse:
|
103
|
+
# File.open(path) do |file|
|
104
|
+
# CSV.parse(file)
|
105
|
+
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
106
|
+
#
|
107
|
+
# Method CSV.parse_line returns only the first row:
|
108
|
+
# File.open(path) do |file|
|
109
|
+
# CSV.parse_line(file)
|
110
|
+
# end # => ["foo", "0"]
|
111
|
+
#
|
112
|
+
# Method CSV.foreach iterates, passing each row to the given block:
|
113
|
+
# File.open(path) do |file|
|
114
|
+
# CSV.foreach(file) do |row|
|
115
|
+
# p row
|
116
|
+
# end
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# Output:
|
120
|
+
# ["foo", "0"]
|
121
|
+
# ["bar", "1"]
|
122
|
+
# ["baz", "2"]
|
123
|
+
#
|
124
|
+
# Method CSV.table returns the entire CSV data as a CSV::Table object:
|
125
|
+
# File.open(path) do |file|
|
126
|
+
# CSV.table(file)
|
127
|
+
# end # => #<CSV::Table mode:col_or_row row_count:3>
|
128
|
+
#
|
129
|
+
# ### Simple Generating
|
130
|
+
#
|
131
|
+
# Method CSV.generate returns a String; this example uses method CSV#<< to
|
132
|
+
# append the rows that are to be generated:
|
133
|
+
# output_string = CSV.generate do |csv|
|
134
|
+
# csv << ['foo', 0]
|
135
|
+
# csv << ['bar', 1]
|
136
|
+
# csv << ['baz', 2]
|
137
|
+
# end
|
138
|
+
# output_string # => "foo,0\nbar,1\nbaz,2\n"
|
139
|
+
#
|
140
|
+
# Method CSV.generate_line returns a String containing the single row
|
141
|
+
# constructed from an Array:
|
142
|
+
# CSV.generate_line(['foo', '0']) # => "foo,0\n"
|
143
|
+
#
|
144
|
+
# CSV extends class Array with instance method `Array#to_csv`, which forms an
|
145
|
+
# Array into a String:
|
146
|
+
# ['foo', '0'].to_csv # => "foo,0\n"
|
147
|
+
#
|
148
|
+
# ### "Filtering" CSV
|
149
|
+
#
|
150
|
+
# Method CSV.filter provides a Unix-style filter for CSV data. The input data is
|
151
|
+
# processed to form the output data:
|
152
|
+
# in_string = "foo,0\nbar,1\nbaz,2\n"
|
153
|
+
# out_string = ''
|
154
|
+
# CSV.filter(in_string, out_string) do |row|
|
155
|
+
# row[0] = row[0].upcase
|
156
|
+
# row[1] *= 4
|
157
|
+
# end
|
158
|
+
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
|
159
|
+
#
|
160
|
+
# ## CSV Objects
|
161
|
+
#
|
162
|
+
# There are three ways to create a CSV object:
|
163
|
+
# * Method CSV.new returns a new CSV object.
|
164
|
+
# * Method CSV.instance returns a new or cached CSV object.
|
165
|
+
# * Method CSV() also returns a new or cached CSV object.
|
166
|
+
#
|
167
|
+
# ### Instance Methods
|
168
|
+
#
|
169
|
+
# CSV has three groups of instance methods:
|
170
|
+
# * Its own internally defined instance methods.
|
171
|
+
# * Methods included by module Enumerable.
|
172
|
+
# * Methods delegated to class IO. See below.
|
173
|
+
#
|
174
|
+
# #### Delegated Methods
|
175
|
+
#
|
176
|
+
# For convenience, a CSV object will delegate to many methods in class IO. (A
|
177
|
+
# few have wrapper "guard code" in CSV.) You may call:
|
178
|
+
# * IO#binmode
|
179
|
+
# * #binmode?
|
180
|
+
# * IO#close
|
181
|
+
# * IO#close_read
|
182
|
+
# * IO#close_write
|
183
|
+
# * IO#closed?
|
184
|
+
# * #eof
|
185
|
+
# * #eof?
|
186
|
+
# * IO#external_encoding
|
187
|
+
# * IO#fcntl
|
188
|
+
# * IO#fileno
|
189
|
+
# * #flock
|
190
|
+
# * IO#flush
|
191
|
+
# * IO#fsync
|
192
|
+
# * IO#internal_encoding
|
193
|
+
# * #ioctl
|
194
|
+
# * IO#isatty
|
195
|
+
# * #path
|
196
|
+
# * IO#pid
|
197
|
+
# * IO#pos
|
198
|
+
# * IO#pos=
|
199
|
+
# * IO#reopen
|
200
|
+
# * #rewind
|
201
|
+
# * IO#seek
|
202
|
+
# * #stat
|
203
|
+
# * IO#string
|
204
|
+
# * IO#sync
|
205
|
+
# * IO#sync=
|
206
|
+
# * IO#tell
|
207
|
+
# * #to_i
|
208
|
+
# * #to_io
|
209
|
+
# * IO#truncate
|
210
|
+
# * IO#tty?
|
211
|
+
#
|
212
|
+
# ### Options
|
213
|
+
#
|
214
|
+
# The default values for options are:
|
215
|
+
# DEFAULT_OPTIONS = {
|
216
|
+
# # For both parsing and generating.
|
217
|
+
# col_sep: ",",
|
218
|
+
# row_sep: :auto,
|
219
|
+
# quote_char: '"',
|
220
|
+
# # For parsing.
|
221
|
+
# field_size_limit: nil,
|
222
|
+
# converters: nil,
|
223
|
+
# unconverted_fields: nil,
|
224
|
+
# headers: false,
|
225
|
+
# return_headers: false,
|
226
|
+
# header_converters: nil,
|
227
|
+
# skip_blanks: false,
|
228
|
+
# skip_lines: nil,
|
229
|
+
# liberal_parsing: false,
|
230
|
+
# nil_value: nil,
|
231
|
+
# empty_value: "",
|
232
|
+
# strip: false,
|
233
|
+
# # For generating.
|
234
|
+
# write_headers: nil,
|
235
|
+
# quote_empty: true,
|
236
|
+
# force_quotes: false,
|
237
|
+
# write_converters: nil,
|
238
|
+
# write_nil_value: nil,
|
239
|
+
# write_empty_value: "",
|
240
|
+
# }
|
241
|
+
#
|
242
|
+
# #### Options for Parsing
|
243
|
+
#
|
244
|
+
# Options for parsing, described in detail below, include:
|
245
|
+
# * `row_sep`: Specifies the row separator; used to delimit rows.
|
246
|
+
# * `col_sep`: Specifies the column separator; used to delimit fields.
|
247
|
+
# * `quote_char`: Specifies the quote character; used to quote fields.
|
248
|
+
# * `field_size_limit`: Specifies the maximum field size + 1 allowed.
|
249
|
+
# Deprecated since 3.2.3. Use `max_field_size` instead.
|
250
|
+
# * `max_field_size`: Specifies the maximum field size allowed.
|
251
|
+
# * `converters`: Specifies the field converters to be used.
|
252
|
+
# * `unconverted_fields`: Specifies whether unconverted fields are to be
|
253
|
+
# available.
|
254
|
+
# * `headers`: Specifies whether data contains headers, or specifies the
|
255
|
+
# headers themselves.
|
256
|
+
# * `return_headers`: Specifies whether headers are to be returned.
|
257
|
+
# * `header_converters`: Specifies the header converters to be used.
|
258
|
+
# * `skip_blanks`: Specifies whether blanks lines are to be ignored.
|
259
|
+
# * `skip_lines`: Specifies how comments lines are to be recognized.
|
260
|
+
# * `strip`: Specifies whether leading and trailing whitespace are to be
|
261
|
+
# stripped from fields. This must be compatible with `col_sep`; if it is
|
262
|
+
# not, then an `ArgumentError` exception will be raised.
|
263
|
+
# * `liberal_parsing`: Specifies whether CSV should attempt to parse
|
264
|
+
# non-compliant data.
|
265
|
+
# * `nil_value`: Specifies the object that is to be substituted for each null
|
266
|
+
# (no-text) field.
|
267
|
+
# * `empty_value`: Specifies the object that is to be substituted for each
|
268
|
+
# empty field.
|
269
|
+
#
|
270
|
+
# ###### Option `row_sep`
|
271
|
+
#
|
272
|
+
# Specifies the row separator, a String or the Symbol `:auto` (see below), to be
|
273
|
+
# used for both parsing and generating.
|
274
|
+
#
|
275
|
+
# Default value:
|
276
|
+
# CSV::DEFAULT_OPTIONS.fetch(:row_sep) # => :auto
|
277
|
+
#
|
278
|
+
# ---
|
279
|
+
#
|
280
|
+
# When `row_sep` is a String, that String becomes the row separator. The String
|
281
|
+
# will be transcoded into the data's Encoding before use.
|
282
|
+
#
|
283
|
+
# Using `"\n"`:
|
284
|
+
# row_sep = "\n"
|
285
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
286
|
+
# csv << [:foo, 0]
|
287
|
+
# csv << [:bar, 1]
|
288
|
+
# csv << [:baz, 2]
|
289
|
+
# end
|
290
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
291
|
+
# ary = CSV.parse(str)
|
292
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
293
|
+
#
|
294
|
+
# Using `|` (pipe):
|
295
|
+
# row_sep = '|'
|
296
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
297
|
+
# csv << [:foo, 0]
|
298
|
+
# csv << [:bar, 1]
|
299
|
+
# csv << [:baz, 2]
|
300
|
+
# end
|
301
|
+
# str # => "foo,0|bar,1|baz,2|"
|
302
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
303
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
304
|
+
#
|
305
|
+
# Using `--` (two hyphens):
|
306
|
+
# row_sep = '--'
|
307
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
308
|
+
# csv << [:foo, 0]
|
309
|
+
# csv << [:bar, 1]
|
310
|
+
# csv << [:baz, 2]
|
311
|
+
# end
|
312
|
+
# str # => "foo,0--bar,1--baz,2--"
|
313
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
314
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
315
|
+
#
|
316
|
+
# Using `''` (empty string):
|
317
|
+
# row_sep = ''
|
318
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
319
|
+
# csv << [:foo, 0]
|
320
|
+
# csv << [:bar, 1]
|
321
|
+
# csv << [:baz, 2]
|
322
|
+
# end
|
323
|
+
# str # => "foo,0bar,1baz,2"
|
324
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
325
|
+
# ary # => [["foo", "0bar", "1baz", "2"]]
|
326
|
+
#
|
327
|
+
# ---
|
328
|
+
#
|
329
|
+
# When `row_sep` is the Symbol `:auto` (the default), generating uses `"\n"` as
|
330
|
+
# the row separator:
|
331
|
+
# str = CSV.generate do |csv|
|
332
|
+
# csv << [:foo, 0]
|
333
|
+
# csv << [:bar, 1]
|
334
|
+
# csv << [:baz, 2]
|
335
|
+
# end
|
336
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
337
|
+
#
|
338
|
+
# Parsing, on the other hand, invokes auto-discovery of the row separator.
|
339
|
+
#
|
340
|
+
# Auto-discovery reads ahead in the data looking for the next `\r\n`, `\n`, or
|
341
|
+
# `\r` sequence. The sequence will be selected even if it occurs in a quoted
|
342
|
+
# field, assuming that you would have the same line endings there.
|
343
|
+
#
|
344
|
+
# Example:
|
345
|
+
# str = CSV.generate do |csv|
|
346
|
+
# csv << [:foo, 0]
|
347
|
+
# csv << [:bar, 1]
|
348
|
+
# csv << [:baz, 2]
|
349
|
+
# end
|
350
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
351
|
+
# ary = CSV.parse(str)
|
352
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
353
|
+
#
|
354
|
+
# The default `$INPUT_RECORD_SEPARATOR` (`$/`) is used if any of the following
|
355
|
+
# is true:
|
356
|
+
# * None of those sequences is found.
|
357
|
+
# * Data is `ARGF`, `STDIN`, `STDOUT`, or `STDERR`.
|
358
|
+
# * The stream is only available for output.
|
359
|
+
#
|
360
|
+
# Obviously, discovery takes a little time. Set manually if speed is important.
|
361
|
+
# Also note that IO objects should be opened in binary mode on Windows if this
|
362
|
+
# feature will be used as the line-ending translation can cause problems with
|
363
|
+
# resetting the document position to where it was before the read ahead.
|
364
|
+
#
|
365
|
+
# ###### Option `col_sep`
|
366
|
+
#
|
367
|
+
# Specifies the String field separator to be used for both parsing and
|
368
|
+
# generating. The String will be transcoded into the data's Encoding before use.
|
369
|
+
#
|
370
|
+
# Default value:
|
371
|
+
# CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
|
372
|
+
#
|
373
|
+
# Using the default (comma):
|
374
|
+
# str = CSV.generate do |csv|
|
375
|
+
# csv << [:foo, 0]
|
376
|
+
# csv << [:bar, 1]
|
377
|
+
# csv << [:baz, 2]
|
378
|
+
# end
|
379
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
380
|
+
# ary = CSV.parse(str)
|
381
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
382
|
+
#
|
383
|
+
# Using `:` (colon):
|
384
|
+
# col_sep = ':'
|
385
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
386
|
+
# csv << [:foo, 0]
|
387
|
+
# csv << [:bar, 1]
|
388
|
+
# csv << [:baz, 2]
|
389
|
+
# end
|
390
|
+
# str # => "foo:0\nbar:1\nbaz:2\n"
|
391
|
+
# ary = CSV.parse(str, col_sep: col_sep)
|
392
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
393
|
+
#
|
394
|
+
# Using `::` (two colons):
|
395
|
+
# col_sep = '::'
|
396
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
397
|
+
# csv << [:foo, 0]
|
398
|
+
# csv << [:bar, 1]
|
399
|
+
# csv << [:baz, 2]
|
400
|
+
# end
|
401
|
+
# str # => "foo::0\nbar::1\nbaz::2\n"
|
402
|
+
# ary = CSV.parse(str, col_sep: col_sep)
|
403
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
404
|
+
#
|
405
|
+
# Using `''` (empty string):
|
406
|
+
# col_sep = ''
|
407
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
408
|
+
# csv << [:foo, 0]
|
409
|
+
# csv << [:bar, 1]
|
410
|
+
# csv << [:baz, 2]
|
411
|
+
# end
|
412
|
+
# str # => "foo0\nbar1\nbaz2\n"
|
413
|
+
#
|
414
|
+
# ---
|
415
|
+
#
|
416
|
+
# Raises an exception if parsing with the empty String:
|
417
|
+
# col_sep = ''
|
418
|
+
# # Raises ArgumentError (:col_sep must be 1 or more characters: "")
|
419
|
+
# CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
|
420
|
+
#
|
421
|
+
# ###### Option `quote_char`
|
422
|
+
#
|
423
|
+
# Specifies the character (String of length 1) used used to quote fields in both
|
424
|
+
# parsing and generating. This String will be transcoded into the data's
|
425
|
+
# Encoding before use.
|
426
|
+
#
|
427
|
+
# Default value:
|
428
|
+
# CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote)
|
429
|
+
#
|
430
|
+
# This is useful for an application that incorrectly uses `'` (single-quote) to
|
431
|
+
# quote fields, instead of the correct `"` (double-quote).
|
432
|
+
#
|
433
|
+
# Using the default (double quote):
|
434
|
+
# str = CSV.generate do |csv|
|
435
|
+
# csv << ['foo', 0]
|
436
|
+
# csv << ["'bar'", 1]
|
437
|
+
# csv << ['"baz"', 2]
|
438
|
+
# end
|
439
|
+
# str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n"
|
440
|
+
# ary = CSV.parse(str)
|
441
|
+
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
442
|
+
#
|
443
|
+
# Using `'` (single-quote):
|
444
|
+
# quote_char = "'"
|
445
|
+
# str = CSV.generate(quote_char: quote_char) do |csv|
|
446
|
+
# csv << ['foo', 0]
|
447
|
+
# csv << ["'bar'", 1]
|
448
|
+
# csv << ['"baz"', 2]
|
449
|
+
# end
|
450
|
+
# str # => "foo,0\n'''bar''',1\n\"baz\",2\n"
|
451
|
+
# ary = CSV.parse(str, quote_char: quote_char)
|
452
|
+
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
453
|
+
#
|
454
|
+
# ---
|
455
|
+
#
|
456
|
+
# Raises an exception if the String length is greater than 1:
|
457
|
+
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
|
458
|
+
# CSV.new('', quote_char: 'xx')
|
459
|
+
#
|
460
|
+
# Raises an exception if the value is not a String:
|
461
|
+
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
|
462
|
+
# CSV.new('', quote_char: :foo)
|
463
|
+
#
|
464
|
+
# ###### Option `field_size_limit`
|
465
|
+
#
|
466
|
+
# Specifies the Integer field size limit.
|
467
|
+
#
|
468
|
+
# Default value:
|
469
|
+
# CSV::DEFAULT_OPTIONS.fetch(:field_size_limit) # => nil
|
470
|
+
#
|
471
|
+
# This is a maximum size CSV will read ahead looking for the closing quote for a
|
472
|
+
# field. (In truth, it reads to the first line ending beyond this size.) If a
|
473
|
+
# quote cannot be found within the limit CSV will raise a MalformedCSVError,
|
474
|
+
# assuming the data is faulty. You can use this limit to prevent what are
|
475
|
+
# effectively DoS attacks on the parser. However, this limit can cause a
|
476
|
+
# legitimate parse to fail; therefore the default value is `nil` (no limit).
|
477
|
+
#
|
478
|
+
# For the examples in this section:
|
479
|
+
# str = <<~EOT
|
480
|
+
# "a","b"
|
481
|
+
# "
|
482
|
+
# 2345
|
483
|
+
# ",""
|
484
|
+
# EOT
|
485
|
+
# str # => "\"a\",\"b\"\n\"\n2345\n\",\"\"\n"
|
486
|
+
#
|
487
|
+
# Using the default `nil`:
|
488
|
+
# ary = CSV.parse(str)
|
489
|
+
# ary # => [["a", "b"], ["\n2345\n", ""]]
|
490
|
+
#
|
491
|
+
# Using `50`:
|
492
|
+
# field_size_limit = 50
|
493
|
+
# ary = CSV.parse(str, field_size_limit: field_size_limit)
|
494
|
+
# ary # => [["a", "b"], ["\n2345\n", ""]]
|
495
|
+
#
|
496
|
+
# ---
|
497
|
+
#
|
498
|
+
# Raises an exception if a field is too long:
|
499
|
+
# big_str = "123456789\n" * 1024
|
500
|
+
# # Raises CSV::MalformedCSVError (Field size exceeded in line 1.)
|
501
|
+
# CSV.parse('valid,fields,"' + big_str + '"', field_size_limit: 2048)
|
502
|
+
#
|
503
|
+
# ###### Option `converters`
|
504
|
+
#
|
505
|
+
# Specifies converters to be used in parsing fields. See [Field
|
506
|
+
# Converters](#class-CSV-label-Field+Converters)
|
507
|
+
#
|
508
|
+
# Default value:
|
509
|
+
# CSV::DEFAULT_OPTIONS.fetch(:converters) # => nil
|
510
|
+
#
|
511
|
+
# The value may be a field converter name (see [Stored
|
512
|
+
# Converters](#class-CSV-label-Stored+Converters)):
|
513
|
+
# str = '1,2,3'
|
514
|
+
# # Without a converter
|
515
|
+
# array = CSV.parse_line(str)
|
516
|
+
# array # => ["1", "2", "3"]
|
517
|
+
# # With built-in converter :integer
|
518
|
+
# array = CSV.parse_line(str, converters: :integer)
|
519
|
+
# array # => [1, 2, 3]
|
520
|
+
#
|
521
|
+
# The value may be a converter list (see [Converter
|
522
|
+
# Lists](#class-CSV-label-Converter+Lists)):
|
523
|
+
# str = '1,3.14159'
|
524
|
+
# # Without converters
|
525
|
+
# array = CSV.parse_line(str)
|
526
|
+
# array # => ["1", "3.14159"]
|
527
|
+
# # With built-in converters
|
528
|
+
# array = CSV.parse_line(str, converters: [:integer, :float])
|
529
|
+
# array # => [1, 3.14159]
|
530
|
+
#
|
531
|
+
# The value may be a Proc custom converter: (see [Custom Field
|
532
|
+
# Converters](#class-CSV-label-Custom+Field+Converters)):
|
533
|
+
# str = ' foo , bar , baz '
|
534
|
+
# # Without a converter
|
535
|
+
# array = CSV.parse_line(str)
|
536
|
+
# array # => [" foo ", " bar ", " baz "]
|
537
|
+
# # With a custom converter
|
538
|
+
# array = CSV.parse_line(str, converters: proc {|field| field.strip })
|
539
|
+
# array # => ["foo", "bar", "baz"]
|
540
|
+
#
|
541
|
+
# See also [Custom Field Converters](#class-CSV-label-Custom+Field+Converters)
|
542
|
+
#
|
543
|
+
# ---
|
544
|
+
#
|
545
|
+
# Raises an exception if the converter is not a converter name or a Proc:
|
546
|
+
# str = 'foo,0'
|
547
|
+
# # Raises NoMethodError (undefined method `arity' for nil:NilClass)
|
548
|
+
# CSV.parse(str, converters: :foo)
|
549
|
+
#
|
550
|
+
# ###### Option `unconverted_fields`
|
551
|
+
#
|
552
|
+
# Specifies the boolean that determines whether unconverted field values are to
|
553
|
+
# be available.
|
554
|
+
#
|
555
|
+
# Default value:
|
556
|
+
# CSV::DEFAULT_OPTIONS.fetch(:unconverted_fields) # => nil
|
557
|
+
#
|
558
|
+
# The unconverted field values are those found in the source data, prior to any
|
559
|
+
# conversions performed via option `converters`.
|
560
|
+
#
|
561
|
+
# When option `unconverted_fields` is `true`, each returned row (Array or
|
562
|
+
# CSV::Row) has an added method, `unconverted_fields`, that returns the
|
563
|
+
# unconverted field values:
|
564
|
+
# str = <<-EOT
|
565
|
+
# foo,0
|
566
|
+
# bar,1
|
567
|
+
# baz,2
|
568
|
+
# EOT
|
569
|
+
# # Without unconverted_fields
|
570
|
+
# csv = CSV.parse(str, converters: :integer)
|
571
|
+
# csv # => [["foo", 0], ["bar", 1], ["baz", 2]]
|
572
|
+
# csv.first.respond_to?(:unconverted_fields) # => false
|
573
|
+
# # With unconverted_fields
|
574
|
+
# csv = CSV.parse(str, converters: :integer, unconverted_fields: true)
|
575
|
+
# csv # => [["foo", 0], ["bar", 1], ["baz", 2]]
|
576
|
+
# csv.first.respond_to?(:unconverted_fields) # => true
|
577
|
+
# csv.first.unconverted_fields # => ["foo", "0"]
|
578
|
+
#
|
579
|
+
# ###### Option `headers`
|
580
|
+
#
|
581
|
+
# Specifies a boolean, Symbol, Array, or String to be used to define column
|
582
|
+
# headers.
|
583
|
+
#
|
584
|
+
# Default value:
|
585
|
+
# CSV::DEFAULT_OPTIONS.fetch(:headers) # => false
|
586
|
+
#
|
587
|
+
# ---
|
588
|
+
#
|
589
|
+
# Without `headers`:
|
590
|
+
# str = <<-EOT
|
591
|
+
# Name,Count
|
592
|
+
# foo,0
|
593
|
+
# bar,1
|
594
|
+
# bax,2
|
595
|
+
# EOT
|
596
|
+
# csv = CSV.new(str)
|
597
|
+
# csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
|
598
|
+
# csv.headers # => nil
|
599
|
+
# csv.shift # => ["Name", "Count"]
|
600
|
+
#
|
601
|
+
# ---
|
602
|
+
#
|
603
|
+
# If set to `true` or the Symbol `:first_row`, the first row of the data is
|
604
|
+
# treated as a row of headers:
|
605
|
+
# str = <<-EOT
|
606
|
+
# Name,Count
|
607
|
+
# foo,0
|
608
|
+
# bar,1
|
609
|
+
# bax,2
|
610
|
+
# EOT
|
611
|
+
# csv = CSV.new(str, headers: true)
|
612
|
+
# csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\n" quote_char:"\"" headers:["Name", "Count"]>
|
613
|
+
# csv.headers # => ["Name", "Count"]
|
614
|
+
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
|
615
|
+
#
|
616
|
+
# ---
|
617
|
+
#
|
618
|
+
# If set to an Array, the Array elements are treated as headers:
|
619
|
+
# str = <<-EOT
|
620
|
+
# foo,0
|
621
|
+
# bar,1
|
622
|
+
# bax,2
|
623
|
+
# EOT
|
624
|
+
# csv = CSV.new(str, headers: ['Name', 'Count'])
|
625
|
+
# csv
|
626
|
+
# csv.headers # => ["Name", "Count"]
|
627
|
+
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
|
628
|
+
#
|
629
|
+
# ---
|
630
|
+
#
|
631
|
+
# If set to a String `str`, method `CSV::parse_line(str, options)` is called
|
632
|
+
# with the current `options`, and the returned Array is treated as headers:
|
633
|
+
# str = <<-EOT
|
634
|
+
# foo,0
|
635
|
+
# bar,1
|
636
|
+
# bax,2
|
637
|
+
# EOT
|
638
|
+
# csv = CSV.new(str, headers: 'Name,Count')
|
639
|
+
# csv
|
640
|
+
# csv.headers # => ["Name", "Count"]
|
641
|
+
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
|
642
|
+
#
|
643
|
+
# ###### Option `return_headers`
|
644
|
+
#
|
645
|
+
# Specifies the boolean that determines whether method #shift returns or ignores
|
646
|
+
# the header row.
|
647
|
+
#
|
648
|
+
# Default value:
|
649
|
+
# CSV::DEFAULT_OPTIONS.fetch(:return_headers) # => false
|
650
|
+
#
|
651
|
+
# Examples:
|
652
|
+
# str = <<-EOT
|
653
|
+
# Name,Count
|
654
|
+
# foo,0
|
655
|
+
# bar,1
|
656
|
+
# bax,2
|
657
|
+
# EOT
|
658
|
+
# # Without return_headers first row is str.
|
659
|
+
# csv = CSV.new(str, headers: true)
|
660
|
+
# csv.shift # => #<CSV::Row "Name":"foo" "Count":"0">
|
661
|
+
# # With return_headers first row is headers.
|
662
|
+
# csv = CSV.new(str, headers: true, return_headers: true)
|
663
|
+
# csv.shift # => #<CSV::Row "Name":"Name" "Count":"Count">
|
664
|
+
#
|
665
|
+
# ###### Option `header_converters`
|
666
|
+
#
|
667
|
+
# Specifies converters to be used in parsing headers. See [Header
|
668
|
+
# Converters](#class-CSV-label-Header+Converters)
|
669
|
+
#
|
670
|
+
# Default value:
|
671
|
+
# CSV::DEFAULT_OPTIONS.fetch(:header_converters) # => nil
|
672
|
+
#
|
673
|
+
# Identical in functionality to option
|
674
|
+
# [converters](#class-CSV-label-Option+converters) except that:
|
675
|
+
# * The converters apply only to the header row.
|
676
|
+
# * The built-in header converters are `:downcase` and `:symbol`.
|
677
|
+
#
|
678
|
+
# This section assumes prior execution of:
|
679
|
+
# str = <<-EOT
|
680
|
+
# Name,Value
|
681
|
+
# foo,0
|
682
|
+
# bar,1
|
683
|
+
# baz,2
|
684
|
+
# EOT
|
685
|
+
# # With no header converter
|
686
|
+
# table = CSV.parse(str, headers: true)
|
687
|
+
# table.headers # => ["Name", "Value"]
|
688
|
+
#
|
689
|
+
# The value may be a header converter name (see [Stored
|
690
|
+
# Converters](#class-CSV-label-Stored+Converters)):
|
691
|
+
# table = CSV.parse(str, headers: true, header_converters: :downcase)
|
692
|
+
# table.headers # => ["name", "value"]
|
693
|
+
#
|
694
|
+
# The value may be a converter list (see [Converter
|
695
|
+
# Lists](#class-CSV-label-Converter+Lists)):
|
696
|
+
# header_converters = [:downcase, :symbol]
|
697
|
+
# table = CSV.parse(str, headers: true, header_converters: header_converters)
|
698
|
+
# table.headers # => [:name, :value]
|
699
|
+
#
|
700
|
+
# The value may be a Proc custom converter (see [Custom Header
|
701
|
+
# Converters](#class-CSV-label-Custom+Header+Converters)):
|
702
|
+
# upcase_converter = proc {|field| field.upcase }
|
703
|
+
# table = CSV.parse(str, headers: true, header_converters: upcase_converter)
|
704
|
+
# table.headers # => ["NAME", "VALUE"]
|
705
|
+
#
|
706
|
+
# See also [Custom Header Converters](#class-CSV-label-Custom+Header+Converters)
|
707
|
+
#
|
708
|
+
# ###### Option `skip_blanks`
|
709
|
+
#
|
710
|
+
# Specifies a boolean that determines whether blank lines in the input will be
|
711
|
+
# ignored; a line that contains a column separator is not considered to be
|
712
|
+
# blank.
|
713
|
+
#
|
714
|
+
# Default value:
|
715
|
+
# CSV::DEFAULT_OPTIONS.fetch(:skip_blanks) # => false
|
716
|
+
#
|
717
|
+
# See also option [skiplines](#class-CSV-label-Option+skip_lines).
|
718
|
+
#
|
719
|
+
# For examples in this section:
|
720
|
+
# str = <<-EOT
|
721
|
+
# foo,0
|
722
|
+
#
|
723
|
+
# bar,1
|
724
|
+
# baz,2
|
725
|
+
#
|
726
|
+
# ,
|
727
|
+
# EOT
|
728
|
+
#
|
729
|
+
# Using the default, `false`:
|
730
|
+
# ary = CSV.parse(str)
|
731
|
+
# ary # => [["foo", "0"], [], ["bar", "1"], ["baz", "2"], [], [nil, nil]]
|
732
|
+
#
|
733
|
+
# Using `true`:
|
734
|
+
# ary = CSV.parse(str, skip_blanks: true)
|
735
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"], [nil, nil]]
|
736
|
+
#
|
737
|
+
# Using a truthy value:
|
738
|
+
# ary = CSV.parse(str, skip_blanks: :foo)
|
739
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"], [nil, nil]]
|
740
|
+
#
|
741
|
+
# ###### Option `skip_lines`
|
742
|
+
#
|
743
|
+
# Specifies an object to use in identifying comment lines in the input that are
|
744
|
+
# to be ignored:
|
745
|
+
# * If a Regexp, ignores lines that match it.
|
746
|
+
# * If a String, converts it to a Regexp, ignores lines that match it.
|
747
|
+
# * If `nil`, no lines are considered to be comments.
|
748
|
+
#
|
749
|
+
# Default value:
|
750
|
+
# CSV::DEFAULT_OPTIONS.fetch(:skip_lines) # => nil
|
751
|
+
#
|
752
|
+
# For examples in this section:
|
753
|
+
# str = <<-EOT
|
754
|
+
# # Comment
|
755
|
+
# foo,0
|
756
|
+
# bar,1
|
757
|
+
# baz,2
|
758
|
+
# # Another comment
|
759
|
+
# EOT
|
760
|
+
# str # => "# Comment\nfoo,0\nbar,1\nbaz,2\n# Another comment\n"
|
761
|
+
#
|
762
|
+
# Using the default, `nil`:
|
763
|
+
# ary = CSV.parse(str)
|
764
|
+
# ary # => [["# Comment"], ["foo", "0"], ["bar", "1"], ["baz", "2"], ["# Another comment"]]
|
765
|
+
#
|
766
|
+
# Using a Regexp:
|
767
|
+
# ary = CSV.parse(str, skip_lines: /^#/)
|
768
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
769
|
+
#
|
770
|
+
# Using a String:
|
771
|
+
# ary = CSV.parse(str, skip_lines: '#')
|
772
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
773
|
+
#
|
774
|
+
# ---
|
775
|
+
#
|
776
|
+
# Raises an exception if given an object that is not a Regexp, a String, or
|
777
|
+
# `nil`:
|
778
|
+
# # Raises ArgumentError (:skip_lines has to respond to #match: 0)
|
779
|
+
# CSV.parse(str, skip_lines: 0)
|
780
|
+
#
|
781
|
+
# ###### Option `strip`
|
782
|
+
#
|
783
|
+
# Specifies the boolean value that determines whether whitespace is stripped
|
784
|
+
# from each input field.
|
785
|
+
#
|
786
|
+
# Default value:
|
787
|
+
# CSV::DEFAULT_OPTIONS.fetch(:strip) # => false
|
788
|
+
#
|
789
|
+
# With default value `false`:
|
790
|
+
# ary = CSV.parse_line(' a , b ')
|
791
|
+
# ary # => [" a ", " b "]
|
792
|
+
#
|
793
|
+
# With value `true`:
|
794
|
+
# ary = CSV.parse_line(' a , b ', strip: true)
|
795
|
+
# ary # => ["a", "b"]
|
796
|
+
#
|
797
|
+
# ###### Option `liberal_parsing`
|
798
|
+
#
|
799
|
+
# Specifies the boolean or hash value that determines whether CSV will attempt
|
800
|
+
# to parse input not conformant with RFC 4180, such as double quotes in unquoted
|
801
|
+
# fields.
|
802
|
+
#
|
803
|
+
# Default value:
|
804
|
+
# CSV::DEFAULT_OPTIONS.fetch(:liberal_parsing) # => false
|
805
|
+
#
|
806
|
+
# For the next two examples:
|
807
|
+
# str = 'is,this "three, or four",fields'
|
808
|
+
#
|
809
|
+
# Without `liberal_parsing`:
|
810
|
+
# # Raises CSV::MalformedCSVError (Illegal quoting in str 1.)
|
811
|
+
# CSV.parse_line(str)
|
812
|
+
#
|
813
|
+
# With `liberal_parsing`:
|
814
|
+
# ary = CSV.parse_line(str, liberal_parsing: true)
|
815
|
+
# ary # => ["is", "this \"three", " or four\"", "fields"]
|
816
|
+
#
|
817
|
+
# Use the `backslash_quote` sub-option to parse values that use a backslash to
|
818
|
+
# escape a double-quote character. This causes the parser to treat `\"` as if
|
819
|
+
# it were `""`.
|
820
|
+
#
|
821
|
+
# For the next two examples:
|
822
|
+
# str = 'Show,"Harry \"Handcuff\" Houdini, the one and only","Tampa Theater"'
|
823
|
+
#
|
824
|
+
# With `liberal_parsing`, but without the `backslash_quote` sub-option:
|
825
|
+
# # Incorrect interpretation of backslash; incorrectly interprets the quoted comma as a field separator.
|
826
|
+
# ary = CSV.parse_line(str, liberal_parsing: true)
|
827
|
+
# ary # => ["Show", "\"Harry \\\"Handcuff\\\" Houdini", " the one and only\"", "Tampa Theater"]
|
828
|
+
# puts ary[1] # => "Harry \"Handcuff\" Houdini
|
829
|
+
#
|
830
|
+
# With `liberal_parsing` and its `backslash_quote` sub-option:
|
831
|
+
# ary = CSV.parse_line(str, liberal_parsing: { backslash_quote: true })
|
832
|
+
# ary # => ["Show", "Harry \"Handcuff\" Houdini, the one and only", "Tampa Theater"]
|
833
|
+
# puts ary[1] # => Harry "Handcuff" Houdini, the one and only
|
834
|
+
#
|
835
|
+
# ###### Option `nil_value`
|
836
|
+
#
|
837
|
+
# Specifies the object that is to be substituted for each null (no-text) field.
|
838
|
+
#
|
839
|
+
# Default value:
|
840
|
+
# CSV::DEFAULT_OPTIONS.fetch(:nil_value) # => nil
|
841
|
+
#
|
842
|
+
# With the default, `nil`:
|
843
|
+
# CSV.parse_line('a,,b,,c') # => ["a", nil, "b", nil, "c"]
|
844
|
+
#
|
845
|
+
# With a different object:
|
846
|
+
# CSV.parse_line('a,,b,,c', nil_value: 0) # => ["a", 0, "b", 0, "c"]
|
847
|
+
#
|
848
|
+
# ###### Option `empty_value`
|
849
|
+
#
|
850
|
+
# Specifies the object that is to be substituted for each field that has an
|
851
|
+
# empty String.
|
852
|
+
#
|
853
|
+
# Default value:
|
854
|
+
# CSV::DEFAULT_OPTIONS.fetch(:empty_value) # => "" (empty string)
|
855
|
+
#
|
856
|
+
# With the default, `""`:
|
857
|
+
# CSV.parse_line('a,"",b,"",c') # => ["a", "", "b", "", "c"]
|
858
|
+
#
|
859
|
+
# With a different object:
|
860
|
+
# CSV.parse_line('a,"",b,"",c', empty_value: 'x') # => ["a", "x", "b", "x", "c"]
|
861
|
+
#
|
862
|
+
# #### Options for Generating
|
863
|
+
#
|
864
|
+
# Options for generating, described in detail below, include:
|
865
|
+
# * `row_sep`: Specifies the row separator; used to delimit rows.
|
866
|
+
# * `col_sep`: Specifies the column separator; used to delimit fields.
|
867
|
+
# * `quote_char`: Specifies the quote character; used to quote fields.
|
868
|
+
# * `write_headers`: Specifies whether headers are to be written.
|
869
|
+
# * `force_quotes`: Specifies whether each output field is to be quoted.
|
870
|
+
# * `quote_empty`: Specifies whether each empty output field is to be quoted.
|
871
|
+
# * `write_converters`: Specifies the field converters to be used in writing.
|
872
|
+
# * `write_nil_value`: Specifies the object that is to be substituted for each
|
873
|
+
# `nil`-valued field.
|
874
|
+
# * `write_empty_value`: Specifies the object that is to be substituted for
|
875
|
+
# each empty field.
|
876
|
+
#
|
877
|
+
# ###### Option `row_sep`
|
878
|
+
#
|
879
|
+
# Specifies the row separator, a String or the Symbol `:auto` (see below), to be
|
880
|
+
# used for both parsing and generating.
|
881
|
+
#
|
882
|
+
# Default value:
|
883
|
+
# CSV::DEFAULT_OPTIONS.fetch(:row_sep) # => :auto
|
884
|
+
#
|
885
|
+
# ---
|
886
|
+
#
|
887
|
+
# When `row_sep` is a String, that String becomes the row separator. The String
|
888
|
+
# will be transcoded into the data's Encoding before use.
|
889
|
+
#
|
890
|
+
# Using `"\n"`:
|
891
|
+
# row_sep = "\n"
|
892
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
893
|
+
# csv << [:foo, 0]
|
894
|
+
# csv << [:bar, 1]
|
895
|
+
# csv << [:baz, 2]
|
896
|
+
# end
|
897
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
898
|
+
# ary = CSV.parse(str)
|
899
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
900
|
+
#
|
901
|
+
# Using `|` (pipe):
|
902
|
+
# row_sep = '|'
|
903
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
904
|
+
# csv << [:foo, 0]
|
905
|
+
# csv << [:bar, 1]
|
906
|
+
# csv << [:baz, 2]
|
907
|
+
# end
|
908
|
+
# str # => "foo,0|bar,1|baz,2|"
|
909
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
910
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
911
|
+
#
|
912
|
+
# Using `--` (two hyphens):
|
913
|
+
# row_sep = '--'
|
914
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
915
|
+
# csv << [:foo, 0]
|
916
|
+
# csv << [:bar, 1]
|
917
|
+
# csv << [:baz, 2]
|
918
|
+
# end
|
919
|
+
# str # => "foo,0--bar,1--baz,2--"
|
920
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
921
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
922
|
+
#
|
923
|
+
# Using `''` (empty string):
|
924
|
+
# row_sep = ''
|
925
|
+
# str = CSV.generate(row_sep: row_sep) do |csv|
|
926
|
+
# csv << [:foo, 0]
|
927
|
+
# csv << [:bar, 1]
|
928
|
+
# csv << [:baz, 2]
|
929
|
+
# end
|
930
|
+
# str # => "foo,0bar,1baz,2"
|
931
|
+
# ary = CSV.parse(str, row_sep: row_sep)
|
932
|
+
# ary # => [["foo", "0bar", "1baz", "2"]]
|
933
|
+
#
|
934
|
+
# ---
|
935
|
+
#
|
936
|
+
# When `row_sep` is the Symbol `:auto` (the default), generating uses `"\n"` as
|
937
|
+
# the row separator:
|
938
|
+
# str = CSV.generate do |csv|
|
939
|
+
# csv << [:foo, 0]
|
940
|
+
# csv << [:bar, 1]
|
941
|
+
# csv << [:baz, 2]
|
942
|
+
# end
|
943
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
944
|
+
#
|
945
|
+
# Parsing, on the other hand, invokes auto-discovery of the row separator.
|
946
|
+
#
|
947
|
+
# Auto-discovery reads ahead in the data looking for the next `\r\n`, `\n`, or
|
948
|
+
# `\r` sequence. The sequence will be selected even if it occurs in a quoted
|
949
|
+
# field, assuming that you would have the same line endings there.
|
950
|
+
#
|
951
|
+
# Example:
|
952
|
+
# str = CSV.generate do |csv|
|
953
|
+
# csv << [:foo, 0]
|
954
|
+
# csv << [:bar, 1]
|
955
|
+
# csv << [:baz, 2]
|
956
|
+
# end
|
957
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
958
|
+
# ary = CSV.parse(str)
|
959
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
960
|
+
#
|
961
|
+
# The default `$INPUT_RECORD_SEPARATOR` (`$/`) is used if any of the following
|
962
|
+
# is true:
|
963
|
+
# * None of those sequences is found.
|
964
|
+
# * Data is `ARGF`, `STDIN`, `STDOUT`, or `STDERR`.
|
965
|
+
# * The stream is only available for output.
|
966
|
+
#
|
967
|
+
# Obviously, discovery takes a little time. Set manually if speed is important.
|
968
|
+
# Also note that IO objects should be opened in binary mode on Windows if this
|
969
|
+
# feature will be used as the line-ending translation can cause problems with
|
970
|
+
# resetting the document position to where it was before the read ahead.
|
971
|
+
#
|
972
|
+
# ###### Option `col_sep`
|
973
|
+
#
|
974
|
+
# Specifies the String field separator to be used for both parsing and
|
975
|
+
# generating. The String will be transcoded into the data's Encoding before use.
|
976
|
+
#
|
977
|
+
# Default value:
|
978
|
+
# CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
|
979
|
+
#
|
980
|
+
# Using the default (comma):
|
981
|
+
# str = CSV.generate do |csv|
|
982
|
+
# csv << [:foo, 0]
|
983
|
+
# csv << [:bar, 1]
|
984
|
+
# csv << [:baz, 2]
|
985
|
+
# end
|
986
|
+
# str # => "foo,0\nbar,1\nbaz,2\n"
|
987
|
+
# ary = CSV.parse(str)
|
988
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
989
|
+
#
|
990
|
+
# Using `:` (colon):
|
991
|
+
# col_sep = ':'
|
992
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
993
|
+
# csv << [:foo, 0]
|
994
|
+
# csv << [:bar, 1]
|
995
|
+
# csv << [:baz, 2]
|
996
|
+
# end
|
997
|
+
# str # => "foo:0\nbar:1\nbaz:2\n"
|
998
|
+
# ary = CSV.parse(str, col_sep: col_sep)
|
999
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1000
|
+
#
|
1001
|
+
# Using `::` (two colons):
|
1002
|
+
# col_sep = '::'
|
1003
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
1004
|
+
# csv << [:foo, 0]
|
1005
|
+
# csv << [:bar, 1]
|
1006
|
+
# csv << [:baz, 2]
|
1007
|
+
# end
|
1008
|
+
# str # => "foo::0\nbar::1\nbaz::2\n"
|
1009
|
+
# ary = CSV.parse(str, col_sep: col_sep)
|
1010
|
+
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1011
|
+
#
|
1012
|
+
# Using `''` (empty string):
|
1013
|
+
# col_sep = ''
|
1014
|
+
# str = CSV.generate(col_sep: col_sep) do |csv|
|
1015
|
+
# csv << [:foo, 0]
|
1016
|
+
# csv << [:bar, 1]
|
1017
|
+
# csv << [:baz, 2]
|
1018
|
+
# end
|
1019
|
+
# str # => "foo0\nbar1\nbaz2\n"
|
1020
|
+
#
|
1021
|
+
# ---
|
1022
|
+
#
|
1023
|
+
# Raises an exception if parsing with the empty String:
|
1024
|
+
# col_sep = ''
|
1025
|
+
# # Raises ArgumentError (:col_sep must be 1 or more characters: "")
|
1026
|
+
# CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
|
1027
|
+
#
|
1028
|
+
# ###### Option `quote_char`
|
1029
|
+
#
|
1030
|
+
# Specifies the character (String of length 1) used used to quote fields in both
|
1031
|
+
# parsing and generating. This String will be transcoded into the data's
|
1032
|
+
# Encoding before use.
|
1033
|
+
#
|
1034
|
+
# Default value:
|
1035
|
+
# CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote)
|
1036
|
+
#
|
1037
|
+
# This is useful for an application that incorrectly uses `'` (single-quote) to
|
1038
|
+
# quote fields, instead of the correct `"` (double-quote).
|
1039
|
+
#
|
1040
|
+
# Using the default (double quote):
|
1041
|
+
# str = CSV.generate do |csv|
|
1042
|
+
# csv << ['foo', 0]
|
1043
|
+
# csv << ["'bar'", 1]
|
1044
|
+
# csv << ['"baz"', 2]
|
1045
|
+
# end
|
1046
|
+
# str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n"
|
1047
|
+
# ary = CSV.parse(str)
|
1048
|
+
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
1049
|
+
#
|
1050
|
+
# Using `'` (single-quote):
|
1051
|
+
# quote_char = "'"
|
1052
|
+
# str = CSV.generate(quote_char: quote_char) do |csv|
|
1053
|
+
# csv << ['foo', 0]
|
1054
|
+
# csv << ["'bar'", 1]
|
1055
|
+
# csv << ['"baz"', 2]
|
1056
|
+
# end
|
1057
|
+
# str # => "foo,0\n'''bar''',1\n\"baz\",2\n"
|
1058
|
+
# ary = CSV.parse(str, quote_char: quote_char)
|
1059
|
+
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
|
1060
|
+
#
|
1061
|
+
# ---
|
1062
|
+
#
|
1063
|
+
# Raises an exception if the String length is greater than 1:
|
1064
|
+
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
|
1065
|
+
# CSV.new('', quote_char: 'xx')
|
1066
|
+
#
|
1067
|
+
# Raises an exception if the value is not a String:
|
1068
|
+
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
|
1069
|
+
# CSV.new('', quote_char: :foo)
|
1070
|
+
#
|
1071
|
+
# ###### Option `write_headers`
|
1072
|
+
#
|
1073
|
+
# Specifies the boolean that determines whether a header row is included in the
|
1074
|
+
# output; ignored if there are no headers.
|
1075
|
+
#
|
1076
|
+
# Default value:
|
1077
|
+
# CSV::DEFAULT_OPTIONS.fetch(:write_headers) # => nil
|
1078
|
+
#
|
1079
|
+
# Without `write_headers`:
|
1080
|
+
# file_path = 't.csv'
|
1081
|
+
# CSV.open(file_path,'w',
|
1082
|
+
# :headers => ['Name','Value']
|
1083
|
+
# ) do |csv|
|
1084
|
+
# csv << ['foo', '0']
|
1085
|
+
# end
|
1086
|
+
# CSV.open(file_path) do |csv|
|
1087
|
+
# csv.shift
|
1088
|
+
# end # => ["foo", "0"]
|
1089
|
+
#
|
1090
|
+
# With `write_headers`":
|
1091
|
+
# CSV.open(file_path,'w',
|
1092
|
+
# :write_headers => true,
|
1093
|
+
# :headers => ['Name','Value']
|
1094
|
+
# ) do |csv|
|
1095
|
+
# csv << ['foo', '0']
|
1096
|
+
# end
|
1097
|
+
# CSV.open(file_path) do |csv|
|
1098
|
+
# csv.shift
|
1099
|
+
# end # => ["Name", "Value"]
|
1100
|
+
#
|
1101
|
+
# ###### Option `force_quotes`
|
1102
|
+
#
|
1103
|
+
# Specifies the boolean that determines whether each output field is to be
|
1104
|
+
# double-quoted.
|
1105
|
+
#
|
1106
|
+
# Default value:
|
1107
|
+
# CSV::DEFAULT_OPTIONS.fetch(:force_quotes) # => false
|
1108
|
+
#
|
1109
|
+
# For examples in this section:
|
1110
|
+
# ary = ['foo', 0, nil]
|
1111
|
+
#
|
1112
|
+
# Using the default, `false`:
|
1113
|
+
# str = CSV.generate_line(ary)
|
1114
|
+
# str # => "foo,0,\n"
|
1115
|
+
#
|
1116
|
+
# Using `true`:
|
1117
|
+
# str = CSV.generate_line(ary, force_quotes: true)
|
1118
|
+
# str # => "\"foo\",\"0\",\"\"\n"
|
1119
|
+
#
|
1120
|
+
# ###### Option `quote_empty`
|
1121
|
+
#
|
1122
|
+
# Specifies the boolean that determines whether an empty value is to be
|
1123
|
+
# double-quoted.
|
1124
|
+
#
|
1125
|
+
# Default value:
|
1126
|
+
# CSV::DEFAULT_OPTIONS.fetch(:quote_empty) # => true
|
1127
|
+
#
|
1128
|
+
# With the default `true`:
|
1129
|
+
# CSV.generate_line(['"', ""]) # => "\"\"\"\",\"\"\n"
|
1130
|
+
#
|
1131
|
+
# With `false`:
|
1132
|
+
# CSV.generate_line(['"', ""], quote_empty: false) # => "\"\"\"\",\n"
|
1133
|
+
#
|
1134
|
+
# ###### Option `write_converters`
|
1135
|
+
#
|
1136
|
+
# Specifies converters to be used in generating fields. See [Write
|
1137
|
+
# Converters](#class-CSV-label-Write+Converters)
|
1138
|
+
#
|
1139
|
+
# Default value:
|
1140
|
+
# CSV::DEFAULT_OPTIONS.fetch(:write_converters) # => nil
|
1141
|
+
#
|
1142
|
+
# With no write converter:
|
1143
|
+
# str = CSV.generate_line(["\na\n", "\tb\t", " c "])
|
1144
|
+
# str # => "\"\na\n\",\tb\t, c \n"
|
1145
|
+
#
|
1146
|
+
# With a write converter:
|
1147
|
+
# strip_converter = proc {|field| field.strip }
|
1148
|
+
# str = CSV.generate_line(["\na\n", "\tb\t", " c "], write_converters: strip_converter)
|
1149
|
+
# str # => "a,b,c\n"
|
1150
|
+
#
|
1151
|
+
# With two write converters (called in order):
|
1152
|
+
# upcase_converter = proc {|field| field.upcase }
|
1153
|
+
# downcase_converter = proc {|field| field.downcase }
|
1154
|
+
# write_converters = [upcase_converter, downcase_converter]
|
1155
|
+
# str = CSV.generate_line(['a', 'b', 'c'], write_converters: write_converters)
|
1156
|
+
# str # => "a,b,c\n"
|
1157
|
+
#
|
1158
|
+
# See also [Write Converters](#class-CSV-label-Write+Converters)
|
1159
|
+
#
|
1160
|
+
# ###### Option `write_nil_value`
|
1161
|
+
#
|
1162
|
+
# Specifies the object that is to be substituted for each `nil`-valued field.
|
1163
|
+
#
|
1164
|
+
# Default value:
|
1165
|
+
# CSV::DEFAULT_OPTIONS.fetch(:write_nil_value) # => nil
|
1166
|
+
#
|
1167
|
+
# Without the option:
|
1168
|
+
# str = CSV.generate_line(['a', nil, 'c', nil])
|
1169
|
+
# str # => "a,,c,\n"
|
1170
|
+
#
|
1171
|
+
# With the option:
|
1172
|
+
# str = CSV.generate_line(['a', nil, 'c', nil], write_nil_value: "x")
|
1173
|
+
# str # => "a,x,c,x\n"
|
1174
|
+
#
|
1175
|
+
# ###### Option `write_empty_value`
|
1176
|
+
#
|
1177
|
+
# Specifies the object that is to be substituted for each field that has an
|
1178
|
+
# empty String.
|
1179
|
+
#
|
1180
|
+
# Default value:
|
1181
|
+
# CSV::DEFAULT_OPTIONS.fetch(:write_empty_value) # => ""
|
1182
|
+
#
|
1183
|
+
# Without the option:
|
1184
|
+
# str = CSV.generate_line(['a', '', 'c', ''])
|
1185
|
+
# str # => "a,\"\",c,\"\"\n"
|
1186
|
+
#
|
1187
|
+
# With the option:
|
1188
|
+
# str = CSV.generate_line(['a', '', 'c', ''], write_empty_value: "x")
|
1189
|
+
# str # => "a,x,c,x\n"
|
1190
|
+
#
|
1191
|
+
# ### CSV with Headers
|
1192
|
+
#
|
1193
|
+
# CSV allows to specify column names of CSV file, whether they are in data, or
|
1194
|
+
# provided separately. If headers are specified, reading methods return an
|
1195
|
+
# instance of CSV::Table, consisting of CSV::Row.
|
1196
|
+
#
|
1197
|
+
# # Headers are part of data
|
1198
|
+
# data = CSV.parse(<<~ROWS, headers: true)
|
1199
|
+
# Name,Department,Salary
|
1200
|
+
# Bob,Engineering,1000
|
1201
|
+
# Jane,Sales,2000
|
1202
|
+
# John,Management,5000
|
1203
|
+
# ROWS
|
1204
|
+
#
|
1205
|
+
# data.class #=> CSV::Table
|
1206
|
+
# data.first #=> #<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
|
1207
|
+
# data.first.to_h #=> {"Name"=>"Bob", "Department"=>"Engineering", "Salary"=>"1000"}
|
1208
|
+
#
|
1209
|
+
# # Headers provided by developer
|
1210
|
+
# data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary])
|
1211
|
+
# data.first #=> #<CSV::Row name:"Bob" department:"Engineering" salary:"1000">
|
1212
|
+
#
|
1213
|
+
# ### Converters
|
1214
|
+
#
|
1215
|
+
# By default, each value (field or header) parsed by CSV is formed into a
|
1216
|
+
# String. You can use a *field* *converter* or *header* *converter* to
|
1217
|
+
# intercept and modify the parsed values:
|
1218
|
+
# * See [Field Converters](#class-CSV-label-Field+Converters).
|
1219
|
+
# * See [Header Converters](#class-CSV-label-Header+Converters).
|
1220
|
+
#
|
1221
|
+
# Also by default, each value to be written during generation is written
|
1222
|
+
# 'as-is'. You can use a *write* *converter* to modify values before writing.
|
1223
|
+
# * See [Write Converters](#class-CSV-label-Write+Converters).
|
1224
|
+
#
|
1225
|
+
# #### Specifying Converters
|
1226
|
+
#
|
1227
|
+
# You can specify converters for parsing or generating in the `options` argument
|
1228
|
+
# to various CSV methods:
|
1229
|
+
# * Option `converters` for converting parsed field values.
|
1230
|
+
# * Option `header_converters` for converting parsed header values.
|
1231
|
+
# * Option `write_converters` for converting values to be written (generated).
|
1232
|
+
#
|
1233
|
+
# There are three forms for specifying converters:
|
1234
|
+
# * A converter proc: executable code to be used for conversion.
|
1235
|
+
# * A converter name: the name of a stored converter.
|
1236
|
+
# * A converter list: an array of converter procs, converter names, and
|
1237
|
+
# converter lists.
|
1238
|
+
#
|
1239
|
+
# ##### Converter Procs
|
1240
|
+
#
|
1241
|
+
# This converter proc, `strip_converter`, accepts a value `field` and returns
|
1242
|
+
# `field.strip`:
|
1243
|
+
# strip_converter = proc {|field| field.strip }
|
1244
|
+
#
|
1245
|
+
# In this call to `CSV.parse`, the keyword argument `converters:
|
1246
|
+
# string_converter` specifies that:
|
1247
|
+
# * Proc `string_converter` is to be called for each parsed field.
|
1248
|
+
# * The converter's return value is to replace the `field` value.
|
1249
|
+
# Example:
|
1250
|
+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
|
1251
|
+
# array = CSV.parse(string, converters: strip_converter)
|
1252
|
+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1253
|
+
#
|
1254
|
+
# A converter proc can receive a second argument, `field_info`, that contains
|
1255
|
+
# details about the field. This modified `strip_converter` displays its
|
1256
|
+
# arguments:
|
1257
|
+
# strip_converter = proc do |field, field_info|
|
1258
|
+
# p [field, field_info]
|
1259
|
+
# field.strip
|
1260
|
+
# end
|
1261
|
+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
|
1262
|
+
# array = CSV.parse(string, converters: strip_converter)
|
1263
|
+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1264
|
+
#
|
1265
|
+
# Output:
|
1266
|
+
# [" foo ", #<struct CSV::FieldInfo index=0, line=1, header=nil>]
|
1267
|
+
# [" 0 ", #<struct CSV::FieldInfo index=1, line=1, header=nil>]
|
1268
|
+
# [" bar ", #<struct CSV::FieldInfo index=0, line=2, header=nil>]
|
1269
|
+
# [" 1 ", #<struct CSV::FieldInfo index=1, line=2, header=nil>]
|
1270
|
+
# [" baz ", #<struct CSV::FieldInfo index=0, line=3, header=nil>]
|
1271
|
+
# [" 2 ", #<struct CSV::FieldInfo index=1, line=3, header=nil>]
|
1272
|
+
#
|
1273
|
+
# Each CSV::FieldInfo object shows:
|
1274
|
+
# * The 0-based field index.
|
1275
|
+
# * The 1-based line index.
|
1276
|
+
# * The field header, if any.
|
1277
|
+
#
|
1278
|
+
# ##### Stored Converters
|
1279
|
+
#
|
1280
|
+
# A converter may be given a name and stored in a structure where the parsing
|
1281
|
+
# methods can find it by name.
|
1282
|
+
#
|
1283
|
+
# The storage structure for field converters is the Hash CSV::Converters. It has
|
1284
|
+
# several built-in converter procs:
|
1285
|
+
# * `:integer`: converts each String-embedded integer into a true Integer.
|
1286
|
+
# * `:float`: converts each String-embedded float into a true Float.
|
1287
|
+
# * `:date`: converts each String-embedded date into a true Date.
|
1288
|
+
# * `:date_time`: converts each String-embedded date-time into a true DateTime
|
1289
|
+
# . This example creates a converter proc, then stores it:
|
1290
|
+
# strip_converter = proc {|field| field.strip }
|
1291
|
+
# CSV::Converters[:strip] = strip_converter
|
1292
|
+
#
|
1293
|
+
# Then the parsing method call can refer to the converter by its name, `:strip`:
|
1294
|
+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
|
1295
|
+
# array = CSV.parse(string, converters: :strip)
|
1296
|
+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1297
|
+
#
|
1298
|
+
# The storage structure for header converters is the Hash CSV::HeaderConverters,
|
1299
|
+
# which works in the same way. It also has built-in converter procs:
|
1300
|
+
# * `:downcase`: Downcases each header.
|
1301
|
+
# * `:symbol`: Converts each header to a Symbol.
|
1302
|
+
#
|
1303
|
+
# There is no such storage structure for write headers.
|
1304
|
+
#
|
1305
|
+
# In order for the parsing methods to access stored converters in
|
1306
|
+
# non-main-Ractors, the storage structure must be made shareable first.
|
1307
|
+
# Therefore, `Ractor.make_shareable(CSV::Converters)` and
|
1308
|
+
# `Ractor.make_shareable(CSV::HeaderConverters)` must be called before the
|
1309
|
+
# creation of Ractors that use the converters stored in these structures. (Since
|
1310
|
+
# making the storage structures shareable involves freezing them, any custom
|
1311
|
+
# converters that are to be used must be added first.)
|
1312
|
+
#
|
1313
|
+
# ##### Converter Lists
|
1314
|
+
#
|
1315
|
+
# A *converter* *list* is an Array that may include any assortment of:
|
1316
|
+
# * Converter procs.
|
1317
|
+
# * Names of stored converters.
|
1318
|
+
# * Nested converter lists.
|
1319
|
+
#
|
1320
|
+
# Examples:
|
1321
|
+
# numeric_converters = [:integer, :float]
|
1322
|
+
# date_converters = [:date, :date_time]
|
1323
|
+
# [numeric_converters, strip_converter]
|
1324
|
+
# [strip_converter, date_converters, :float]
|
1325
|
+
#
|
1326
|
+
# Like a converter proc, a converter list may be named and stored in either
|
1327
|
+
# CSV::Converters or CSV::HeaderConverters:
|
1328
|
+
# CSV::Converters[:custom] = [strip_converter, date_converters, :float]
|
1329
|
+
# CSV::HeaderConverters[:custom] = [:downcase, :symbol]
|
1330
|
+
#
|
1331
|
+
# There are two built-in converter lists:
|
1332
|
+
# CSV::Converters[:numeric] # => [:integer, :float]
|
1333
|
+
# CSV::Converters[:all] # => [:date_time, :numeric]
|
1334
|
+
#
|
1335
|
+
# #### Field Converters
|
1336
|
+
#
|
1337
|
+
# With no conversion, all parsed fields in all rows become Strings:
|
1338
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1339
|
+
# ary = CSV.parse(string)
|
1340
|
+
# ary # => # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1341
|
+
#
|
1342
|
+
# When you specify a field converter, each parsed field is passed to the
|
1343
|
+
# converter; its return value becomes the stored value for the field. A
|
1344
|
+
# converter might, for example, convert an integer embedded in a String into a
|
1345
|
+
# true Integer. (In fact, that's what built-in field converter `:integer` does.)
|
1346
|
+
#
|
1347
|
+
# There are three ways to use field converters.
|
1348
|
+
#
|
1349
|
+
# * Using option [converters](#class-CSV-label-Option+converters) with a
|
1350
|
+
# parsing method:
|
1351
|
+
# ary = CSV.parse(string, converters: :integer)
|
1352
|
+
# ary # => [0, 1, 2] # => [["foo", 0], ["bar", 1], ["baz", 2]]
|
1353
|
+
#
|
1354
|
+
# * Using option [converters](#class-CSV-label-Option+converters) with a new
|
1355
|
+
# CSV instance:
|
1356
|
+
# csv = CSV.new(string, converters: :integer)
|
1357
|
+
# # Field converters in effect:
|
1358
|
+
# csv.converters # => [:integer]
|
1359
|
+
# csv.read # => [["foo", 0], ["bar", 1], ["baz", 2]]
|
1360
|
+
#
|
1361
|
+
# * Using method #convert to add a field converter to a CSV instance:
|
1362
|
+
# csv = CSV.new(string)
|
1363
|
+
# # Add a converter.
|
1364
|
+
# csv.convert(:integer)
|
1365
|
+
# csv.converters # => [:integer]
|
1366
|
+
# csv.read # => [["foo", 0], ["bar", 1], ["baz", 2]]
|
1367
|
+
#
|
1368
|
+
# Installing a field converter does not affect already-read rows:
|
1369
|
+
# csv = CSV.new(string)
|
1370
|
+
# csv.shift # => ["foo", "0"]
|
1371
|
+
# # Add a converter.
|
1372
|
+
# csv.convert(:integer)
|
1373
|
+
# csv.converters # => [:integer]
|
1374
|
+
# csv.read # => [["bar", 1], ["baz", 2]]
|
1375
|
+
#
|
1376
|
+
# There are additional built-in converters, and custom converters are also
|
1377
|
+
# supported.
|
1378
|
+
#
|
1379
|
+
# ##### Built-In Field Converters
|
1380
|
+
#
|
1381
|
+
# The built-in field converters are in Hash CSV::Converters:
|
1382
|
+
# * Each key is a field converter name.
|
1383
|
+
# * Each value is one of:
|
1384
|
+
# * A Proc field converter.
|
1385
|
+
# * An Array of field converter names.
|
1386
|
+
#
|
1387
|
+
# Display:
|
1388
|
+
# CSV::Converters.each_pair do |name, value|
|
1389
|
+
# if value.kind_of?(Proc)
|
1390
|
+
# p [name, value.class]
|
1391
|
+
# else
|
1392
|
+
# p [name, value]
|
1393
|
+
# end
|
1394
|
+
# end
|
1395
|
+
#
|
1396
|
+
# Output:
|
1397
|
+
# [:integer, Proc]
|
1398
|
+
# [:float, Proc]
|
1399
|
+
# [:numeric, [:integer, :float]]
|
1400
|
+
# [:date, Proc]
|
1401
|
+
# [:date_time, Proc]
|
1402
|
+
# [:all, [:date_time, :numeric]]
|
1403
|
+
#
|
1404
|
+
# Each of these converters transcodes values to UTF-8 before attempting
|
1405
|
+
# conversion. If a value cannot be transcoded to UTF-8 the conversion will fail
|
1406
|
+
# and the value will remain unconverted.
|
1407
|
+
#
|
1408
|
+
# Converter `:integer` converts each field that Integer() accepts:
|
1409
|
+
# data = '0,1,2,x'
|
1410
|
+
# # Without the converter
|
1411
|
+
# csv = CSV.parse_line(data)
|
1412
|
+
# csv # => ["0", "1", "2", "x"]
|
1413
|
+
# # With the converter
|
1414
|
+
# csv = CSV.parse_line(data, converters: :integer)
|
1415
|
+
# csv # => [0, 1, 2, "x"]
|
1416
|
+
#
|
1417
|
+
# Converter `:float` converts each field that Float() accepts:
|
1418
|
+
# data = '1.0,3.14159,x'
|
1419
|
+
# # Without the converter
|
1420
|
+
# csv = CSV.parse_line(data)
|
1421
|
+
# csv # => ["1.0", "3.14159", "x"]
|
1422
|
+
# # With the converter
|
1423
|
+
# csv = CSV.parse_line(data, converters: :float)
|
1424
|
+
# csv # => [1.0, 3.14159, "x"]
|
1425
|
+
#
|
1426
|
+
# Converter `:numeric` converts with both `:integer` and `:float`..
|
1427
|
+
#
|
1428
|
+
# Converter `:date` converts each field that Date::parse accepts:
|
1429
|
+
# data = '2001-02-03,x'
|
1430
|
+
# # Without the converter
|
1431
|
+
# csv = CSV.parse_line(data)
|
1432
|
+
# csv # => ["2001-02-03", "x"]
|
1433
|
+
# # With the converter
|
1434
|
+
# csv = CSV.parse_line(data, converters: :date)
|
1435
|
+
# csv # => [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, "x"]
|
1436
|
+
#
|
1437
|
+
# Converter `:date_time` converts each field that DateTime::parse accepts:
|
1438
|
+
# data = '2020-05-07T14:59:00-05:00,x'
|
1439
|
+
# # Without the converter
|
1440
|
+
# csv = CSV.parse_line(data)
|
1441
|
+
# csv # => ["2020-05-07T14:59:00-05:00", "x"]
|
1442
|
+
# # With the converter
|
1443
|
+
# csv = CSV.parse_line(data, converters: :date_time)
|
1444
|
+
# csv # => [#<DateTime: 2020-05-07T14:59:00-05:00 ((2458977j,71940s,0n),-18000s,2299161j)>, "x"]
|
1445
|
+
#
|
1446
|
+
# Converter `:numeric` converts with both `:date_time` and `:numeric`..
|
1447
|
+
#
|
1448
|
+
# As seen above, method #convert adds converters to a CSV instance, and method
|
1449
|
+
# #converters returns an Array of the converters in effect:
|
1450
|
+
# csv = CSV.new('0,1,2')
|
1451
|
+
# csv.converters # => []
|
1452
|
+
# csv.convert(:integer)
|
1453
|
+
# csv.converters # => [:integer]
|
1454
|
+
# csv.convert(:date)
|
1455
|
+
# csv.converters # => [:integer, :date]
|
1456
|
+
#
|
1457
|
+
# ##### Custom Field Converters
|
1458
|
+
#
|
1459
|
+
# You can define a custom field converter:
|
1460
|
+
# strip_converter = proc {|field| field.strip }
|
1461
|
+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
|
1462
|
+
# array = CSV.parse(string, converters: strip_converter)
|
1463
|
+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1464
|
+
#
|
1465
|
+
# You can register the converter in Converters Hash, which allows you to refer
|
1466
|
+
# to it by name:
|
1467
|
+
# CSV::Converters[:strip] = strip_converter
|
1468
|
+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
|
1469
|
+
# array = CSV.parse(string, converters: :strip)
|
1470
|
+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1471
|
+
#
|
1472
|
+
# #### Header Converters
|
1473
|
+
#
|
1474
|
+
# Header converters operate only on headers (and not on other rows).
|
1475
|
+
#
|
1476
|
+
# There are three ways to use header converters; these examples use built-in
|
1477
|
+
# header converter `:downcase`, which downcases each parsed header.
|
1478
|
+
#
|
1479
|
+
# * Option `header_converters` with a singleton parsing method:
|
1480
|
+
# string = "Name,Count\nFoo,0\n,Bar,1\nBaz,2"
|
1481
|
+
# tbl = CSV.parse(string, headers: true, header_converters: :downcase)
|
1482
|
+
# tbl.class # => CSV::Table
|
1483
|
+
# tbl.headers # => ["name", "count"]
|
1484
|
+
#
|
1485
|
+
# * Option `header_converters` with a new CSV instance:
|
1486
|
+
# csv = CSV.new(string, header_converters: :downcase)
|
1487
|
+
# # Header converters in effect:
|
1488
|
+
# csv.header_converters # => [:downcase]
|
1489
|
+
# tbl = CSV.parse(string, headers: true)
|
1490
|
+
# tbl.headers # => ["Name", "Count"]
|
1491
|
+
#
|
1492
|
+
# * Method #header_convert adds a header converter to a CSV instance:
|
1493
|
+
# csv = CSV.new(string)
|
1494
|
+
# # Add a header converter.
|
1495
|
+
# csv.header_convert(:downcase)
|
1496
|
+
# csv.header_converters # => [:downcase]
|
1497
|
+
# tbl = CSV.parse(string, headers: true)
|
1498
|
+
# tbl.headers # => ["Name", "Count"]
|
1499
|
+
#
|
1500
|
+
# ##### Built-In Header Converters
|
1501
|
+
#
|
1502
|
+
# The built-in header converters are in Hash CSV::HeaderConverters. The keys
|
1503
|
+
# there are the names of the converters:
|
1504
|
+
# CSV::HeaderConverters.keys # => [:downcase, :symbol]
|
1505
|
+
#
|
1506
|
+
# Converter `:downcase` converts each header by downcasing it:
|
1507
|
+
# string = "Name,Count\nFoo,0\n,Bar,1\nBaz,2"
|
1508
|
+
# tbl = CSV.parse(string, headers: true, header_converters: :downcase)
|
1509
|
+
# tbl.class # => CSV::Table
|
1510
|
+
# tbl.headers # => ["name", "count"]
|
1511
|
+
#
|
1512
|
+
# Converter `:symbol` converts each header by making it into a Symbol:
|
1513
|
+
# string = "Name,Count\nFoo,0\n,Bar,1\nBaz,2"
|
1514
|
+
# tbl = CSV.parse(string, headers: true, header_converters: :symbol)
|
1515
|
+
# tbl.headers # => [:name, :count]
|
1516
|
+
#
|
1517
|
+
# Details:
|
1518
|
+
# * Strips leading and trailing whitespace.
|
1519
|
+
# * Downcases the header.
|
1520
|
+
# * Replaces embedded spaces with underscores.
|
1521
|
+
# * Removes non-word characters.
|
1522
|
+
# * Makes the string into a Symbol.
|
1523
|
+
#
|
1524
|
+
# ##### Custom Header Converters
|
1525
|
+
#
|
1526
|
+
# You can define a custom header converter:
|
1527
|
+
# upcase_converter = proc {|header| header.upcase }
|
1528
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1529
|
+
# table = CSV.parse(string, headers: true, header_converters: upcase_converter)
|
1530
|
+
# table # => #<CSV::Table mode:col_or_row row_count:4>
|
1531
|
+
# table.headers # => ["NAME", "VALUE"]
|
1532
|
+
#
|
1533
|
+
# You can register the converter in HeaderConverters Hash, which allows you to
|
1534
|
+
# refer to it by name:
|
1535
|
+
# CSV::HeaderConverters[:upcase] = upcase_converter
|
1536
|
+
# table = CSV.parse(string, headers: true, header_converters: :upcase)
|
1537
|
+
# table # => #<CSV::Table mode:col_or_row row_count:4>
|
1538
|
+
# table.headers # => ["NAME", "VALUE"]
|
1539
|
+
#
|
1540
|
+
# ##### Write Converters
|
1541
|
+
#
|
1542
|
+
# When you specify a write converter for generating CSV, each field to be
|
1543
|
+
# written is passed to the converter; its return value becomes the new value for
|
1544
|
+
# the field. A converter might, for example, strip whitespace from a field.
|
1545
|
+
#
|
1546
|
+
# Using no write converter (all fields unmodified):
|
1547
|
+
# output_string = CSV.generate do |csv|
|
1548
|
+
# csv << [' foo ', 0]
|
1549
|
+
# csv << [' bar ', 1]
|
1550
|
+
# csv << [' baz ', 2]
|
1551
|
+
# end
|
1552
|
+
# output_string # => " foo ,0\n bar ,1\n baz ,2\n"
|
1553
|
+
#
|
1554
|
+
# Using option `write_converters` with two custom write converters:
|
1555
|
+
# strip_converter = proc {|field| field.respond_to?(:strip) ? field.strip : field }
|
1556
|
+
# upcase_converter = proc {|field| field.respond_to?(:upcase) ? field.upcase : field }
|
1557
|
+
# write_converters = [strip_converter, upcase_converter]
|
1558
|
+
# output_string = CSV.generate(write_converters: write_converters) do |csv|
|
1559
|
+
# csv << [' foo ', 0]
|
1560
|
+
# csv << [' bar ', 1]
|
1561
|
+
# csv << [' baz ', 2]
|
1562
|
+
# end
|
1563
|
+
# output_string # => "FOO,0\nBAR,1\nBAZ,2\n"
|
1564
|
+
#
|
1565
|
+
# ### Character Encodings (M17n or Multilingualization)
|
1566
|
+
#
|
1567
|
+
# This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
|
1568
|
+
# or String object being read from or written to. Your data is never transcoded
|
1569
|
+
# (unless you ask Ruby to transcode it for you) and will literally be parsed in
|
1570
|
+
# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
|
1571
|
+
# Encoding of your data. This is accomplished by transcoding the parser itself
|
1572
|
+
# into your Encoding.
|
1573
|
+
#
|
1574
|
+
# Some transcoding must take place, of course, to accomplish this multiencoding
|
1575
|
+
# support. For example, `:col_sep`, `:row_sep`, and `:quote_char` must be
|
1576
|
+
# transcoded to match your data. Hopefully this makes the entire process feel
|
1577
|
+
# transparent, since CSV's defaults should just magically work for your data.
|
1578
|
+
# However, you can set these values manually in the target Encoding to avoid the
|
1579
|
+
# translation.
|
1580
|
+
#
|
1581
|
+
# It's also important to note that while all of CSV's core parser is now
|
1582
|
+
# Encoding agnostic, some features are not. For example, the built-in converters
|
1583
|
+
# will try to transcode data to UTF-8 before making conversions. Again, you can
|
1584
|
+
# provide custom converters that are aware of your Encodings to avoid this
|
1585
|
+
# translation. It's just too hard for me to support native conversions in all of
|
1586
|
+
# Ruby's Encodings.
|
1587
|
+
#
|
1588
|
+
# Anyway, the practical side of this is simple: make sure IO and String objects
|
1589
|
+
# passed into CSV have the proper Encoding set and everything should just work.
|
1590
|
+
# CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(),
|
1591
|
+
# CSV::read(), and CSV::readlines()) do allow you to specify the Encoding.
|
1592
|
+
#
|
1593
|
+
# One minor exception comes when generating CSV into a String with an Encoding
|
1594
|
+
# that is not ASCII compatible. There's no existing data for CSV to use to
|
1595
|
+
# prepare itself and thus you will probably need to manually specify the desired
|
1596
|
+
# Encoding for most of those cases. It will try to guess using the fields in a
|
1597
|
+
# row of output though, when using CSV::generate_line() or Array#to_csv().
|
1598
|
+
#
|
1599
|
+
# I try to point out any other Encoding issues in the documentation of methods
|
1600
|
+
# as they come up.
|
1601
|
+
#
|
1602
|
+
# This has been tested to the best of my ability with all non-"dummy" Encodings
|
1603
|
+
# Ruby ships with. However, it is brave new code and may have some bugs. Please
|
1604
|
+
# feel free to [report](mailto:james@grayproductions.net) any issues you find
|
1605
|
+
# with it.
|
1606
|
+
#
|
1607
|
+
class CSV < Object
|
1608
|
+
include Enumerable[untyped]
|
1609
|
+
extend Forwardable
|
1610
|
+
|
1611
|
+
# <!--
|
1612
|
+
# rdoc-file=lib/csv.rb
|
1613
|
+
# - foreach(path_or_io, mode='r', **options) {|row| ... )
|
1614
|
+
# - foreach(path_or_io, mode='r', **options) -> new_enumerator
|
1615
|
+
# -->
|
1616
|
+
# Calls the block with each row read from source `path_or_io`.
|
1617
|
+
#
|
1618
|
+
# Path input without headers:
|
1619
|
+
#
|
1620
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1621
|
+
# in_path = 't.csv'
|
1622
|
+
# File.write(in_path, string)
|
1623
|
+
# CSV.foreach(in_path) {|row| p row }
|
1624
|
+
#
|
1625
|
+
# Output:
|
1626
|
+
#
|
1627
|
+
# ["foo", "0"]
|
1628
|
+
# ["bar", "1"]
|
1629
|
+
# ["baz", "2"]
|
1630
|
+
#
|
1631
|
+
# Path input with headers:
|
1632
|
+
#
|
1633
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1634
|
+
# in_path = 't.csv'
|
1635
|
+
# File.write(in_path, string)
|
1636
|
+
# CSV.foreach(in_path, headers: true) {|row| p row }
|
1637
|
+
#
|
1638
|
+
# Output:
|
1639
|
+
#
|
1640
|
+
# <CSV::Row "Name":"foo" "Value":"0">
|
1641
|
+
# <CSV::Row "Name":"bar" "Value":"1">
|
1642
|
+
# <CSV::Row "Name":"baz" "Value":"2">
|
1643
|
+
#
|
1644
|
+
# IO stream input without headers:
|
1645
|
+
#
|
1646
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1647
|
+
# path = 't.csv'
|
1648
|
+
# File.write(path, string)
|
1649
|
+
# File.open('t.csv') do |in_io|
|
1650
|
+
# CSV.foreach(in_io) {|row| p row }
|
1651
|
+
# end
|
1652
|
+
#
|
1653
|
+
# Output:
|
1654
|
+
#
|
1655
|
+
# ["foo", "0"]
|
1656
|
+
# ["bar", "1"]
|
1657
|
+
# ["baz", "2"]
|
1658
|
+
#
|
1659
|
+
# IO stream input with headers:
|
1660
|
+
#
|
1661
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1662
|
+
# path = 't.csv'
|
1663
|
+
# File.write(path, string)
|
1664
|
+
# File.open('t.csv') do |in_io|
|
1665
|
+
# CSV.foreach(in_io, headers: true) {|row| p row }
|
1666
|
+
# end
|
1667
|
+
#
|
1668
|
+
# Output:
|
1669
|
+
#
|
1670
|
+
# <CSV::Row "Name":"foo" "Value":"0">
|
1671
|
+
# <CSV::Row "Name":"bar" "Value":"1">
|
1672
|
+
# <CSV::Row "Name":"baz" "Value":"2">
|
1673
|
+
#
|
1674
|
+
# With no block given, returns an Enumerator:
|
1675
|
+
#
|
1676
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1677
|
+
# path = 't.csv'
|
1678
|
+
# File.write(path, string)
|
1679
|
+
# CSV.foreach(path) # => #<Enumerator: CSV:foreach("t.csv", "r")>
|
1680
|
+
#
|
1681
|
+
# Arguments:
|
1682
|
+
# * Argument `path_or_io` must be a file path or an IO stream.
|
1683
|
+
# * Argument `mode`, if given, must be a File mode. See [Access
|
1684
|
+
# Modes](rdoc-ref:File@Access+Modes).
|
1685
|
+
# * Arguments `**options` must be keyword options. See [Options for
|
1686
|
+
# Parsing](#class-CSV-label-Options+for+Parsing).
|
1687
|
+
# * This method optionally accepts an additional `:encoding` option that you
|
1688
|
+
# can use to specify the Encoding of the data read from `path` or `io`. You
|
1689
|
+
# must provide this unless your data is in the encoding given by
|
1690
|
+
# `Encoding::default_external`. Parsing will use this to determine how to
|
1691
|
+
# parse the data. You may provide a second Encoding to have the data
|
1692
|
+
# transcoded as it is read. For example,
|
1693
|
+
# encoding: 'UTF-32BE:UTF-8'
|
1694
|
+
#
|
1695
|
+
# would read `UTF-32BE` data from the file but transcode it to `UTF-8`
|
1696
|
+
# before parsing.
|
1697
|
+
#
|
1698
|
+
def self.foreach: (String | IO path, ?String mode, headers: true | :first_row | Array[untyped] | String, **untyped options) { (::CSV::Row arg0) -> void } -> void
|
1699
|
+
| (String | IO path, ?String mode, headers: true | :first_row | Array[untyped] | String, **untyped options) -> Enumerator[::CSV::Row, void]
|
1700
|
+
| (String | IO path, ?String mode, **untyped options) { (::Array[String?] arg0) -> void } -> void
|
1701
|
+
| (String | IO path, ?String mode, **untyped options) -> Enumerator[::Array[String?], void]
|
1702
|
+
|
1703
|
+
# <!--
|
1704
|
+
# rdoc-file=lib/csv.rb
|
1705
|
+
# - CSV.new(string)
|
1706
|
+
# - CSV.new(io)
|
1707
|
+
# - CSV.new(string, **options)
|
1708
|
+
# - CSV.new(io, **options)
|
1709
|
+
# -->
|
1710
|
+
# Returns the new CSV object created using `string` or `io` and the specified
|
1711
|
+
# `options`.
|
1712
|
+
#
|
1713
|
+
# * Argument `string` should be a String object; it will be put into a new
|
1714
|
+
# StringIO object positioned at the beginning.
|
1715
|
+
# * Argument `io` should be an IO object that is:
|
1716
|
+
# * Open for reading; on return, the IO object will be closed.
|
1717
|
+
# * Positioned at the beginning. To position at the end, for appending,
|
1718
|
+
# use method CSV.generate. For any other positioning, pass a preset
|
1719
|
+
# StringIO object instead.
|
1720
|
+
# * Argument `options`: See:
|
1721
|
+
# * [Options for Parsing](#class-CSV-label-Options+for+Parsing)
|
1722
|
+
# * [Options for Generating](#class-CSV-label-Options+for+Generating)
|
1723
|
+
# For performance reasons, the options cannot be overridden in a CSV object,
|
1724
|
+
# so those specified here will endure.
|
1725
|
+
#
|
1726
|
+
# In addition to the CSV instance methods, several IO methods are delegated. See
|
1727
|
+
# [Delegated Methods](#class-CSV-label-Delegated+Methods).
|
1728
|
+
#
|
1729
|
+
# ---
|
1730
|
+
#
|
1731
|
+
# Create a CSV object from a String object:
|
1732
|
+
# csv = CSV.new('foo,0')
|
1733
|
+
# csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
|
1734
|
+
#
|
1735
|
+
# Create a CSV object from a File object:
|
1736
|
+
# File.write('t.csv', 'foo,0')
|
1737
|
+
# csv = CSV.new(File.open('t.csv'))
|
1738
|
+
# csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
|
1739
|
+
#
|
1740
|
+
# ---
|
1741
|
+
#
|
1742
|
+
# Raises an exception if the argument is `nil`:
|
1743
|
+
# # Raises ArgumentError (Cannot parse nil as CSV):
|
1744
|
+
# CSV.new(nil)
|
1745
|
+
#
|
1746
|
+
def initialize: (?String | IO | StringIO io, ?::Hash[Symbol, untyped] options) -> void
|
1747
|
+
|
1748
|
+
# <!--
|
1749
|
+
# rdoc-file=lib/csv.rb
|
1750
|
+
# - parse(string) -> array_of_arrays
|
1751
|
+
# - parse(io) -> array_of_arrays
|
1752
|
+
# - parse(string, headers: ..., **options) -> csv_table
|
1753
|
+
# - parse(io, headers: ..., **options) -> csv_table
|
1754
|
+
# - parse(string, **options) {|row| ... }
|
1755
|
+
# - parse(io, **options) {|row| ... }
|
1756
|
+
# -->
|
1757
|
+
# Parses `string` or `io` using the specified `options`.
|
1758
|
+
#
|
1759
|
+
# * Argument `string` should be a String object; it will be put into a new
|
1760
|
+
# StringIO object positioned at the beginning.
|
1761
|
+
# * Argument `io` should be an IO object that is:
|
1762
|
+
# * Open for reading; on return, the IO object will be closed.
|
1763
|
+
# * Positioned at the beginning. To position at the end, for appending,
|
1764
|
+
# use method CSV.generate. For any other positioning, pass a preset
|
1765
|
+
# StringIO object instead.
|
1766
|
+
# * Argument `options`: see [Options for
|
1767
|
+
# Parsing](#class-CSV-label-Options+for+Parsing)
|
1768
|
+
#
|
1769
|
+
# ###### Without Option `headers`
|
1770
|
+
#
|
1771
|
+
# Without {option `headers`[}](#class-CSV-label-Option+headers) case.
|
1772
|
+
#
|
1773
|
+
# These examples assume prior execution of:
|
1774
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1775
|
+
# path = 't.csv'
|
1776
|
+
# File.write(path, string)
|
1777
|
+
#
|
1778
|
+
# ---
|
1779
|
+
#
|
1780
|
+
# With no block given, returns an Array of Arrays formed from the source.
|
1781
|
+
#
|
1782
|
+
# Parse a String:
|
1783
|
+
# a_of_a = CSV.parse(string)
|
1784
|
+
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1785
|
+
#
|
1786
|
+
# Parse an open File:
|
1787
|
+
# a_of_a = File.open(path) do |file|
|
1788
|
+
# CSV.parse(file)
|
1789
|
+
# end
|
1790
|
+
# a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1791
|
+
#
|
1792
|
+
# ---
|
1793
|
+
#
|
1794
|
+
# With a block given, calls the block with each parsed row:
|
1795
|
+
#
|
1796
|
+
# Parse a String:
|
1797
|
+
# CSV.parse(string) {|row| p row }
|
1798
|
+
#
|
1799
|
+
# Output:
|
1800
|
+
# ["foo", "0"]
|
1801
|
+
# ["bar", "1"]
|
1802
|
+
# ["baz", "2"]
|
1803
|
+
#
|
1804
|
+
# Parse an open File:
|
1805
|
+
# File.open(path) do |file|
|
1806
|
+
# CSV.parse(file) {|row| p row }
|
1807
|
+
# end
|
1808
|
+
#
|
1809
|
+
# Output:
|
1810
|
+
# ["foo", "0"]
|
1811
|
+
# ["bar", "1"]
|
1812
|
+
# ["baz", "2"]
|
1813
|
+
#
|
1814
|
+
# ###### With Option `headers`
|
1815
|
+
#
|
1816
|
+
# With {option `headers`[}](#class-CSV-label-Option+headers) case.
|
1817
|
+
#
|
1818
|
+
# These examples assume prior execution of:
|
1819
|
+
# string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
|
1820
|
+
# path = 't.csv'
|
1821
|
+
# File.write(path, string)
|
1822
|
+
#
|
1823
|
+
# ---
|
1824
|
+
#
|
1825
|
+
# With no block given, returns a CSV::Table object formed from the source.
|
1826
|
+
#
|
1827
|
+
# Parse a String:
|
1828
|
+
# csv_table = CSV.parse(string, headers: ['Name', 'Count'])
|
1829
|
+
# csv_table # => #<CSV::Table mode:col_or_row row_count:5>
|
1830
|
+
#
|
1831
|
+
# Parse an open File:
|
1832
|
+
# csv_table = File.open(path) do |file|
|
1833
|
+
# CSV.parse(file, headers: ['Name', 'Count'])
|
1834
|
+
# end
|
1835
|
+
# csv_table # => #<CSV::Table mode:col_or_row row_count:4>
|
1836
|
+
#
|
1837
|
+
# ---
|
1838
|
+
#
|
1839
|
+
# With a block given, calls the block with each parsed row, which has been
|
1840
|
+
# formed into a CSV::Row object:
|
1841
|
+
#
|
1842
|
+
# Parse a String:
|
1843
|
+
# CSV.parse(string, headers: ['Name', 'Count']) {|row| p row }
|
1844
|
+
#
|
1845
|
+
# Output:
|
1846
|
+
# # <CSV::Row "Name":"foo" "Count":"0">
|
1847
|
+
# # <CSV::Row "Name":"bar" "Count":"1">
|
1848
|
+
# # <CSV::Row "Name":"baz" "Count":"2">
|
1849
|
+
#
|
1850
|
+
# Parse an open File:
|
1851
|
+
# File.open(path) do |file|
|
1852
|
+
# CSV.parse(file, headers: ['Name', 'Count']) {|row| p row }
|
1853
|
+
# end
|
1854
|
+
#
|
1855
|
+
# Output:
|
1856
|
+
# # <CSV::Row "Name":"foo" "Count":"0">
|
1857
|
+
# # <CSV::Row "Name":"bar" "Count":"1">
|
1858
|
+
# # <CSV::Row "Name":"baz" "Count":"2">
|
1859
|
+
#
|
1860
|
+
# ---
|
1861
|
+
#
|
1862
|
+
# Raises an exception if the argument is not a String object or IO object:
|
1863
|
+
# # Raises NoMethodError (undefined method `close' for :foo:Symbol)
|
1864
|
+
# CSV.parse(:foo)
|
1865
|
+
#
|
1866
|
+
def self.parse: (String str, ?::Hash[Symbol, untyped] options) ?{ (::Array[String?] arg0) -> void } -> ::Array[::Array[String?]]?
|
1867
|
+
|
1868
|
+
# <!--
|
1869
|
+
# rdoc-file=lib/csv.rb
|
1870
|
+
# - CSV.parse_line(string) -> new_array or nil
|
1871
|
+
# - CSV.parse_line(io) -> new_array or nil
|
1872
|
+
# - CSV.parse_line(string, **options) -> new_array or nil
|
1873
|
+
# - CSV.parse_line(io, **options) -> new_array or nil
|
1874
|
+
# - CSV.parse_line(string, headers: true, **options) -> csv_row or nil
|
1875
|
+
# - CSV.parse_line(io, headers: true, **options) -> csv_row or nil
|
1876
|
+
# -->
|
1877
|
+
# Returns the data created by parsing the first line of `string` or `io` using
|
1878
|
+
# the specified `options`.
|
1879
|
+
#
|
1880
|
+
# * Argument `string` should be a String object; it will be put into a new
|
1881
|
+
# StringIO object positioned at the beginning.
|
1882
|
+
# * Argument `io` should be an IO object that is:
|
1883
|
+
# * Open for reading; on return, the IO object will be closed.
|
1884
|
+
# * Positioned at the beginning. To position at the end, for appending,
|
1885
|
+
# use method CSV.generate. For any other positioning, pass a preset
|
1886
|
+
# StringIO object instead.
|
1887
|
+
# * Argument `options`: see [Options for
|
1888
|
+
# Parsing](#class-CSV-label-Options+for+Parsing)
|
1889
|
+
#
|
1890
|
+
# ###### Without Option `headers`
|
1891
|
+
#
|
1892
|
+
# Without option `headers`, returns the first row as a new Array.
|
1893
|
+
#
|
1894
|
+
# These examples assume prior execution of:
|
1895
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1896
|
+
# path = 't.csv'
|
1897
|
+
# File.write(path, string)
|
1898
|
+
#
|
1899
|
+
# Parse the first line from a String object:
|
1900
|
+
# CSV.parse_line(string) # => ["foo", "0"]
|
1901
|
+
#
|
1902
|
+
# Parse the first line from a File object:
|
1903
|
+
# File.open(path) do |file|
|
1904
|
+
# CSV.parse_line(file) # => ["foo", "0"]
|
1905
|
+
# end # => ["foo", "0"]
|
1906
|
+
#
|
1907
|
+
# Returns `nil` if the argument is an empty String:
|
1908
|
+
# CSV.parse_line('') # => nil
|
1909
|
+
#
|
1910
|
+
# ###### With Option `headers`
|
1911
|
+
#
|
1912
|
+
# With {option `headers`[}](#class-CSV-label-Option+headers), returns the first
|
1913
|
+
# row as a CSV::Row object.
|
1914
|
+
#
|
1915
|
+
# These examples assume prior execution of:
|
1916
|
+
# string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
|
1917
|
+
# path = 't.csv'
|
1918
|
+
# File.write(path, string)
|
1919
|
+
#
|
1920
|
+
# Parse the first line from a String object:
|
1921
|
+
# CSV.parse_line(string, headers: true) # => #<CSV::Row "Name":"foo" "Count":"0">
|
1922
|
+
#
|
1923
|
+
# Parse the first line from a File object:
|
1924
|
+
# File.open(path) do |file|
|
1925
|
+
# CSV.parse_line(file, headers: true)
|
1926
|
+
# end # => #<CSV::Row "Name":"foo" "Count":"0">
|
1927
|
+
#
|
1928
|
+
# ---
|
1929
|
+
#
|
1930
|
+
# Raises an exception if the argument is `nil`:
|
1931
|
+
# # Raises ArgumentError (Cannot parse nil as CSV):
|
1932
|
+
# CSV.parse_line(nil)
|
1933
|
+
#
|
1934
|
+
def self.parse_line: (String str, ?::Hash[Symbol, untyped] options) -> ::Array[String?]?
|
1935
|
+
|
1936
|
+
# <!--
|
1937
|
+
# rdoc-file=lib/csv.rb
|
1938
|
+
# - csv.read -> array or csv_table
|
1939
|
+
# -->
|
1940
|
+
# Forms the remaining rows from `self` into:
|
1941
|
+
# * A CSV::Table object, if headers are in use.
|
1942
|
+
# * An Array of Arrays, otherwise.
|
1943
|
+
#
|
1944
|
+
# The data source must be opened for reading.
|
1945
|
+
#
|
1946
|
+
# Without headers:
|
1947
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1948
|
+
# path = 't.csv'
|
1949
|
+
# File.write(path, string)
|
1950
|
+
# csv = CSV.open(path)
|
1951
|
+
# csv.read # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1952
|
+
#
|
1953
|
+
# With headers:
|
1954
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1955
|
+
# path = 't.csv'
|
1956
|
+
# File.write(path, string)
|
1957
|
+
# csv = CSV.open(path, headers: true)
|
1958
|
+
# csv.read # => #<CSV::Table mode:col_or_row row_count:4>
|
1959
|
+
#
|
1960
|
+
# ---
|
1961
|
+
#
|
1962
|
+
# Raises an exception if the source is not opened for reading:
|
1963
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1964
|
+
# csv = CSV.new(string)
|
1965
|
+
# csv.close
|
1966
|
+
# # Raises IOError (not opened for reading)
|
1967
|
+
# csv.read
|
1968
|
+
#
|
1969
|
+
def read: () -> ::Array[::Array[String?]]
|
1970
|
+
|
1971
|
+
# <!--
|
1972
|
+
# rdoc-file=lib/csv.rb
|
1973
|
+
# - readline()
|
1974
|
+
# -->
|
1975
|
+
#
|
1976
|
+
def readline: () -> ::Array[String?]?
|
1977
|
+
|
1978
|
+
# <!--
|
1979
|
+
# rdoc-file=lib/csv.rb
|
1980
|
+
# - read(source, **options) -> array_of_arrays
|
1981
|
+
# - read(source, headers: true, **options) -> csv_table
|
1982
|
+
# -->
|
1983
|
+
# Opens the given `source` with the given `options` (see CSV.open), reads the
|
1984
|
+
# source (see CSV#read), and returns the result, which will be either an Array
|
1985
|
+
# of Arrays or a CSV::Table.
|
1986
|
+
#
|
1987
|
+
# Without headers:
|
1988
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
1989
|
+
# path = 't.csv'
|
1990
|
+
# File.write(path, string)
|
1991
|
+
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
1992
|
+
#
|
1993
|
+
# With headers:
|
1994
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
1995
|
+
# path = 't.csv'
|
1996
|
+
# File.write(path, string)
|
1997
|
+
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
|
1998
|
+
#
|
1999
|
+
def self.read: (String | IO path, headers: true | :first_row | Array[untyped] | String, **untyped options) -> ::CSV::Table[CSV::Row]
|
2000
|
+
| (String | IO path, ?::Hash[Symbol, untyped] options) -> ::Array[::Array[String?]]
|
2001
|
+
|
2002
|
+
# <!--
|
2003
|
+
# rdoc-file=lib/csv.rb
|
2004
|
+
# - csv << row -> self
|
2005
|
+
# -->
|
2006
|
+
# Appends a row to `self`.
|
2007
|
+
#
|
2008
|
+
# * Argument `row` must be an Array object or a CSV::Row object.
|
2009
|
+
# * The output stream must be open for writing.
|
2010
|
+
#
|
2011
|
+
# ---
|
2012
|
+
#
|
2013
|
+
# Append Arrays:
|
2014
|
+
# CSV.generate do |csv|
|
2015
|
+
# csv << ['foo', 0]
|
2016
|
+
# csv << ['bar', 1]
|
2017
|
+
# csv << ['baz', 2]
|
2018
|
+
# end # => "foo,0\nbar,1\nbaz,2\n"
|
2019
|
+
#
|
2020
|
+
# Append CSV::Rows:
|
2021
|
+
# headers = []
|
2022
|
+
# CSV.generate do |csv|
|
2023
|
+
# csv << CSV::Row.new(headers, ['foo', 0])
|
2024
|
+
# csv << CSV::Row.new(headers, ['bar', 1])
|
2025
|
+
# csv << CSV::Row.new(headers, ['baz', 2])
|
2026
|
+
# end # => "foo,0\nbar,1\nbaz,2\n"
|
2027
|
+
#
|
2028
|
+
# Headers in CSV::Row objects are not appended:
|
2029
|
+
# headers = ['Name', 'Count']
|
2030
|
+
# CSV.generate do |csv|
|
2031
|
+
# csv << CSV::Row.new(headers, ['foo', 0])
|
2032
|
+
# csv << CSV::Row.new(headers, ['bar', 1])
|
2033
|
+
# csv << CSV::Row.new(headers, ['baz', 2])
|
2034
|
+
# end # => "foo,0\nbar,1\nbaz,2\n"
|
2035
|
+
#
|
2036
|
+
# ---
|
2037
|
+
#
|
2038
|
+
# Raises an exception if `row` is not an Array or CSV::Row:
|
2039
|
+
# CSV.generate do |csv|
|
2040
|
+
# # Raises NoMethodError (undefined method `collect' for :foo:Symbol)
|
2041
|
+
# csv << :foo
|
2042
|
+
# end
|
2043
|
+
#
|
2044
|
+
# Raises an exception if the output stream is not opened for writing:
|
2045
|
+
# path = 't.csv'
|
2046
|
+
# File.write(path, '')
|
2047
|
+
# File.open(path) do |file|
|
2048
|
+
# CSV.open(file) do |csv|
|
2049
|
+
# # Raises IOError (not opened for writing)
|
2050
|
+
# csv << ['foo', 0]
|
2051
|
+
# end
|
2052
|
+
# end
|
2053
|
+
#
|
2054
|
+
def <<: (::Array[untyped] | CSV::Row row) -> void
|
2055
|
+
|
2056
|
+
# <!--
|
2057
|
+
# rdoc-file=lib/csv.rb
|
2058
|
+
# - generate(csv_string, **options) {|csv| ... }
|
2059
|
+
# - generate(**options) {|csv| ... }
|
2060
|
+
# -->
|
2061
|
+
# * Argument `csv_string`, if given, must be a String object; defaults to a
|
2062
|
+
# new empty String.
|
2063
|
+
# * Arguments `options`, if given, should be generating options. See [Options
|
2064
|
+
# for Generating](#class-CSV-label-Options+for+Generating).
|
2065
|
+
#
|
2066
|
+
# ---
|
2067
|
+
#
|
2068
|
+
# Creates a new CSV object via `CSV.new(csv_string, **options)`; calls the block
|
2069
|
+
# with the CSV object, which the block may modify; returns the String generated
|
2070
|
+
# from the CSV object.
|
2071
|
+
#
|
2072
|
+
# Note that a passed String **is** modified by this method. Pass
|
2073
|
+
# `csv_string`.dup if the String must be preserved.
|
2074
|
+
#
|
2075
|
+
# This method has one additional option: `:encoding`, which sets the base
|
2076
|
+
# Encoding for the output if no no `str` is specified. CSV needs this hint if
|
2077
|
+
# you plan to output non-ASCII compatible data.
|
2078
|
+
#
|
2079
|
+
# ---
|
2080
|
+
#
|
2081
|
+
# Add lines:
|
2082
|
+
# input_string = "foo,0\nbar,1\nbaz,2\n"
|
2083
|
+
# output_string = CSV.generate(input_string) do |csv|
|
2084
|
+
# csv << ['bat', 3]
|
2085
|
+
# csv << ['bam', 4]
|
2086
|
+
# end
|
2087
|
+
# output_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n"
|
2088
|
+
# input_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n"
|
2089
|
+
# output_string.equal?(input_string) # => true # Same string, modified
|
2090
|
+
#
|
2091
|
+
# Add lines into new string, preserving old string:
|
2092
|
+
# input_string = "foo,0\nbar,1\nbaz,2\n"
|
2093
|
+
# output_string = CSV.generate(input_string.dup) do |csv|
|
2094
|
+
# csv << ['bat', 3]
|
2095
|
+
# csv << ['bam', 4]
|
2096
|
+
# end
|
2097
|
+
# output_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n"
|
2098
|
+
# input_string # => "foo,0\nbar,1\nbaz,2\n"
|
2099
|
+
# output_string.equal?(input_string) # => false # Different strings
|
2100
|
+
#
|
2101
|
+
# Create lines from nothing:
|
2102
|
+
# output_string = CSV.generate do |csv|
|
2103
|
+
# csv << ['foo', 0]
|
2104
|
+
# csv << ['bar', 1]
|
2105
|
+
# csv << ['baz', 2]
|
2106
|
+
# end
|
2107
|
+
# output_string # => "foo,0\nbar,1\nbaz,2\n"
|
2108
|
+
#
|
2109
|
+
# ---
|
2110
|
+
#
|
2111
|
+
# Raises an exception if `csv_string` is not a String object:
|
2112
|
+
# # Raises TypeError (no implicit conversion of Integer into String)
|
2113
|
+
# CSV.generate(0)
|
2114
|
+
#
|
2115
|
+
def self.generate: (?String str, **untyped options) { (CSV csv) -> void } -> String
|
2116
|
+
|
2117
|
+
# <!--
|
2118
|
+
# rdoc-file=lib/csv.rb
|
2119
|
+
# - csv.each -> enumerator
|
2120
|
+
# - csv.each {|row| ...}
|
2121
|
+
# -->
|
2122
|
+
# Calls the block with each successive row. The data source must be opened for
|
2123
|
+
# reading.
|
2124
|
+
#
|
2125
|
+
# Without headers:
|
2126
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
2127
|
+
# csv = CSV.new(string)
|
2128
|
+
# csv.each do |row|
|
2129
|
+
# p row
|
2130
|
+
# end
|
2131
|
+
#
|
2132
|
+
# Output:
|
2133
|
+
# ["foo", "0"]
|
2134
|
+
# ["bar", "1"]
|
2135
|
+
# ["baz", "2"]
|
2136
|
+
#
|
2137
|
+
# With headers:
|
2138
|
+
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2139
|
+
# csv = CSV.new(string, headers: true)
|
2140
|
+
# csv.each do |row|
|
2141
|
+
# p row
|
2142
|
+
# end
|
2143
|
+
#
|
2144
|
+
# Output:
|
2145
|
+
# <CSV::Row "Name":"foo" "Value":"0">
|
2146
|
+
# <CSV::Row "Name":"bar" "Value":"1">
|
2147
|
+
# <CSV::Row "Name":"baz" "Value":"2">
|
2148
|
+
#
|
2149
|
+
# ---
|
2150
|
+
#
|
2151
|
+
# Raises an exception if the source is not opened for reading:
|
2152
|
+
# string = "foo,0\nbar,1\nbaz,2\n"
|
2153
|
+
# csv = CSV.new(string)
|
2154
|
+
# csv.close
|
2155
|
+
# # Raises IOError (not opened for reading)
|
2156
|
+
# csv.each do |row|
|
2157
|
+
# p row
|
2158
|
+
# end
|
2159
|
+
#
|
2160
|
+
def each: () -> Enumerator[untyped, Integer]
|
2161
|
+
| () { (untyped) -> void } -> Integer
|
2162
|
+
|
2163
|
+
# <!--
|
2164
|
+
# rdoc-file=lib/csv.rb
|
2165
|
+
# - csv.headers -> object
|
2166
|
+
# -->
|
2167
|
+
# Returns the value that determines whether headers are used; used for parsing;
|
2168
|
+
# see {Option `headers`[}](#class-CSV-label-Option+headers):
|
2169
|
+
# CSV.new('').headers # => nil
|
2170
|
+
#
|
2171
|
+
def headers: () -> (Array[String] | true | nil)
|
2172
|
+
end
|
2173
|
+
|
2174
|
+
# <!-- rdoc-file=lib/csv.rb -->
|
2175
|
+
# Default values for method options.
|
2176
|
+
#
|
2177
|
+
CSV::DEFAULT_OPTIONS: ::Hash[untyped, untyped]
|
2178
|
+
|
2179
|
+
# <!-- rdoc-file=lib/csv/version.rb -->
|
2180
|
+
# The version of the installed library.
|
2181
|
+
#
|
2182
|
+
CSV::VERSION: String
|
2183
|
+
|
2184
|
+
# <!-- rdoc-file=lib/csv/row.rb -->
|
2185
|
+
# # CSV::Row
|
2186
|
+
# A CSV::Row instance represents a CSV table row. (see [class
|
2187
|
+
# CSV](../CSV.html)).
|
2188
|
+
#
|
2189
|
+
# The instance may have:
|
2190
|
+
# * Fields: each is an object, not necessarily a String.
|
2191
|
+
# * Headers: each serves a key, and also need not be a String.
|
2192
|
+
#
|
2193
|
+
# ### Instance Methods
|
2194
|
+
#
|
2195
|
+
# CSV::Row has three groups of instance methods:
|
2196
|
+
# * Its own internally defined instance methods.
|
2197
|
+
# * Methods included by module Enumerable.
|
2198
|
+
# * Methods delegated to class Array.:
|
2199
|
+
# * Array#empty?
|
2200
|
+
# * Array#length
|
2201
|
+
# * Array#size
|
2202
|
+
#
|
2203
|
+
# ## Creating a CSV::Row Instance
|
2204
|
+
#
|
2205
|
+
# Commonly, a new CSV::Row instance is created by parsing CSV source that has
|
2206
|
+
# headers:
|
2207
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2208
|
+
# table = CSV.parse(source, headers: true)
|
2209
|
+
# table.each {|row| p row }
|
2210
|
+
#
|
2211
|
+
# Output:
|
2212
|
+
# #<CSV::Row "Name":"foo" "Value":"0">
|
2213
|
+
# #<CSV::Row "Name":"bar" "Value":"1">
|
2214
|
+
# #<CSV::Row "Name":"baz" "Value":"2">
|
2215
|
+
#
|
2216
|
+
# You can also create a row directly. See ::new.
|
2217
|
+
#
|
2218
|
+
# ## Headers
|
2219
|
+
#
|
2220
|
+
# Like a CSV::Table, a CSV::Row has headers.
|
2221
|
+
#
|
2222
|
+
# A CSV::Row that was created by parsing CSV source inherits its headers from
|
2223
|
+
# the table:
|
2224
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2225
|
+
# table = CSV.parse(source, headers: true)
|
2226
|
+
# row = table.first
|
2227
|
+
# row.headers # => ["Name", "Value"]
|
2228
|
+
#
|
2229
|
+
# You can also create a new row with headers; like the keys in a Hash, the
|
2230
|
+
# headers need not be Strings:
|
2231
|
+
# row = CSV::Row.new([:name, :value], ['foo', 0])
|
2232
|
+
# row.headers # => [:name, :value]
|
2233
|
+
#
|
2234
|
+
# The new row retains its headers even if added to a table that has headers:
|
2235
|
+
# table << row # => #<CSV::Table mode:col_or_row row_count:5>
|
2236
|
+
# row.headers # => [:name, :value]
|
2237
|
+
# row[:name] # => "foo"
|
2238
|
+
# row['Name'] # => nil
|
2239
|
+
#
|
2240
|
+
# ## Accessing Fields
|
2241
|
+
#
|
2242
|
+
# You may access a field in a CSV::Row with either its Integer index
|
2243
|
+
# (Array-style) or its header (Hash-style).
|
2244
|
+
#
|
2245
|
+
# Fetch a field using method #[]:
|
2246
|
+
# row = CSV::Row.new(['Name', 'Value'], ['foo', 0])
|
2247
|
+
# row[1] # => 0
|
2248
|
+
# row['Value'] # => 0
|
2249
|
+
#
|
2250
|
+
# Set a field using method #[]=:
|
2251
|
+
# row = CSV::Row.new(['Name', 'Value'], ['foo', 0])
|
2252
|
+
# row # => #<CSV::Row "Name":"foo" "Value":0>
|
2253
|
+
# row[0] = 'bar'
|
2254
|
+
# row['Value'] = 1
|
2255
|
+
# row # => #<CSV::Row "Name":"bar" "Value":1>
|
2256
|
+
#
|
2257
|
+
class CSV::Row < Object
|
2258
|
+
include Enumerable[Array[String]]
|
2259
|
+
extend Forwardable
|
2260
|
+
|
2261
|
+
# <!--
|
2262
|
+
# rdoc-file=lib/csv/row.rb
|
2263
|
+
# - CSV::Row.new(headers, fields, header_row = false) -> csv_row
|
2264
|
+
# -->
|
2265
|
+
# Returns the new CSV::Row instance constructed from arguments `headers` and
|
2266
|
+
# `fields`; both should be Arrays; note that the fields need not be Strings:
|
2267
|
+
# row = CSV::Row.new(['Name', 'Value'], ['foo', 0])
|
2268
|
+
# row # => #<CSV::Row "Name":"foo" "Value":0>
|
2269
|
+
#
|
2270
|
+
# If the Array lengths are different, the shorter is `nil`-filled:
|
2271
|
+
# row = CSV::Row.new(['Name', 'Value', 'Date', 'Size'], ['foo', 0])
|
2272
|
+
# row # => #<CSV::Row "Name":"foo" "Value":0 "Date":nil "Size":nil>
|
2273
|
+
#
|
2274
|
+
# Each CSV::Row object is either a *field row* or a *header row*; by default, a
|
2275
|
+
# new row is a field row; for the row created above:
|
2276
|
+
# row.field_row? # => true
|
2277
|
+
# row.header_row? # => false
|
2278
|
+
#
|
2279
|
+
# If the optional argument `header_row` is given as `true`, the created row is a
|
2280
|
+
# header row:
|
2281
|
+
# row = CSV::Row.new(['Name', 'Value'], ['foo', 0], header_row = true)
|
2282
|
+
# row # => #<CSV::Row "Name":"foo" "Value":0>
|
2283
|
+
# row.field_row? # => false
|
2284
|
+
# row.header_row? # => true
|
2285
|
+
#
|
2286
|
+
def initialize: (Array[untyped] headers, Array[untyped] fields, ?header_row: bool) -> void
|
2287
|
+
|
2288
|
+
# <!--
|
2289
|
+
# rdoc-file=lib/csv/row.rb
|
2290
|
+
# - row << [header, value] -> self
|
2291
|
+
# - row << hash -> self
|
2292
|
+
# - row << value -> self
|
2293
|
+
# -->
|
2294
|
+
# Adds a field to `self`; returns `self`:
|
2295
|
+
#
|
2296
|
+
# If the argument is a 2-element Array `[header, value]`, a field is added with
|
2297
|
+
# the given `header` and `value`:
|
2298
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2299
|
+
# table = CSV.parse(source, headers: true)
|
2300
|
+
# row = table[0]
|
2301
|
+
# row << ['NAME', 'Bat']
|
2302
|
+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz" "NAME":"Bat">
|
2303
|
+
#
|
2304
|
+
# If the argument is a Hash, each `key-value` pair is added as a field with
|
2305
|
+
# header `key` and value `value`.
|
2306
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2307
|
+
# table = CSV.parse(source, headers: true)
|
2308
|
+
# row = table[0]
|
2309
|
+
# row << {NAME: 'Bat', name: 'Bam'}
|
2310
|
+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz" NAME:"Bat" name:"Bam">
|
2311
|
+
#
|
2312
|
+
# Otherwise, the given `value` is added as a field with no header.
|
2313
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2314
|
+
# table = CSV.parse(source, headers: true)
|
2315
|
+
# row = table[0]
|
2316
|
+
# row << 'Bag'
|
2317
|
+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz" nil:"Bag">
|
2318
|
+
#
|
2319
|
+
def <<: (untyped arg) -> untyped
|
2320
|
+
|
2321
|
+
# <!--
|
2322
|
+
# rdoc-file=lib/csv/row.rb
|
2323
|
+
# - row == other -> true or false
|
2324
|
+
# -->
|
2325
|
+
# Returns `true` if `other` is a /CSV::Row that has the same fields (headers and
|
2326
|
+
# values) in the same order as `self`; otherwise returns `false`:
|
2327
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2328
|
+
# table = CSV.parse(source, headers: true)
|
2329
|
+
# row = table[0]
|
2330
|
+
# other_row = table[0]
|
2331
|
+
# row == other_row # => true
|
2332
|
+
# other_row = table[1]
|
2333
|
+
# row == other_row # => false
|
2334
|
+
#
|
2335
|
+
def ==: (untyped other) -> bool
|
2336
|
+
|
2337
|
+
# <!--
|
2338
|
+
# rdoc-file=lib/csv/row.rb
|
2339
|
+
# - [](header_or_index, minimum_index = 0)
|
2340
|
+
# -->
|
2341
|
+
#
|
2342
|
+
alias [] field
|
2343
|
+
|
2344
|
+
# <!--
|
2345
|
+
# rdoc-file=lib/csv/row.rb
|
2346
|
+
# - row[index] = value -> value
|
2347
|
+
# - row[header, offset] = value -> value
|
2348
|
+
# - row[header] = value -> value
|
2349
|
+
# -->
|
2350
|
+
# Assigns the field value for the given `index` or `header`; returns `value`.
|
2351
|
+
#
|
2352
|
+
# ---
|
2353
|
+
#
|
2354
|
+
# Assign field value by Integer index:
|
2355
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2356
|
+
# table = CSV.parse(source, headers: true)
|
2357
|
+
# row = table[0]
|
2358
|
+
# row[0] = 'Bat'
|
2359
|
+
# row[1] = 3
|
2360
|
+
# row # => #<CSV::Row "Name":"Bat" "Value":3>
|
2361
|
+
#
|
2362
|
+
# Counts backward from the last column if `index` is negative:
|
2363
|
+
# row[-1] = 4
|
2364
|
+
# row[-2] = 'Bam'
|
2365
|
+
# row # => #<CSV::Row "Name":"Bam" "Value":4>
|
2366
|
+
#
|
2367
|
+
# Extends the row with `nil:nil` if positive `index` is not in the row:
|
2368
|
+
# row[4] = 5
|
2369
|
+
# row # => #<CSV::Row "Name":"bad" "Value":4 nil:nil nil:nil nil:5>
|
2370
|
+
#
|
2371
|
+
# Raises IndexError if negative `index` is too small (too far from zero).
|
2372
|
+
#
|
2373
|
+
# ---
|
2374
|
+
#
|
2375
|
+
# Assign field value by header (first found):
|
2376
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2377
|
+
# table = CSV.parse(source, headers: true)
|
2378
|
+
# row = table[0]
|
2379
|
+
# row['Name'] = 'Bat'
|
2380
|
+
# row # => #<CSV::Row "Name":"Bat" "Name":"Bar" "Name":"Baz">
|
2381
|
+
#
|
2382
|
+
# Assign field value by header, ignoring `offset` leading fields:
|
2383
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2384
|
+
# table = CSV.parse(source, headers: true)
|
2385
|
+
# row = table[0]
|
2386
|
+
# row['Name', 2] = 4
|
2387
|
+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":4>
|
2388
|
+
#
|
2389
|
+
# Append new field by (new) header:
|
2390
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2391
|
+
# table = CSV.parse(source, headers: true)
|
2392
|
+
# row = table[0]
|
2393
|
+
# row['New'] = 6
|
2394
|
+
# row# => #<CSV::Row "Name":"foo" "Value":"0" "New":6>
|
2395
|
+
#
|
2396
|
+
def []=: (*untyped args) -> untyped
|
2397
|
+
|
2398
|
+
# <!--
|
2399
|
+
# rdoc-file=lib/csv/row.rb
|
2400
|
+
# - delete(index) -> [header, value] or nil
|
2401
|
+
# - delete(header) -> [header, value] or empty_array
|
2402
|
+
# - delete(header, offset) -> [header, value] or empty_array
|
2403
|
+
# -->
|
2404
|
+
# Removes a specified field from `self`; returns the 2-element Array `[header,
|
2405
|
+
# value]` if the field exists.
|
2406
|
+
#
|
2407
|
+
# If an Integer argument `index` is given, removes and returns the field at
|
2408
|
+
# offset `index`, or returns `nil` if the field does not exist:
|
2409
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2410
|
+
# table = CSV.parse(source, headers: true)
|
2411
|
+
# row = table[0]
|
2412
|
+
# row.delete(1) # => ["Name", "Bar"]
|
2413
|
+
# row.delete(50) # => nil
|
2414
|
+
#
|
2415
|
+
# Otherwise, if the single argument `header` is given, removes and returns the
|
2416
|
+
# first-found field with the given header, of returns a new empty Array if the
|
2417
|
+
# field does not exist:
|
2418
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2419
|
+
# table = CSV.parse(source, headers: true)
|
2420
|
+
# row = table[0]
|
2421
|
+
# row.delete('Name') # => ["Name", "Foo"]
|
2422
|
+
# row.delete('NAME') # => []
|
2423
|
+
#
|
2424
|
+
# If argument `header` and Integer argument `offset` are given, removes and
|
2425
|
+
# returns the first-found field with the given header whose `index` is at least
|
2426
|
+
# as large as `offset`:
|
2427
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2428
|
+
# table = CSV.parse(source, headers: true)
|
2429
|
+
# row = table[0]
|
2430
|
+
# row.delete('Name', 1) # => ["Name", "Bar"]
|
2431
|
+
# row.delete('NAME', 1) # => []
|
2432
|
+
#
|
2433
|
+
def delete: (untyped header_or_index, ?untyped minimum_index) -> untyped
|
2434
|
+
|
2435
|
+
# <!--
|
2436
|
+
# rdoc-file=lib/csv/row.rb
|
2437
|
+
# - row.delete_if {|header, value| ... } -> self
|
2438
|
+
# -->
|
2439
|
+
# Removes fields from `self` as selected by the block; returns `self`.
|
2440
|
+
#
|
2441
|
+
# Removes each field for which the block returns a truthy value:
|
2442
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2443
|
+
# table = CSV.parse(source, headers: true)
|
2444
|
+
# row = table[0]
|
2445
|
+
# row.delete_if {|header, value| value.start_with?('B') } # => true
|
2446
|
+
# row # => #<CSV::Row "Name":"Foo">
|
2447
|
+
# row.delete_if {|header, value| header.start_with?('B') } # => false
|
2448
|
+
#
|
2449
|
+
# If no block is given, returns a new Enumerator:
|
2450
|
+
# row.delete_if # => #<Enumerator: #<CSV::Row "Name":"Foo">:delete_if>
|
2451
|
+
#
|
2452
|
+
def delete_if: () { (*untyped) -> untyped } -> untyped
|
2453
|
+
|
2454
|
+
# <!--
|
2455
|
+
# rdoc-file=lib/csv/row.rb
|
2456
|
+
# - row.dig(index_or_header, *identifiers) -> object
|
2457
|
+
# -->
|
2458
|
+
# Finds and returns the object in nested object that is specified by
|
2459
|
+
# `index_or_header` and `specifiers`.
|
2460
|
+
#
|
2461
|
+
# The nested objects may be instances of various classes. See [Dig
|
2462
|
+
# Methods](rdoc-ref:dig_methods.rdoc).
|
2463
|
+
#
|
2464
|
+
# Examples:
|
2465
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2466
|
+
# table = CSV.parse(source, headers: true)
|
2467
|
+
# row = table[0]
|
2468
|
+
# row.dig(1) # => "0"
|
2469
|
+
# row.dig('Value') # => "0"
|
2470
|
+
# row.dig(5) # => nil
|
2471
|
+
#
|
2472
|
+
def dig: (untyped index_or_header, *untyped indexes) -> untyped
|
2473
|
+
|
2474
|
+
# <!--
|
2475
|
+
# rdoc-file=lib/csv/row.rb
|
2476
|
+
# - row.each {|header, value| ... } -> self
|
2477
|
+
# -->
|
2478
|
+
# Calls the block with each header-value pair; returns `self`:
|
2479
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2480
|
+
# table = CSV.parse(source, headers: true)
|
2481
|
+
# row = table[0]
|
2482
|
+
# row.each {|header, value| p [header, value] }
|
2483
|
+
#
|
2484
|
+
# Output:
|
2485
|
+
# ["Name", "Foo"]
|
2486
|
+
# ["Name", "Bar"]
|
2487
|
+
# ["Name", "Baz"]
|
2488
|
+
#
|
2489
|
+
# If no block is given, returns a new Enumerator:
|
2490
|
+
# row.each # => #<Enumerator: #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz">:each>
|
2491
|
+
#
|
2492
|
+
def each: () -> Enumerator[Array[String], self]
|
2493
|
+
| () { (Array[String]) -> void } -> self
|
2494
|
+
|
2495
|
+
# <!--
|
2496
|
+
# rdoc-file=lib/csv/row.rb
|
2497
|
+
# - each_pair(&block)
|
2498
|
+
# -->
|
2499
|
+
#
|
2500
|
+
alias each_pair each
|
2501
|
+
|
2502
|
+
def empty?: (*untyped args) { (*untyped) -> untyped } -> bool
|
2503
|
+
|
2504
|
+
# <!--
|
2505
|
+
# rdoc-file=lib/csv/row.rb
|
2506
|
+
# - fetch(header) -> value
|
2507
|
+
# - fetch(header, default) -> value
|
2508
|
+
# - fetch(header) {|row| ... } -> value
|
2509
|
+
# -->
|
2510
|
+
# Returns the field value as specified by `header`.
|
2511
|
+
#
|
2512
|
+
# ---
|
2513
|
+
#
|
2514
|
+
# With the single argument `header`, returns the field value for that header
|
2515
|
+
# (first found):
|
2516
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2517
|
+
# table = CSV.parse(source, headers: true)
|
2518
|
+
# row = table[0]
|
2519
|
+
# row.fetch('Name') # => "Foo"
|
2520
|
+
#
|
2521
|
+
# Raises exception `KeyError` if the header does not exist.
|
2522
|
+
#
|
2523
|
+
# ---
|
2524
|
+
#
|
2525
|
+
# With arguments `header` and `default` given, returns the field value for the
|
2526
|
+
# header (first found) if the header exists, otherwise returns `default`:
|
2527
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2528
|
+
# table = CSV.parse(source, headers: true)
|
2529
|
+
# row = table[0]
|
2530
|
+
# row.fetch('Name', '') # => "Foo"
|
2531
|
+
# row.fetch(:nosuch, '') # => ""
|
2532
|
+
#
|
2533
|
+
# ---
|
2534
|
+
#
|
2535
|
+
# With argument `header` and a block given, returns the field value for the
|
2536
|
+
# header (first found) if the header exists; otherwise calls the block and
|
2537
|
+
# returns its return value:
|
2538
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2539
|
+
# table = CSV.parse(source, headers: true)
|
2540
|
+
# row = table[0]
|
2541
|
+
# row.fetch('Name') {|header| fail 'Cannot happen' } # => "Foo"
|
2542
|
+
# row.fetch(:nosuch) {|header| "Header '#{header} not found'" } # => "Header 'nosuch not found'"
|
2543
|
+
#
|
2544
|
+
def fetch: (untyped header, *untyped varargs) ?{ (*untyped) -> untyped } -> untyped
|
2545
|
+
|
2546
|
+
# <!--
|
2547
|
+
# rdoc-file=lib/csv/row.rb
|
2548
|
+
# - field(index) -> value
|
2549
|
+
# - field(header) -> value
|
2550
|
+
# - field(header, offset) -> value
|
2551
|
+
# -->
|
2552
|
+
# Returns the field value for the given `index` or `header`.
|
2553
|
+
#
|
2554
|
+
# ---
|
2555
|
+
#
|
2556
|
+
# Fetch field value by Integer index:
|
2557
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2558
|
+
# table = CSV.parse(source, headers: true)
|
2559
|
+
# row = table[0]
|
2560
|
+
# row.field(0) # => "foo"
|
2561
|
+
# row.field(1) # => "bar"
|
2562
|
+
#
|
2563
|
+
# Counts backward from the last column if `index` is negative:
|
2564
|
+
# row.field(-1) # => "0"
|
2565
|
+
# row.field(-2) # => "foo"
|
2566
|
+
#
|
2567
|
+
# Returns `nil` if `index` is out of range:
|
2568
|
+
# row.field(2) # => nil
|
2569
|
+
# row.field(-3) # => nil
|
2570
|
+
#
|
2571
|
+
# ---
|
2572
|
+
#
|
2573
|
+
# Fetch field value by header (first found):
|
2574
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2575
|
+
# table = CSV.parse(source, headers: true)
|
2576
|
+
# row = table[0]
|
2577
|
+
# row.field('Name') # => "Foo"
|
2578
|
+
#
|
2579
|
+
# Fetch field value by header, ignoring `offset` leading fields:
|
2580
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2581
|
+
# table = CSV.parse(source, headers: true)
|
2582
|
+
# row = table[0]
|
2583
|
+
# row.field('Name', 2) # => "Baz"
|
2584
|
+
#
|
2585
|
+
# Returns `nil` if the header does not exist.
|
2586
|
+
#
|
2587
|
+
def field: (untyped header_or_index, ?untyped minimum_index) -> untyped
|
2588
|
+
|
2589
|
+
# <!--
|
2590
|
+
# rdoc-file=lib/csv/row.rb
|
2591
|
+
# - row.field?(value) -> true or false
|
2592
|
+
# -->
|
2593
|
+
# Returns `true` if `value` is a field in this row, `false` otherwise:
|
2594
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2595
|
+
# table = CSV.parse(source, headers: true)
|
2596
|
+
# row = table[0]
|
2597
|
+
# row.field?('Bar') # => true
|
2598
|
+
# row.field?('BAR') # => false
|
2599
|
+
#
|
2600
|
+
def field?: (untyped data) -> bool
|
2601
|
+
|
2602
|
+
# <!--
|
2603
|
+
# rdoc-file=lib/csv/row.rb
|
2604
|
+
# - row.field_row? -> true or false
|
2605
|
+
# -->
|
2606
|
+
# Returns `true` if this is a field row, `false` otherwise.
|
2607
|
+
#
|
2608
|
+
def field_row?: () -> bool
|
2609
|
+
|
2610
|
+
# <!--
|
2611
|
+
# rdoc-file=lib/csv/row.rb
|
2612
|
+
# - self.fields(*specifiers) -> array_of_fields
|
2613
|
+
# -->
|
2614
|
+
# Returns field values per the given `specifiers`, which may be any mixture of:
|
2615
|
+
# * Integer index.
|
2616
|
+
# * Range of Integer indexes.
|
2617
|
+
# * 2-element Array containing a header and offset.
|
2618
|
+
# * Header.
|
2619
|
+
# * Range of headers.
|
2620
|
+
#
|
2621
|
+
# For `specifier` in one of the first four cases above, returns the result of
|
2622
|
+
# `self.field(specifier)`; see #field.
|
2623
|
+
#
|
2624
|
+
# Although there may be any number of `specifiers`, the examples here will
|
2625
|
+
# illustrate one at a time.
|
2626
|
+
#
|
2627
|
+
# When the specifier is an Integer `index`, returns `self.field(index)`L
|
2628
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2629
|
+
# table = CSV.parse(source, headers: true)
|
2630
|
+
# row = table[0]
|
2631
|
+
# row.fields(1) # => ["Bar"]
|
2632
|
+
#
|
2633
|
+
# When the specifier is a Range of Integers `range`, returns
|
2634
|
+
# `self.field(range)`:
|
2635
|
+
# row.fields(1..2) # => ["Bar", "Baz"]
|
2636
|
+
#
|
2637
|
+
# When the specifier is a 2-element Array `array`, returns `self.field(array)`L
|
2638
|
+
# row.fields('Name', 1) # => ["Foo", "Bar"]
|
2639
|
+
#
|
2640
|
+
# When the specifier is a header `header`, returns `self.field(header)`L
|
2641
|
+
# row.fields('Name') # => ["Foo"]
|
2642
|
+
#
|
2643
|
+
# When the specifier is a Range of headers `range`, forms a new Range
|
2644
|
+
# `new_range` from the indexes of `range.start` and `range.end`, and returns
|
2645
|
+
# `self.field(new_range)`:
|
2646
|
+
# source = "Name,NAME,name\nFoo,Bar,Baz\n"
|
2647
|
+
# table = CSV.parse(source, headers: true)
|
2648
|
+
# row = table[0]
|
2649
|
+
# row.fields('Name'..'NAME') # => ["Foo", "Bar"]
|
2650
|
+
#
|
2651
|
+
# Returns all fields if no argument given:
|
2652
|
+
# row.fields # => ["Foo", "Bar", "Baz"]
|
2653
|
+
#
|
2654
|
+
def fields: (*untyped headers_and_or_indices) -> untyped
|
2655
|
+
|
2656
|
+
# <!--
|
2657
|
+
# rdoc-file=lib/csv/row.rb
|
2658
|
+
# - row.has_key?(header) -> true or false
|
2659
|
+
# -->
|
2660
|
+
# Returns `true` if there is a field with the given `header`, `false` otherwise.
|
2661
|
+
#
|
2662
|
+
def has_key?: (untyped header) -> bool
|
2663
|
+
|
2664
|
+
# <!--
|
2665
|
+
# rdoc-file=lib/csv/row.rb
|
2666
|
+
# - header?(header)
|
2667
|
+
# -->
|
2668
|
+
#
|
2669
|
+
alias header? has_key?
|
2670
|
+
|
2671
|
+
# <!--
|
2672
|
+
# rdoc-file=lib/csv/row.rb
|
2673
|
+
# - row.header_row? -> true or false
|
2674
|
+
# -->
|
2675
|
+
# Returns `true` if this is a header row, `false` otherwise.
|
2676
|
+
#
|
2677
|
+
def header_row?: () -> bool
|
2678
|
+
|
2679
|
+
# <!--
|
2680
|
+
# rdoc-file=lib/csv/row.rb
|
2681
|
+
# - row.headers -> array_of_headers
|
2682
|
+
# -->
|
2683
|
+
# Returns the headers for this row:
|
2684
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2685
|
+
# table = CSV.parse(source, headers: true)
|
2686
|
+
# row = table.first
|
2687
|
+
# row.headers # => ["Name", "Value"]
|
2688
|
+
#
|
2689
|
+
def headers: () -> untyped
|
2690
|
+
|
2691
|
+
# <!--
|
2692
|
+
# rdoc-file=lib/csv/row.rb
|
2693
|
+
# - include?(header)
|
2694
|
+
# -->
|
2695
|
+
#
|
2696
|
+
alias include? has_key?
|
2697
|
+
|
2698
|
+
# <!--
|
2699
|
+
# rdoc-file=lib/csv/row.rb
|
2700
|
+
# - index(header) -> index
|
2701
|
+
# - index(header, offset) -> index
|
2702
|
+
# -->
|
2703
|
+
# Returns the index for the given header, if it exists; otherwise returns `nil`.
|
2704
|
+
#
|
2705
|
+
# With the single argument `header`, returns the index of the first-found field
|
2706
|
+
# with the given `header`:
|
2707
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2708
|
+
# table = CSV.parse(source, headers: true)
|
2709
|
+
# row = table[0]
|
2710
|
+
# row.index('Name') # => 0
|
2711
|
+
# row.index('NAME') # => nil
|
2712
|
+
#
|
2713
|
+
# With arguments `header` and `offset`, returns the index of the first-found
|
2714
|
+
# field with given `header`, but ignoring the first `offset` fields:
|
2715
|
+
# row.index('Name', 1) # => 1
|
2716
|
+
# row.index('Name', 3) # => nil
|
2717
|
+
#
|
2718
|
+
def index: (untyped header, ?untyped minimum_index) -> untyped
|
2719
|
+
|
2720
|
+
# <!--
|
2721
|
+
# rdoc-file=lib/csv/row.rb
|
2722
|
+
# - row.inspect -> string
|
2723
|
+
# -->
|
2724
|
+
# Returns an ASCII-compatible String showing:
|
2725
|
+
# * Class CSV::Row.
|
2726
|
+
# * Header-value pairs.
|
2727
|
+
# Example:
|
2728
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2729
|
+
# table = CSV.parse(source, headers: true)
|
2730
|
+
# row = table[0]
|
2731
|
+
# row.inspect # => "#<CSV::Row \"Name\":\"foo\" \"Value\":\"0\">"
|
2732
|
+
#
|
2733
|
+
def inspect: () -> String
|
2734
|
+
|
2735
|
+
# <!--
|
2736
|
+
# rdoc-file=lib/csv/row.rb
|
2737
|
+
# - key?(header)
|
2738
|
+
# -->
|
2739
|
+
#
|
2740
|
+
alias key? has_key?
|
2741
|
+
|
2742
|
+
def length: (*untyped args) { (*untyped) -> untyped } -> untyped
|
2743
|
+
|
2744
|
+
# <!--
|
2745
|
+
# rdoc-file=lib/csv/row.rb
|
2746
|
+
# - member?(header)
|
2747
|
+
# -->
|
2748
|
+
#
|
2749
|
+
alias member? has_key?
|
2750
|
+
|
2751
|
+
# <!--
|
2752
|
+
# rdoc-file=lib/csv/row.rb
|
2753
|
+
# - row.push(*values) -> self
|
2754
|
+
# -->
|
2755
|
+
# Appends each of the given `values` to `self` as a field; returns `self`:
|
2756
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2757
|
+
# table = CSV.parse(source, headers: true)
|
2758
|
+
# row = table[0]
|
2759
|
+
# row.push('Bat', 'Bam')
|
2760
|
+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz" nil:"Bat" nil:"Bam">
|
2761
|
+
#
|
2762
|
+
def push: (*untyped args) -> untyped
|
2763
|
+
|
2764
|
+
def size: (*untyped args) { (*untyped) -> untyped } -> untyped
|
2765
|
+
|
2766
|
+
# <!--
|
2767
|
+
# rdoc-file=lib/csv/row.rb
|
2768
|
+
# - row.to_csv -> csv_string
|
2769
|
+
# -->
|
2770
|
+
# Returns the row as a CSV String. Headers are not included:
|
2771
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2772
|
+
# table = CSV.parse(source, headers: true)
|
2773
|
+
# row = table[0]
|
2774
|
+
# row.to_csv # => "foo,0\n"
|
2775
|
+
#
|
2776
|
+
def to_csv: (**untyped) -> untyped
|
2777
|
+
|
2778
|
+
# <!--
|
2779
|
+
# rdoc-file=lib/csv/row.rb
|
2780
|
+
# - row.to_h -> hash
|
2781
|
+
# -->
|
2782
|
+
# Returns the new Hash formed by adding each header-value pair in `self` as a
|
2783
|
+
# key-value pair in the Hash.
|
2784
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2785
|
+
# table = CSV.parse(source, headers: true)
|
2786
|
+
# row = table[0]
|
2787
|
+
# row.to_h # => {"Name"=>"foo", "Value"=>"0"}
|
2788
|
+
#
|
2789
|
+
# Header order is preserved, but repeated headers are ignored:
|
2790
|
+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
|
2791
|
+
# table = CSV.parse(source, headers: true)
|
2792
|
+
# row = table[0]
|
2793
|
+
# row.to_h # => {"Name"=>"Foo"}
|
2794
|
+
#
|
2795
|
+
def to_h: () -> untyped
|
2796
|
+
|
2797
|
+
# <!--
|
2798
|
+
# rdoc-file=lib/csv/row.rb
|
2799
|
+
# - to_hash()
|
2800
|
+
# -->
|
2801
|
+
#
|
2802
|
+
alias to_hash to_h
|
2803
|
+
|
2804
|
+
# <!--
|
2805
|
+
# rdoc-file=lib/csv/row.rb
|
2806
|
+
# - to_s(**options)
|
2807
|
+
# -->
|
2808
|
+
#
|
2809
|
+
alias to_s to_csv
|
2810
|
+
|
2811
|
+
# <!--
|
2812
|
+
# rdoc-file=lib/csv/row.rb
|
2813
|
+
# - values_at(*headers_and_or_indices)
|
2814
|
+
# -->
|
2815
|
+
#
|
2816
|
+
alias values_at fields
|
2817
|
+
end
|
2818
|
+
|
2819
|
+
class CSV::FieldInfo < Struct[untyped]
|
2820
|
+
end
|
2821
|
+
|
2822
|
+
# <!-- rdoc-file=lib/csv.rb -->
|
2823
|
+
# The error thrown when the parser encounters illegal CSV formatting.
|
2824
|
+
#
|
2825
|
+
class CSV::MalformedCSVError < RuntimeError
|
2826
|
+
end
|
2827
|
+
|
2828
|
+
# <!-- rdoc-file=lib/csv/table.rb -->
|
2829
|
+
# # CSV::Table
|
2830
|
+
# A CSV::Table instance represents CSV data. (see [class CSV](../CSV.html)).
|
2831
|
+
#
|
2832
|
+
# The instance may have:
|
2833
|
+
# * Rows: each is a Table::Row object.
|
2834
|
+
# * Headers: names for the columns.
|
2835
|
+
#
|
2836
|
+
# ### Instance Methods
|
2837
|
+
#
|
2838
|
+
# CSV::Table has three groups of instance methods:
|
2839
|
+
# * Its own internally defined instance methods.
|
2840
|
+
# * Methods included by module Enumerable.
|
2841
|
+
# * Methods delegated to class Array.:
|
2842
|
+
# * Array#empty?
|
2843
|
+
# * Array#length
|
2844
|
+
# * Array#size
|
2845
|
+
#
|
2846
|
+
# ## Creating a CSV::Table Instance
|
2847
|
+
#
|
2848
|
+
# Commonly, a new CSV::Table instance is created by parsing CSV source using
|
2849
|
+
# headers:
|
2850
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2851
|
+
# table = CSV.parse(source, headers: true)
|
2852
|
+
# table.class # => CSV::Table
|
2853
|
+
#
|
2854
|
+
# You can also create an instance directly. See ::new.
|
2855
|
+
#
|
2856
|
+
# ## Headers
|
2857
|
+
#
|
2858
|
+
# If a table has headers, the headers serve as labels for the columns of data.
|
2859
|
+
# Each header serves as the label for its column.
|
2860
|
+
#
|
2861
|
+
# The headers for a CSV::Table object are stored as an Array of Strings.
|
2862
|
+
#
|
2863
|
+
# Commonly, headers are defined in the first row of CSV source:
|
2864
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2865
|
+
# table = CSV.parse(source, headers: true)
|
2866
|
+
# table.headers # => ["Name", "Value"]
|
2867
|
+
#
|
2868
|
+
# If no headers are defined, the Array is empty:
|
2869
|
+
# table = CSV::Table.new([])
|
2870
|
+
# table.headers # => []
|
2871
|
+
#
|
2872
|
+
# ## Access Modes
|
2873
|
+
#
|
2874
|
+
# CSV::Table provides three modes for accessing table data:
|
2875
|
+
# * Row mode.
|
2876
|
+
# * Column mode.
|
2877
|
+
# * Mixed mode (the default for a new table).
|
2878
|
+
#
|
2879
|
+
# The access mode for aCSV::Table instance affects the behavior of some of its
|
2880
|
+
# instance methods:
|
2881
|
+
# * #[]
|
2882
|
+
# * #[]=
|
2883
|
+
# * #delete
|
2884
|
+
# * #delete_if
|
2885
|
+
# * #each
|
2886
|
+
# * #values_at
|
2887
|
+
#
|
2888
|
+
# ### Row Mode
|
2889
|
+
#
|
2890
|
+
# Set a table to row mode with method #by_row!:
|
2891
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2892
|
+
# table = CSV.parse(source, headers: true)
|
2893
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
2894
|
+
#
|
2895
|
+
# Specify a single row by an Integer index:
|
2896
|
+
# # Get a row.
|
2897
|
+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
|
2898
|
+
# # Set a row, then get it.
|
2899
|
+
# table[1] = CSV::Row.new(['Name', 'Value'], ['bam', 3])
|
2900
|
+
# table[1] # => #<CSV::Row "Name":"bam" "Value":3>
|
2901
|
+
#
|
2902
|
+
# Specify a sequence of rows by a Range:
|
2903
|
+
# # Get rows.
|
2904
|
+
# table[1..2] # => [#<CSV::Row "Name":"bam" "Value":3>, #<CSV::Row "Name":"baz" "Value":"2">]
|
2905
|
+
# # Set rows, then get them.
|
2906
|
+
# table[1..2] = [
|
2907
|
+
# CSV::Row.new(['Name', 'Value'], ['bat', 4]),
|
2908
|
+
# CSV::Row.new(['Name', 'Value'], ['bad', 5]),
|
2909
|
+
# ]
|
2910
|
+
# table[1..2] # => [["Name", #<CSV::Row "Name":"bat" "Value":4>], ["Value", #<CSV::Row "Name":"bad" "Value":5>]]
|
2911
|
+
#
|
2912
|
+
# ### Column Mode
|
2913
|
+
#
|
2914
|
+
# Set a table to column mode with method #by_col!:
|
2915
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2916
|
+
# table = CSV.parse(source, headers: true)
|
2917
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
2918
|
+
#
|
2919
|
+
# Specify a column by an Integer index:
|
2920
|
+
# # Get a column.
|
2921
|
+
# table[0]
|
2922
|
+
# # Set a column, then get it.
|
2923
|
+
# table[0] = ['FOO', 'BAR', 'BAZ']
|
2924
|
+
# table[0] # => ["FOO", "BAR", "BAZ"]
|
2925
|
+
#
|
2926
|
+
# Specify a column by its String header:
|
2927
|
+
# # Get a column.
|
2928
|
+
# table['Name'] # => ["FOO", "BAR", "BAZ"]
|
2929
|
+
# # Set a column, then get it.
|
2930
|
+
# table['Name'] = ['Foo', 'Bar', 'Baz']
|
2931
|
+
# table['Name'] # => ["Foo", "Bar", "Baz"]
|
2932
|
+
#
|
2933
|
+
# ### Mixed Mode
|
2934
|
+
#
|
2935
|
+
# In mixed mode, you can refer to either rows or columns:
|
2936
|
+
# * An Integer index refers to a row.
|
2937
|
+
# * A Range index refers to multiple rows.
|
2938
|
+
# * A String index refers to a column.
|
2939
|
+
#
|
2940
|
+
# Set a table to mixed mode with method #by_col_or_row!:
|
2941
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
2942
|
+
# table = CSV.parse(source, headers: true)
|
2943
|
+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
|
2944
|
+
#
|
2945
|
+
# Specify a single row by an Integer index:
|
2946
|
+
# # Get a row.
|
2947
|
+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
|
2948
|
+
# # Set a row, then get it.
|
2949
|
+
# table[1] = CSV::Row.new(['Name', 'Value'], ['bam', 3])
|
2950
|
+
# table[1] # => #<CSV::Row "Name":"bam" "Value":3>
|
2951
|
+
#
|
2952
|
+
# Specify a sequence of rows by a Range:
|
2953
|
+
# # Get rows.
|
2954
|
+
# table[1..2] # => [#<CSV::Row "Name":"bam" "Value":3>, #<CSV::Row "Name":"baz" "Value":"2">]
|
2955
|
+
# # Set rows, then get them.
|
2956
|
+
# table[1] = CSV::Row.new(['Name', 'Value'], ['bat', 4])
|
2957
|
+
# table[2] = CSV::Row.new(['Name', 'Value'], ['bad', 5])
|
2958
|
+
# table[1..2] # => [["Name", #<CSV::Row "Name":"bat" "Value":4>], ["Value", #<CSV::Row "Name":"bad" "Value":5>]]
|
2959
|
+
#
|
2960
|
+
# Specify a column by its String header:
|
2961
|
+
# # Get a column.
|
2962
|
+
# table['Name'] # => ["foo", "bat", "bad"]
|
2963
|
+
# # Set a column, then get it.
|
2964
|
+
# table['Name'] = ['Foo', 'Bar', 'Baz']
|
2965
|
+
# table['Name'] # => ["Foo", "Bar", "Baz"]
|
2966
|
+
#
|
2967
|
+
class CSV::Table[out Elem] < Object
|
2968
|
+
include Enumerable[Elem]
|
2969
|
+
extend Forwardable
|
2970
|
+
|
2971
|
+
# <!--
|
2972
|
+
# rdoc-file=lib/csv/table.rb
|
2973
|
+
# - CSV::Table.new(array_of_rows, headers = nil) -> csv_table
|
2974
|
+
# -->
|
2975
|
+
# Returns a new CSV::Table object.
|
2976
|
+
#
|
2977
|
+
# * Argument `array_of_rows` must be an Array of CSV::Row objects.
|
2978
|
+
# * Argument `headers`, if given, may be an Array of Strings.
|
2979
|
+
#
|
2980
|
+
# ---
|
2981
|
+
#
|
2982
|
+
# Create an empty CSV::Table object:
|
2983
|
+
# table = CSV::Table.new([])
|
2984
|
+
# table # => #<CSV::Table mode:col_or_row row_count:1>
|
2985
|
+
#
|
2986
|
+
# Create a non-empty CSV::Table object:
|
2987
|
+
# rows = [
|
2988
|
+
# CSV::Row.new([], []),
|
2989
|
+
# CSV::Row.new([], []),
|
2990
|
+
# CSV::Row.new([], []),
|
2991
|
+
# ]
|
2992
|
+
# table = CSV::Table.new(rows)
|
2993
|
+
# table # => #<CSV::Table mode:col_or_row row_count:4>
|
2994
|
+
#
|
2995
|
+
# ---
|
2996
|
+
#
|
2997
|
+
# If argument `headers` is an Array of Strings, those Strings become the table's
|
2998
|
+
# headers:
|
2999
|
+
# table = CSV::Table.new([], headers: ['Name', 'Age'])
|
3000
|
+
# table.headers # => ["Name", "Age"]
|
3001
|
+
#
|
3002
|
+
# If argument `headers` is not given and the table has rows, the headers are
|
3003
|
+
# taken from the first row:
|
3004
|
+
# rows = [
|
3005
|
+
# CSV::Row.new(['Foo', 'Bar'], []),
|
3006
|
+
# CSV::Row.new(['foo', 'bar'], []),
|
3007
|
+
# CSV::Row.new(['FOO', 'BAR'], []),
|
3008
|
+
# ]
|
3009
|
+
# table = CSV::Table.new(rows)
|
3010
|
+
# table.headers # => ["Foo", "Bar"]
|
3011
|
+
#
|
3012
|
+
# If argument `headers` is not given and the table is empty (has no rows), the
|
3013
|
+
# headers are also empty:
|
3014
|
+
# table = CSV::Table.new([])
|
3015
|
+
# table.headers # => []
|
3016
|
+
#
|
3017
|
+
# ---
|
3018
|
+
#
|
3019
|
+
# Raises an exception if argument `array_of_rows` is not an Array object:
|
3020
|
+
# # Raises NoMethodError (undefined method `first' for :foo:Symbol):
|
3021
|
+
# CSV::Table.new(:foo)
|
3022
|
+
#
|
3023
|
+
# Raises an exception if an element of `array_of_rows` is not a CSV::Table
|
3024
|
+
# object:
|
3025
|
+
# # Raises NoMethodError (undefined method `headers' for :foo:Symbol):
|
3026
|
+
# CSV::Table.new([:foo])
|
3027
|
+
#
|
3028
|
+
def initialize: (untyped array_of_rows, ?headers: untyped) -> untyped
|
3029
|
+
|
3030
|
+
# <!--
|
3031
|
+
# rdoc-file=lib/csv/table.rb
|
3032
|
+
# - table << row_or_array -> self
|
3033
|
+
# -->
|
3034
|
+
# If `row_or_array` is a CSV::Row object, it is appended to the table:
|
3035
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3036
|
+
# table = CSV.parse(source, headers: true)
|
3037
|
+
# table << CSV::Row.new(table.headers, ['bat', 3])
|
3038
|
+
# table[3] # => #<CSV::Row "Name":"bat" "Value":3>
|
3039
|
+
#
|
3040
|
+
# If `row_or_array` is an Array, it is used to create a new CSV::Row object
|
3041
|
+
# which is then appended to the table:
|
3042
|
+
# table << ['bam', 4]
|
3043
|
+
# table[4] # => #<CSV::Row "Name":"bam" "Value":4>
|
3044
|
+
#
|
3045
|
+
def <<: (untyped row_or_array) -> untyped
|
3046
|
+
|
3047
|
+
# <!--
|
3048
|
+
# rdoc-file=lib/csv/table.rb
|
3049
|
+
# - table == other_table -> true or false
|
3050
|
+
# -->
|
3051
|
+
# Returns `true` if all each row of `self` `==` the corresponding row of
|
3052
|
+
# `other_table`, otherwise, `false`.
|
3053
|
+
#
|
3054
|
+
# The access mode does no affect the result.
|
3055
|
+
#
|
3056
|
+
# Equal tables:
|
3057
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3058
|
+
# table = CSV.parse(source, headers: true)
|
3059
|
+
# other_table = CSV.parse(source, headers: true)
|
3060
|
+
# table == other_table # => true
|
3061
|
+
#
|
3062
|
+
# Different row count:
|
3063
|
+
# other_table.delete(2)
|
3064
|
+
# table == other_table # => false
|
3065
|
+
#
|
3066
|
+
# Different last row:
|
3067
|
+
# other_table << ['bat', 3]
|
3068
|
+
# table == other_table # => false
|
3069
|
+
#
|
3070
|
+
def ==: (untyped other) -> bool
|
3071
|
+
|
3072
|
+
# <!--
|
3073
|
+
# rdoc-file=lib/csv/table.rb
|
3074
|
+
# - table[n] -> row or column_data
|
3075
|
+
# - table[range] -> array_of_rows or array_of_column_data
|
3076
|
+
# - table[header] -> array_of_column_data
|
3077
|
+
# -->
|
3078
|
+
# Returns data from the table; does not modify the table.
|
3079
|
+
#
|
3080
|
+
# ---
|
3081
|
+
#
|
3082
|
+
#
|
3083
|
+
# Fetch a Row by Its Integer Index
|
3084
|
+
# :
|
3085
|
+
# * Form: `table[n]`, `n` an integer.
|
3086
|
+
# * Access mode: `:row` or `:col_or_row`.
|
3087
|
+
# * Return value: *nth* row of the table, if that row exists; otherwise `nil`.
|
3088
|
+
#
|
3089
|
+
# Returns the *nth* row of the table if that row exists:
|
3090
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3091
|
+
# table = CSV.parse(source, headers: true)
|
3092
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3093
|
+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
|
3094
|
+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
|
3095
|
+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
|
3096
|
+
#
|
3097
|
+
# Counts backward from the last row if `n` is negative:
|
3098
|
+
# table[-1] # => #<CSV::Row "Name":"baz" "Value":"2">
|
3099
|
+
#
|
3100
|
+
# Returns `nil` if `n` is too large or too small:
|
3101
|
+
# table[4] # => nil
|
3102
|
+
# table[-4] # => nil
|
3103
|
+
#
|
3104
|
+
# Raises an exception if the access mode is `:row` and `n` is not an Integer:
|
3105
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3106
|
+
# # Raises TypeError (no implicit conversion of String into Integer):
|
3107
|
+
# table['Name']
|
3108
|
+
#
|
3109
|
+
# ---
|
3110
|
+
#
|
3111
|
+
#
|
3112
|
+
# Fetch a Column by Its Integer Index
|
3113
|
+
# :
|
3114
|
+
# * Form: `table[n]`, `n` an Integer.
|
3115
|
+
# * Access mode: `:col`.
|
3116
|
+
# * Return value: *nth* column of the table, if that column exists; otherwise
|
3117
|
+
# an Array of `nil` fields of length `self.size`.
|
3118
|
+
#
|
3119
|
+
# Returns the *nth* column of the table if that column exists:
|
3120
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3121
|
+
# table = CSV.parse(source, headers: true)
|
3122
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3123
|
+
# table[1] # => ["0", "1", "2"]
|
3124
|
+
#
|
3125
|
+
# Counts backward from the last column if `n` is negative:
|
3126
|
+
# table[-2] # => ["foo", "bar", "baz"]
|
3127
|
+
#
|
3128
|
+
# Returns an Array of `nil` fields if `n` is too large or too small:
|
3129
|
+
# table[4] # => [nil, nil, nil]
|
3130
|
+
# table[-4] # => [nil, nil, nil]
|
3131
|
+
#
|
3132
|
+
# ---
|
3133
|
+
#
|
3134
|
+
#
|
3135
|
+
# Fetch Rows by Range
|
3136
|
+
# :
|
3137
|
+
# * Form: `table[range]`, `range` a Range object.
|
3138
|
+
# * Access mode: `:row` or `:col_or_row`.
|
3139
|
+
# * Return value: rows from the table, beginning at row `range.start`, if
|
3140
|
+
# those rows exists.
|
3141
|
+
#
|
3142
|
+
# Returns rows from the table, beginning at row `range.first`, if those rows
|
3143
|
+
# exist:
|
3144
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3145
|
+
# table = CSV.parse(source, headers: true)
|
3146
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3147
|
+
# rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
|
3148
|
+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
|
3149
|
+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
|
3150
|
+
# rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
|
3151
|
+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
|
3152
|
+
#
|
3153
|
+
# If there are too few rows, returns all from `range.start` to the end:
|
3154
|
+
# rows = table[1..50] # => #<CSV::Row "Name":"bar" "Value":"1">
|
3155
|
+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
|
3156
|
+
#
|
3157
|
+
# Special case: if `range.start == table.size`, returns an empty Array:
|
3158
|
+
# table[table.size..50] # => []
|
3159
|
+
#
|
3160
|
+
# If `range.end` is negative, calculates the ending index from the end:
|
3161
|
+
# rows = table[0..-1]
|
3162
|
+
# rows # => [#<CSV::Row "Name":"foo" "Value":"0">, #<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
|
3163
|
+
#
|
3164
|
+
# If `range.start` is negative, calculates the starting index from the end:
|
3165
|
+
# rows = table[-1..2]
|
3166
|
+
# rows # => [#<CSV::Row "Name":"baz" "Value":"2">]
|
3167
|
+
#
|
3168
|
+
# If `range.start` is larger than `table.size`, returns `nil`:
|
3169
|
+
# table[4..4] # => nil
|
3170
|
+
#
|
3171
|
+
# ---
|
3172
|
+
#
|
3173
|
+
#
|
3174
|
+
# Fetch Columns by Range
|
3175
|
+
# :
|
3176
|
+
# * Form: `table[range]`, `range` a Range object.
|
3177
|
+
# * Access mode: `:col`.
|
3178
|
+
# * Return value: column data from the table, beginning at column
|
3179
|
+
# `range.start`, if those columns exist.
|
3180
|
+
#
|
3181
|
+
# Returns column values from the table, if the column exists; the values are
|
3182
|
+
# arranged by row:
|
3183
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3184
|
+
# table = CSV.parse(source, headers: true)
|
3185
|
+
# table.by_col!
|
3186
|
+
# table[0..1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
3187
|
+
#
|
3188
|
+
# Special case: if `range.start == headers.size`, returns an Array (size:
|
3189
|
+
# `table.size`) of empty Arrays:
|
3190
|
+
# table[table.headers.size..50] # => [[], [], []]
|
3191
|
+
#
|
3192
|
+
# If `range.end` is negative, calculates the ending index from the end:
|
3193
|
+
# table[0..-1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
3194
|
+
#
|
3195
|
+
# If `range.start` is negative, calculates the starting index from the end:
|
3196
|
+
# table[-2..2] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
3197
|
+
#
|
3198
|
+
# If `range.start` is larger than `table.size`, returns an Array of `nil`
|
3199
|
+
# values:
|
3200
|
+
# table[4..4] # => [nil, nil, nil]
|
3201
|
+
#
|
3202
|
+
# ---
|
3203
|
+
#
|
3204
|
+
#
|
3205
|
+
# Fetch a Column by Its String Header
|
3206
|
+
# :
|
3207
|
+
# * Form: `table[header]`, `header` a String header.
|
3208
|
+
# * Access mode: `:col` or `:col_or_row`
|
3209
|
+
# * Return value: column data from the table, if that `header` exists.
|
3210
|
+
#
|
3211
|
+
# Returns column values from the table, if the column exists:
|
3212
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3213
|
+
# table = CSV.parse(source, headers: true)
|
3214
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3215
|
+
# table['Name'] # => ["foo", "bar", "baz"]
|
3216
|
+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
|
3217
|
+
# col = table['Name']
|
3218
|
+
# col # => ["foo", "bar", "baz"]
|
3219
|
+
#
|
3220
|
+
# Modifying the returned column values does not modify the table:
|
3221
|
+
# col[0] = 'bat'
|
3222
|
+
# col # => ["bat", "bar", "baz"]
|
3223
|
+
# table['Name'] # => ["foo", "bar", "baz"]
|
3224
|
+
#
|
3225
|
+
# Returns an Array of `nil` values if there is no such column:
|
3226
|
+
# table['Nosuch'] # => [nil, nil, nil]
|
3227
|
+
#
|
3228
|
+
def []: (untyped index_or_header) -> untyped
|
3229
|
+
|
3230
|
+
# <!--
|
3231
|
+
# rdoc-file=lib/csv/table.rb
|
3232
|
+
# - table[n] = row -> row
|
3233
|
+
# - table[n] = field_or_array_of_fields -> field_or_array_of_fields
|
3234
|
+
# - table[header] = field_or_array_of_fields -> field_or_array_of_fields
|
3235
|
+
# -->
|
3236
|
+
# Puts data onto the table.
|
3237
|
+
#
|
3238
|
+
# ---
|
3239
|
+
#
|
3240
|
+
#
|
3241
|
+
# Set a Row by Its Integer Index
|
3242
|
+
# :
|
3243
|
+
# * Form: `table[n] = row`, `n` an Integer, `row` a CSV::Row instance or an
|
3244
|
+
# Array of fields.
|
3245
|
+
# * Access mode: `:row` or `:col_or_row`.
|
3246
|
+
# * Return value: `row`.
|
3247
|
+
#
|
3248
|
+
# If the row exists, it is replaced:
|
3249
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3250
|
+
# table = CSV.parse(source, headers: true)
|
3251
|
+
# new_row = CSV::Row.new(['Name', 'Value'], ['bat', 3])
|
3252
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3253
|
+
# return_value = table[0] = new_row
|
3254
|
+
# return_value.equal?(new_row) # => true # Returned the row
|
3255
|
+
# table[0].to_h # => {"Name"=>"bat", "Value"=>3}
|
3256
|
+
#
|
3257
|
+
# With access mode `:col_or_row`:
|
3258
|
+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
|
3259
|
+
# table[0] = CSV::Row.new(['Name', 'Value'], ['bam', 4])
|
3260
|
+
# table[0].to_h # => {"Name"=>"bam", "Value"=>4}
|
3261
|
+
#
|
3262
|
+
# With an Array instead of a CSV::Row, inherits headers from the table:
|
3263
|
+
# array = ['bad', 5]
|
3264
|
+
# return_value = table[0] = array
|
3265
|
+
# return_value.equal?(array) # => true # Returned the array
|
3266
|
+
# table[0].to_h # => {"Name"=>"bad", "Value"=>5}
|
3267
|
+
#
|
3268
|
+
# If the row does not exist, extends the table by adding rows: assigns rows with
|
3269
|
+
# `nil` as needed:
|
3270
|
+
# table.size # => 3
|
3271
|
+
# table[5] = ['bag', 6]
|
3272
|
+
# table.size # => 6
|
3273
|
+
# table[3] # => nil
|
3274
|
+
# table[4]# => nil
|
3275
|
+
# table[5].to_h # => {"Name"=>"bag", "Value"=>6}
|
3276
|
+
#
|
3277
|
+
# Note that the `nil` rows are actually `nil`, not a row of `nil` fields.
|
3278
|
+
#
|
3279
|
+
# ---
|
3280
|
+
#
|
3281
|
+
#
|
3282
|
+
# Set a Column by Its Integer Index
|
3283
|
+
# :
|
3284
|
+
# * Form: `table[n] = array_of_fields`, `n` an Integer, `array_of_fields` an
|
3285
|
+
# Array of String fields.
|
3286
|
+
# * Access mode: `:col`.
|
3287
|
+
# * Return value: `array_of_fields`.
|
3288
|
+
#
|
3289
|
+
# If the column exists, it is replaced:
|
3290
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3291
|
+
# table = CSV.parse(source, headers: true)
|
3292
|
+
# new_col = [3, 4, 5]
|
3293
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3294
|
+
# return_value = table[1] = new_col
|
3295
|
+
# return_value.equal?(new_col) # => true # Returned the column
|
3296
|
+
# table[1] # => [3, 4, 5]
|
3297
|
+
# # The rows, as revised:
|
3298
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3299
|
+
# table[0].to_h # => {"Name"=>"foo", "Value"=>3}
|
3300
|
+
# table[1].to_h # => {"Name"=>"bar", "Value"=>4}
|
3301
|
+
# table[2].to_h # => {"Name"=>"baz", "Value"=>5}
|
3302
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3303
|
+
#
|
3304
|
+
# If there are too few values, fills with `nil` values:
|
3305
|
+
# table[1] = [0]
|
3306
|
+
# table[1] # => [0, nil, nil]
|
3307
|
+
#
|
3308
|
+
# If there are too many values, ignores the extra values:
|
3309
|
+
# table[1] = [0, 1, 2, 3, 4]
|
3310
|
+
# table[1] # => [0, 1, 2]
|
3311
|
+
#
|
3312
|
+
# If a single value is given, replaces all fields in the column with that value:
|
3313
|
+
# table[1] = 'bat'
|
3314
|
+
# table[1] # => ["bat", "bat", "bat"]
|
3315
|
+
#
|
3316
|
+
# ---
|
3317
|
+
#
|
3318
|
+
#
|
3319
|
+
# Set a Column by Its String Header
|
3320
|
+
# :
|
3321
|
+
# * Form: `table[header] = field_or_array_of_fields`, `header` a String
|
3322
|
+
# header, `field_or_array_of_fields` a field value or an Array of String
|
3323
|
+
# fields.
|
3324
|
+
# * Access mode: `:col` or `:col_or_row`.
|
3325
|
+
# * Return value: `field_or_array_of_fields`.
|
3326
|
+
#
|
3327
|
+
# If the column exists, it is replaced:
|
3328
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3329
|
+
# table = CSV.parse(source, headers: true)
|
3330
|
+
# new_col = [3, 4, 5]
|
3331
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3332
|
+
# return_value = table['Value'] = new_col
|
3333
|
+
# return_value.equal?(new_col) # => true # Returned the column
|
3334
|
+
# table['Value'] # => [3, 4, 5]
|
3335
|
+
# # The rows, as revised:
|
3336
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3337
|
+
# table[0].to_h # => {"Name"=>"foo", "Value"=>3}
|
3338
|
+
# table[1].to_h # => {"Name"=>"bar", "Value"=>4}
|
3339
|
+
# table[2].to_h # => {"Name"=>"baz", "Value"=>5}
|
3340
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3341
|
+
#
|
3342
|
+
# If there are too few values, fills with `nil` values:
|
3343
|
+
# table['Value'] = [0]
|
3344
|
+
# table['Value'] # => [0, nil, nil]
|
3345
|
+
#
|
3346
|
+
# If there are too many values, ignores the extra values:
|
3347
|
+
# table['Value'] = [0, 1, 2, 3, 4]
|
3348
|
+
# table['Value'] # => [0, 1, 2]
|
3349
|
+
#
|
3350
|
+
# If the column does not exist, extends the table by adding columns:
|
3351
|
+
# table['Note'] = ['x', 'y', 'z']
|
3352
|
+
# table['Note'] # => ["x", "y", "z"]
|
3353
|
+
# # The rows, as revised:
|
3354
|
+
# table.by_row!
|
3355
|
+
# table[0].to_h # => {"Name"=>"foo", "Value"=>0, "Note"=>"x"}
|
3356
|
+
# table[1].to_h # => {"Name"=>"bar", "Value"=>1, "Note"=>"y"}
|
3357
|
+
# table[2].to_h # => {"Name"=>"baz", "Value"=>2, "Note"=>"z"}
|
3358
|
+
# table.by_col!
|
3359
|
+
#
|
3360
|
+
# If a single value is given, replaces all fields in the column with that value:
|
3361
|
+
# table['Value'] = 'bat'
|
3362
|
+
# table['Value'] # => ["bat", "bat", "bat"]
|
3363
|
+
#
|
3364
|
+
def []=: (untyped index_or_header, untyped value) -> untyped
|
3365
|
+
|
3366
|
+
# <!--
|
3367
|
+
# rdoc-file=lib/csv/table.rb
|
3368
|
+
# - table.by_col -> table_dup
|
3369
|
+
# -->
|
3370
|
+
# Returns a duplicate of `self`, in column mode (see [Column
|
3371
|
+
# Mode](#class-CSV::Table-label-Column+Mode)):
|
3372
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3373
|
+
# table = CSV.parse(source, headers: true)
|
3374
|
+
# table.mode # => :col_or_row
|
3375
|
+
# dup_table = table.by_col
|
3376
|
+
# dup_table.mode # => :col
|
3377
|
+
# dup_table.equal?(table) # => false # It's a dup
|
3378
|
+
#
|
3379
|
+
# This may be used to chain method calls without changing the mode (but also
|
3380
|
+
# will affect performance and memory usage):
|
3381
|
+
# dup_table.by_col['Name']
|
3382
|
+
#
|
3383
|
+
# Also note that changes to the duplicate table will not affect the original.
|
3384
|
+
#
|
3385
|
+
def by_col: () -> untyped
|
3386
|
+
|
3387
|
+
# <!--
|
3388
|
+
# rdoc-file=lib/csv/table.rb
|
3389
|
+
# - table.by_col! -> self
|
3390
|
+
# -->
|
3391
|
+
# Sets the mode for `self` to column mode (see [Column
|
3392
|
+
# Mode](#class-CSV::Table-label-Column+Mode)); returns `self`:
|
3393
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3394
|
+
# table = CSV.parse(source, headers: true)
|
3395
|
+
# table.mode # => :col_or_row
|
3396
|
+
# table1 = table.by_col!
|
3397
|
+
# table.mode # => :col
|
3398
|
+
# table1.equal?(table) # => true # Returned self
|
3399
|
+
#
|
3400
|
+
def by_col!: () -> untyped
|
3401
|
+
|
3402
|
+
# <!--
|
3403
|
+
# rdoc-file=lib/csv/table.rb
|
3404
|
+
# - table.by_col_or_row -> table_dup
|
3405
|
+
# -->
|
3406
|
+
# Returns a duplicate of `self`, in mixed mode (see [Mixed
|
3407
|
+
# Mode](#class-CSV::Table-label-Mixed+Mode)):
|
3408
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3409
|
+
# table = CSV.parse(source, headers: true).by_col!
|
3410
|
+
# table.mode # => :col
|
3411
|
+
# dup_table = table.by_col_or_row
|
3412
|
+
# dup_table.mode # => :col_or_row
|
3413
|
+
# dup_table.equal?(table) # => false # It's a dup
|
3414
|
+
#
|
3415
|
+
# This may be used to chain method calls without changing the mode (but also
|
3416
|
+
# will affect performance and memory usage):
|
3417
|
+
# dup_table.by_col_or_row['Name']
|
3418
|
+
#
|
3419
|
+
# Also note that changes to the duplicate table will not affect the original.
|
3420
|
+
#
|
3421
|
+
def by_col_or_row: () -> untyped
|
3422
|
+
|
3423
|
+
# <!--
|
3424
|
+
# rdoc-file=lib/csv/table.rb
|
3425
|
+
# - table.by_col_or_row! -> self
|
3426
|
+
# -->
|
3427
|
+
# Sets the mode for `self` to mixed mode (see [Mixed
|
3428
|
+
# Mode](#class-CSV::Table-label-Mixed+Mode)); returns `self`:
|
3429
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3430
|
+
# table = CSV.parse(source, headers: true).by_col!
|
3431
|
+
# table.mode # => :col
|
3432
|
+
# table1 = table.by_col_or_row!
|
3433
|
+
# table.mode # => :col_or_row
|
3434
|
+
# table1.equal?(table) # => true # Returned self
|
3435
|
+
#
|
3436
|
+
def by_col_or_row!: () -> untyped
|
3437
|
+
|
3438
|
+
# <!--
|
3439
|
+
# rdoc-file=lib/csv/table.rb
|
3440
|
+
# - table.by_row -> table_dup
|
3441
|
+
# -->
|
3442
|
+
# Returns a duplicate of `self`, in row mode (see [Row
|
3443
|
+
# Mode](#class-CSV::Table-label-Row+Mode)):
|
3444
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3445
|
+
# table = CSV.parse(source, headers: true)
|
3446
|
+
# table.mode # => :col_or_row
|
3447
|
+
# dup_table = table.by_row
|
3448
|
+
# dup_table.mode # => :row
|
3449
|
+
# dup_table.equal?(table) # => false # It's a dup
|
3450
|
+
#
|
3451
|
+
# This may be used to chain method calls without changing the mode (but also
|
3452
|
+
# will affect performance and memory usage):
|
3453
|
+
# dup_table.by_row[1]
|
3454
|
+
#
|
3455
|
+
# Also note that changes to the duplicate table will not affect the original.
|
3456
|
+
#
|
3457
|
+
def by_row: () -> untyped
|
3458
|
+
|
3459
|
+
# <!--
|
3460
|
+
# rdoc-file=lib/csv/table.rb
|
3461
|
+
# - table.by_row! -> self
|
3462
|
+
# -->
|
3463
|
+
# Sets the mode for `self` to row mode (see [Row
|
3464
|
+
# Mode](#class-CSV::Table-label-Row+Mode)); returns `self`:
|
3465
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3466
|
+
# table = CSV.parse(source, headers: true)
|
3467
|
+
# table.mode # => :col_or_row
|
3468
|
+
# table1 = table.by_row!
|
3469
|
+
# table.mode # => :row
|
3470
|
+
# table1.equal?(table) # => true # Returned self
|
3471
|
+
#
|
3472
|
+
def by_row!: () -> untyped
|
3473
|
+
|
3474
|
+
# <!--
|
3475
|
+
# rdoc-file=lib/csv/table.rb
|
3476
|
+
# - table.delete(*indexes) -> deleted_values
|
3477
|
+
# - table.delete(*headers) -> deleted_values
|
3478
|
+
# -->
|
3479
|
+
# If the access mode is `:row` or `:col_or_row`, and each argument is either an
|
3480
|
+
# Integer or a Range, returns deleted rows. Otherwise, returns deleted columns
|
3481
|
+
# data.
|
3482
|
+
#
|
3483
|
+
# In either case, the returned values are in the order specified by the
|
3484
|
+
# arguments. Arguments may be repeated.
|
3485
|
+
#
|
3486
|
+
# ---
|
3487
|
+
#
|
3488
|
+
# Returns rows as an Array of CSV::Row objects.
|
3489
|
+
#
|
3490
|
+
# One index:
|
3491
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3492
|
+
# table = CSV.parse(source, headers: true)
|
3493
|
+
# deleted_values = table.delete(0)
|
3494
|
+
# deleted_values # => [#<CSV::Row "Name":"foo" "Value":"0">]
|
3495
|
+
#
|
3496
|
+
# Two indexes:
|
3497
|
+
# table = CSV.parse(source, headers: true)
|
3498
|
+
# deleted_values = table.delete(2, 0)
|
3499
|
+
# deleted_values # => [#<CSV::Row "Name":"baz" "Value":"2">, #<CSV::Row "Name":"foo" "Value":"0">]
|
3500
|
+
#
|
3501
|
+
# ---
|
3502
|
+
#
|
3503
|
+
# Returns columns data as column Arrays.
|
3504
|
+
#
|
3505
|
+
# One header:
|
3506
|
+
# table = CSV.parse(source, headers: true)
|
3507
|
+
# deleted_values = table.delete('Name')
|
3508
|
+
# deleted_values # => ["foo", "bar", "baz"]
|
3509
|
+
#
|
3510
|
+
# Two headers:
|
3511
|
+
# table = CSV.parse(source, headers: true)
|
3512
|
+
# deleted_values = table.delete('Value', 'Name')
|
3513
|
+
# deleted_values # => [["0", "1", "2"], ["foo", "bar", "baz"]]
|
3514
|
+
#
|
3515
|
+
def delete: (*untyped indexes_or_headers) -> untyped
|
3516
|
+
|
3517
|
+
# <!--
|
3518
|
+
# rdoc-file=lib/csv/table.rb
|
3519
|
+
# - table.delete_if {|row_or_column| ... } -> self
|
3520
|
+
# -->
|
3521
|
+
# Removes rows or columns for which the block returns a truthy value; returns
|
3522
|
+
# `self`.
|
3523
|
+
#
|
3524
|
+
# Removes rows when the access mode is `:row` or `:col_or_row`; calls the block
|
3525
|
+
# with each CSV::Row object:
|
3526
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3527
|
+
# table = CSV.parse(source, headers: true)
|
3528
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3529
|
+
# table.size # => 3
|
3530
|
+
# table.delete_if {|row| row['Name'].start_with?('b') }
|
3531
|
+
# table.size # => 1
|
3532
|
+
#
|
3533
|
+
# Removes columns when the access mode is `:col`; calls the block with each
|
3534
|
+
# column as a 2-element array containing the header and an Array of column
|
3535
|
+
# fields:
|
3536
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3537
|
+
# table = CSV.parse(source, headers: true)
|
3538
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3539
|
+
# table.headers.size # => 2
|
3540
|
+
# table.delete_if {|column_data| column_data[1].include?('2') }
|
3541
|
+
# table.headers.size # => 1
|
3542
|
+
#
|
3543
|
+
# Returns a new Enumerator if no block is given:
|
3544
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3545
|
+
# table = CSV.parse(source, headers: true)
|
3546
|
+
# table.delete_if # => #<Enumerator: #<CSV::Table mode:col_or_row row_count:4>:delete_if>
|
3547
|
+
#
|
3548
|
+
def delete_if: () { (*untyped) -> untyped } -> untyped
|
3549
|
+
|
3550
|
+
# <!--
|
3551
|
+
# rdoc-file=lib/csv/table.rb
|
3552
|
+
# - dig(index_or_header, *index_or_headers)
|
3553
|
+
# -->
|
3554
|
+
# Extracts the nested value specified by the sequence of `index` or `header`
|
3555
|
+
# objects by calling dig at each step, returning nil if any intermediate step is
|
3556
|
+
# nil.
|
3557
|
+
#
|
3558
|
+
def dig: (untyped index_or_header, *untyped index_or_headers) -> untyped
|
3559
|
+
|
3560
|
+
# <!--
|
3561
|
+
# rdoc-file=lib/csv/table.rb
|
3562
|
+
# - table.each {|row_or_column| ... ) -> self
|
3563
|
+
# -->
|
3564
|
+
# Calls the block with each row or column; returns `self`.
|
3565
|
+
#
|
3566
|
+
# When the access mode is `:row` or `:col_or_row`, calls the block with each
|
3567
|
+
# CSV::Row object:
|
3568
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3569
|
+
# table = CSV.parse(source, headers: true)
|
3570
|
+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
|
3571
|
+
# table.each {|row| p row }
|
3572
|
+
#
|
3573
|
+
# Output:
|
3574
|
+
# #<CSV::Row "Name":"foo" "Value":"0">
|
3575
|
+
# #<CSV::Row "Name":"bar" "Value":"1">
|
3576
|
+
# #<CSV::Row "Name":"baz" "Value":"2">
|
3577
|
+
#
|
3578
|
+
# When the access mode is `:col`, calls the block with each column as a
|
3579
|
+
# 2-element array containing the header and an Array of column fields:
|
3580
|
+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
|
3581
|
+
# table.each {|column_data| p column_data }
|
3582
|
+
#
|
3583
|
+
# Output:
|
3584
|
+
# ["Name", ["foo", "bar", "baz"]]
|
3585
|
+
# ["Value", ["0", "1", "2"]]
|
3586
|
+
#
|
3587
|
+
# Returns a new Enumerator if no block is given:
|
3588
|
+
# table.each # => #<Enumerator: #<CSV::Table mode:col row_count:4>:each>
|
3589
|
+
#
|
3590
|
+
def each: () -> Enumerator[Elem, self]
|
3591
|
+
| () { (Elem) -> void } -> self
|
3592
|
+
| () { (*untyped) -> void } -> self
|
3593
|
+
|
3594
|
+
def empty?: (*untyped args) { (*untyped) -> untyped } -> untyped
|
3595
|
+
|
3596
|
+
# <!--
|
3597
|
+
# rdoc-file=lib/csv/table.rb
|
3598
|
+
# - table.headers -> array_of_headers
|
3599
|
+
# -->
|
3600
|
+
# Returns a new Array containing the String headers for the table.
|
3601
|
+
#
|
3602
|
+
# If the table is not empty, returns the headers from the first row:
|
3603
|
+
# rows = [
|
3604
|
+
# CSV::Row.new(['Foo', 'Bar'], []),
|
3605
|
+
# CSV::Row.new(['FOO', 'BAR'], []),
|
3606
|
+
# CSV::Row.new(['foo', 'bar'], []),
|
3607
|
+
# ]
|
3608
|
+
# table = CSV::Table.new(rows)
|
3609
|
+
# table.headers # => ["Foo", "Bar"]
|
3610
|
+
# table.delete(0)
|
3611
|
+
# table.headers # => ["FOO", "BAR"]
|
3612
|
+
# table.delete(0)
|
3613
|
+
# table.headers # => ["foo", "bar"]
|
3614
|
+
#
|
3615
|
+
# If the table is empty, returns a copy of the headers in the table itself:
|
3616
|
+
# table.delete(0)
|
3617
|
+
# table.headers # => ["Foo", "Bar"]
|
3618
|
+
#
|
3619
|
+
def headers: () -> untyped
|
3620
|
+
|
3621
|
+
# <!--
|
3622
|
+
# rdoc-file=lib/csv/table.rb
|
3623
|
+
# - table.inspect => string
|
3624
|
+
# -->
|
3625
|
+
# Returns a `US-ASCII`-encoded String showing table:
|
3626
|
+
# * Class: `CSV::Table`.
|
3627
|
+
# * Access mode: `:row`, `:col`, or `:col_or_row`.
|
3628
|
+
# * Size: Row count, including the header row.
|
3629
|
+
#
|
3630
|
+
# Example:
|
3631
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3632
|
+
# table = CSV.parse(source, headers: true)
|
3633
|
+
# table.inspect # => "#<CSV::Table mode:col_or_row row_count:4>\nName,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3634
|
+
#
|
3635
|
+
def inspect: () -> String
|
3636
|
+
|
3637
|
+
def length: (*untyped args) { (*untyped) -> untyped } -> untyped
|
3638
|
+
|
3639
|
+
# <!-- rdoc-file=lib/csv/table.rb -->
|
3640
|
+
# The current access mode for indexing and iteration.
|
3641
|
+
#
|
3642
|
+
def mode: () -> untyped
|
3643
|
+
|
3644
|
+
# <!--
|
3645
|
+
# rdoc-file=lib/csv/table.rb
|
3646
|
+
# - table.push(*rows_or_arrays) -> self
|
3647
|
+
# -->
|
3648
|
+
# A shortcut for appending multiple rows. Equivalent to:
|
3649
|
+
# rows.each {|row| self << row }
|
3650
|
+
#
|
3651
|
+
# Each argument may be either a CSV::Row object or an Array:
|
3652
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3653
|
+
# table = CSV.parse(source, headers: true)
|
3654
|
+
# rows = [
|
3655
|
+
# CSV::Row.new(table.headers, ['bat', 3]),
|
3656
|
+
# ['bam', 4]
|
3657
|
+
# ]
|
3658
|
+
# table.push(*rows)
|
3659
|
+
# table[3..4] # => [#<CSV::Row "Name":"bat" "Value":3>, #<CSV::Row "Name":"bam" "Value":4>]
|
3660
|
+
#
|
3661
|
+
def push: (*untyped rows) -> untyped
|
3662
|
+
|
3663
|
+
def size: (*untyped args) { (*untyped) -> untyped } -> untyped
|
3664
|
+
|
3665
|
+
# <!--
|
3666
|
+
# rdoc-file=lib/csv/table.rb
|
3667
|
+
# - table.to_a -> array_of_arrays
|
3668
|
+
# -->
|
3669
|
+
# Returns the table as an Array of Arrays; the headers are in the first row:
|
3670
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3671
|
+
# table = CSV.parse(source, headers: true)
|
3672
|
+
# table.to_a # => [["Name", "Value"], ["foo", "0"], ["bar", "1"], ["baz", "2"]]
|
3673
|
+
#
|
3674
|
+
def to_a: () -> untyped
|
3675
|
+
|
3676
|
+
# <!--
|
3677
|
+
# rdoc-file=lib/csv/table.rb
|
3678
|
+
# - table.to_csv(**options) -> csv_string
|
3679
|
+
# -->
|
3680
|
+
# Returns the table as CSV string. See [Options for
|
3681
|
+
# Generating](../CSV.html#class-CSV-label-Options+for+Generating).
|
3682
|
+
#
|
3683
|
+
# Defaults option `write_headers` to `true`:
|
3684
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3685
|
+
# table = CSV.parse(source, headers: true)
|
3686
|
+
# table.to_csv # => "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3687
|
+
#
|
3688
|
+
# Omits the headers if option `write_headers` is given as `false` (see {Option
|
3689
|
+
# `write_headers`[}](../CSV.html#class-CSV-label-Option+write_headers)):
|
3690
|
+
# table.to_csv(write_headers: false) # => "foo,0\nbar,1\nbaz,2\n"
|
3691
|
+
#
|
3692
|
+
# Limit rows if option `limit` is given like `2`:
|
3693
|
+
# table.to_csv(limit: 2) # => "Name,Value\nfoo,0\nbar,1\n"
|
3694
|
+
#
|
3695
|
+
def to_csv: (?write_headers: boolish, **untyped) -> untyped
|
3696
|
+
|
3697
|
+
# <!--
|
3698
|
+
# rdoc-file=lib/csv/table.rb
|
3699
|
+
# - to_s(write_headers: true, limit: nil, **options)
|
3700
|
+
# -->
|
3701
|
+
#
|
3702
|
+
alias to_s to_csv
|
3703
|
+
|
3704
|
+
# <!--
|
3705
|
+
# rdoc-file=lib/csv/table.rb
|
3706
|
+
# - table.values_at(*indexes) -> array_of_rows
|
3707
|
+
# - table.values_at(*headers) -> array_of_columns_data
|
3708
|
+
# -->
|
3709
|
+
# If the access mode is `:row` or `:col_or_row`, and each argument is either an
|
3710
|
+
# Integer or a Range, returns rows. Otherwise, returns columns data.
|
3711
|
+
#
|
3712
|
+
# In either case, the returned values are in the order specified by the
|
3713
|
+
# arguments. Arguments may be repeated.
|
3714
|
+
#
|
3715
|
+
# ---
|
3716
|
+
#
|
3717
|
+
# Returns rows as an Array of CSV::Row objects.
|
3718
|
+
#
|
3719
|
+
# No argument:
|
3720
|
+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
|
3721
|
+
# table = CSV.parse(source, headers: true)
|
3722
|
+
# table.values_at # => []
|
3723
|
+
#
|
3724
|
+
# One index:
|
3725
|
+
# values = table.values_at(0)
|
3726
|
+
# values # => [#<CSV::Row "Name":"foo" "Value":"0">]
|
3727
|
+
#
|
3728
|
+
# Two indexes:
|
3729
|
+
# values = table.values_at(2, 0)
|
3730
|
+
# values # => [#<CSV::Row "Name":"baz" "Value":"2">, #<CSV::Row "Name":"foo" "Value":"0">]
|
3731
|
+
#
|
3732
|
+
# One Range:
|
3733
|
+
# values = table.values_at(1..2)
|
3734
|
+
# values # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
|
3735
|
+
#
|
3736
|
+
# Ranges and indexes:
|
3737
|
+
# values = table.values_at(0..1, 1..2, 0, 2)
|
3738
|
+
# pp values
|
3739
|
+
#
|
3740
|
+
# Output:
|
3741
|
+
# [#<CSV::Row "Name":"foo" "Value":"0">,
|
3742
|
+
# #<CSV::Row "Name":"bar" "Value":"1">,
|
3743
|
+
# #<CSV::Row "Name":"bar" "Value":"1">,
|
3744
|
+
# #<CSV::Row "Name":"baz" "Value":"2">,
|
3745
|
+
# #<CSV::Row "Name":"foo" "Value":"0">,
|
3746
|
+
# #<CSV::Row "Name":"baz" "Value":"2">]
|
3747
|
+
#
|
3748
|
+
# ---
|
3749
|
+
#
|
3750
|
+
# Returns columns data as row Arrays, each consisting of the specified columns
|
3751
|
+
# data for that row:
|
3752
|
+
# values = table.values_at('Name')
|
3753
|
+
# values # => [["foo"], ["bar"], ["baz"]]
|
3754
|
+
# values = table.values_at('Value', 'Name')
|
3755
|
+
# values # => [["0", "foo"], ["1", "bar"], ["2", "baz"]]
|
3756
|
+
#
|
3757
|
+
def values_at: (*untyped indices_or_headers) -> untyped
|
3758
|
+
end
|
3759
|
+
|
3760
|
+
%a{annotate:rdoc:skip}
|
3761
|
+
class Array[unchecked out Elem] < Object
|
3762
|
+
# Equivalent to CSV::generate_line(self, options)
|
3763
|
+
#
|
3764
|
+
# ["CSV", "data"].to_csv
|
3765
|
+
# #=> "CSV,data\n"
|
3766
|
+
def to_csv: (**untyped options) -> String
|
3767
|
+
end
|
3768
|
+
|
3769
|
+
%a{annotate:rdoc:skip}
|
3770
|
+
class String
|
3771
|
+
# Equivalent to CSV::parse_line(self, options)
|
3772
|
+
#
|
3773
|
+
# "CSV,data".parse_csv
|
3774
|
+
# #=> ["CSV", "data"]
|
3775
|
+
def parse_csv: (**untyped options) -> ::Array[String?]?
|
3776
|
+
end
|