summaryrefslogtreecommitdiff
path: root/doc/csv/recipes/generating.rdoc
diff options
context:
space:
mode:
authorBurdette Lamar <[email protected]>2020-10-13 20:06:41 -0500
committerSutou Kouhei <[email protected]>2020-11-24 09:33:55 +0900
commitc5fcafd2fd82ddbae38739a874bf84a19b4ef402 (patch)
tree49c100f228340ed6d45e551a39e13edd73fd7a1d /doc/csv/recipes/generating.rdoc
parent3cfb63fcd864e6114138a9ba4e972bd616034bc2 (diff)
[ruby/csv] Split recipes into three pages: parsing, generating, filtering (#184)
Co-authored-by: Sutou Kouhei <[email protected]> https://github.com/ruby/csv/commit/f0bab6a592
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3804
Diffstat (limited to 'doc/csv/recipes/generating.rdoc')
-rw-r--r--doc/csv/recipes/generating.rdoc113
1 files changed, 113 insertions, 0 deletions
diff --git a/doc/csv/recipes/generating.rdoc b/doc/csv/recipes/generating.rdoc
new file mode 100644
index 0000000000..514620017a
--- /dev/null
+++ b/doc/csv/recipes/generating.rdoc
@@ -0,0 +1,113 @@
+== Recipes for Generating \CSV
+
+For other recipes, see {Recipes for CSV}[./recipes_rdoc.html].
+
+All code snippets on this page assume that the following has been executed:
+ require 'csv'
+
+=== Contents
+
+- {Output Formats}[#label-Output+Formats]
+ - {Generating to a String}[#label-Generating+to+a+String]
+ - {Recipe: Generate to String with Headers}[#label-Recipe-3A+Generate+to+String+with+Headers]
+ - {Recipe: Generate to String Without Headers}[#label-Recipe-3A+Generate+to+String+Without+Headers]
+ - {Generating to a File}[#label-Generating+to+a+File]
+ - {Recipe: Generate to File with Headers}[#label-Recipe-3A+Generate+to+File+with+Headers]
+ - {Recipe: Generate to File Without Headers}[#label-Recipe-3A+Generate+to+File+Without+Headers]
+ - {Generating to IO an Stream}[#label-Generating+to+an+IO+Stream]
+ - {Recipe: Generate to IO Stream with Headers}[#label-Recipe-3A+Generate+to+IO+Stream+with+Headers]
+ - {Recipe: Generate to IO Stream Without Headers}[#label-Recipe-3A+Generate+to+IO+Stream+Without+Headers]
+
+=== Output Formats
+
+You can generate \CSV output to a \String, to a \File (via its path), or to an \IO stream.
+
+==== Generating to a \String
+
+You can generate \CSV output to a \String, with or without headers.
+
+===== Recipe: Generate to \String with Headers
+
+Use class method CSV.generate with option +headers+ to generate to a \String.
+
+This example uses method CSV#<< to append the rows
+that are to be generated:
+ output_string = CSV.generate('', headers: ['Name', 'Value'], write_headers: true) do |csv|
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ output_string # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
+
+===== Recipe: Generate to \String Without Headers
+
+Use class method CSV.generate without option +headers+ to generate to a \String.
+
+This example uses method CSV#<< to append the rows
+that are to be generated:
+ output_string = CSV.generate do |csv|
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ output_string # => "Foo,0\nBar,1\nBaz,2\n"
+
+==== Generating to a \File
+
+You can generate /CSV data to a \File, with or without headers.
+
+===== Recipe: Generate to \File with Headers
+
+Use class method CSV.open with option +headers+ generate to a \File.
+
+This example uses method CSV#<< to append the rows
+that are to be generated:
+ path = 't.csv'
+ CSV.open(path, 'w', headers: ['Name', 'Value'], write_headers: true) do |csv|
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
+
+===== Recipe: Generate to \File Without Headers
+
+Use class method CSV.open without option +headers+ to generate to a \File.
+
+This example uses method CSV#<< to append the rows
+that are to be generated:
+ path = 't.csv'
+ CSV.open(path, 'w') do |csv|
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ p File.read(path) # => "Foo,0\nBar,1\nBaz,2\n"
+
+==== Generating to an \IO Stream
+
+You can generate \CSV data to an \IO stream, with or without headers.
+
+==== Recipe: Generate to \IO Stream with Headers
+
+Use class method CSV.new with option +headers+ to generate \CSV data to an \IO stream:
+ path = 't.csv'
+ File.open(path, 'w') do |file|
+ csv = CSV.new(file, headers: ['Name', 'Value'], write_headers: true)
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ p File.read(path) # => "Name,Value\nFoo,0\nBar,1\nBaz,2\n"
+
+===== Recipe: Generate to \IO Stream Without Headers
+
+Use class method CSV.new without option +headers+ to generate \CSV data to an \IO stream:
+ path = 't.csv'
+ File.open(path, 'w') do |file|
+ csv = CSV.new(file)
+ csv << ['Foo', 0]
+ csv << ['Bar', 1]
+ csv << ['Baz', 2]
+ end
+ p File.read(path) # => "Foo,0\nBar,1\nBaz,2\n"