diff options
Diffstat (limited to 'doc/csv/options/common/col_sep.rdoc')
-rw-r--r-- | doc/csv/options/common/col_sep.rdoc | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/doc/csv/options/common/col_sep.rdoc b/doc/csv/options/common/col_sep.rdoc index bb4d3ce389..05769b5773 100644 --- a/doc/csv/options/common/col_sep.rdoc +++ b/doc/csv/options/common/col_sep.rdoc @@ -7,39 +7,57 @@ The \String will be transcoded into the data's \Encoding before use. Default value: CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma) -For examples in this section: - ary = ['a', 'b', 'c'] - -Using the default: - str = CSV.generate_line(line) - str # => "a,b,c\n" - ary = CSV.parse_line(str) - ary # => ["a", "b", "c"] +Using the default (comma): + str = CSV.generate do |csv| + csv << [:foo, 0] + csv << [:bar, 1] + csv << [:baz, 2] + end + str # => "foo,0\nbar,1\nbaz,2\n" + ary = CSV.parse(str) + ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] Using +:+ (colon): col_sep = ':' - str = CSV.generate_line(ary, col_sep: col_sep) - str # => "a:b:c\n" - ary = CSV.parse_line(str, col_sep: col_sep) - ary # => [["a", "b", "c"]] + str = CSV.generate(col_sep: col_sep) do |csv| + csv << [:foo, 0] + csv << [:bar, 1] + csv << [:baz, 2] + end + str # => "foo:0\nbar:1\nbaz:2\n" + ary = CSV.parse(str, col_sep: col_sep) + ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] Using +::+ (two colons): col_sep = '::' - str = CSV.generate_line(ary, col_sep: col_sep) - str # => "a::b::c\n" - ary = CSV.parse_line(str, col_sep: col_sep) - ary # => [["a", "b", "c"]] + str = CSV.generate(col_sep: col_sep) do |csv| + csv << [:foo, 0] + csv << [:bar, 1] + csv << [:baz, 2] + end + str # => "foo::0\nbar::1\nbaz::2\n" + ary = CSV.parse(str, col_sep: col_sep) + ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]] + +Using <tt>''</tt> (empty string): + col_sep = '' + str = CSV.generate(col_sep: col_sep) do |csv| + csv << [:foo, 0] + csv << [:bar, 1] + csv << [:baz, 2] + end + str # => "foo0\nbar1\nbaz2\n" --- -Raises an exception if given the empty \String: +Raises an exception if parsing with the empty \String: col_sep = '' # Raises ArgumentError (:col_sep must be 1 or more characters: "") - CSV.parse_line("a:b:c\n", col_sep: col_sep) + CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep) Raises an exception if the given value is not String-convertible: col_sep = BasicObject.new # Raises NoMethodError (undefined method `to_s' for #<BasicObject:>) - CSV.generate_line(line, col_sep: col_sep) + CSV.generate(line, col_sep: col_sep) # Raises NoMethodError (undefined method `to_s' for #<BasicObject:>) CSV.parse(str, col_sep: col_sep) |