Skip to content

Commit 85e293c

Browse files
BurdetteLamarkou
andauthored
Add document for CSV.instance (#136)
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
1 parent 1a4b96b commit 85e293c

File tree

1 file changed

+94
-66
lines changed

1 file changed

+94
-66
lines changed

lib/csv.rb

Lines changed: 94 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,44 @@
181181
# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
182182
# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin
183183
#
184+
# == Delegated Methods
185+
#
186+
# For convenience, a CSV object will delegate to many methods in class IO.
187+
# (A few have wrapper "guard code" in \CSV.) You may call:
188+
# * IO#binmode
189+
# * #binmode?
190+
# * IO#close
191+
# * IO#close_read
192+
# * IO#close_write
193+
# * IO#closed?
194+
# * #eof
195+
# * #eof?
196+
# * IO#external_encoding
197+
# * IO#fcntl
198+
# * IO#fileno
199+
# * #flock
200+
# * IO#flush
201+
# * IO#fsync
202+
# * IO#internal_encoding
203+
# * #ioctl
204+
# * IO#isatty
205+
# * #path
206+
# * IO#pid
207+
# * IO#pos
208+
# * IO#pos=
209+
# * IO#reopen
210+
# * #rewind
211+
# * IO#seek
212+
# * #stat
213+
# * IO#string
214+
# * IO#sync
215+
# * IO#sync=
216+
# * IO#tell
217+
# * #to_i
218+
# * #to_io
219+
# * IO#truncate
220+
# * IO#tty?
221+
#
184222
# == Options
185223
#
186224
# The default values for options are:
@@ -674,18 +712,47 @@ def initialize(message, line_number)
674712
}.freeze
675713

676714
class << self
715+
# :call-seq:
716+
# instance(string, **options)
717+
# instance(io = $stdout, **options)
718+
# instance(string, **options) {|csv| ... }
719+
# instance(io = $stdout, **options) {|csv| ... }
677720
#
678-
# This method will return a CSV instance, just like CSV::new(), but the
679-
# instance will be cached and returned for all future calls to this method for
680-
# the same +data+ object (tested by Object#object_id()) with the same
681-
# +options+.
721+
# Creates or retrieves cached \CSV objects.
722+
# For arguments and options, see CSV.new.
682723
#
683-
# See {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
684-
# and {Options for Generating}[#class-CSV-label-Options+for+Generating].
724+
# ---
685725
#
686-
# If a block is given, the instance is passed to the block and the return
687-
# value becomes the return value of the block.
726+
# With no block given, returns a \CSV object.
688727
#
728+
# The first call to +instance+ creates and caches a \CSV object:
729+
# s0 = 's0'
730+
# csv0 = CSV.instance(s0)
731+
# csv0.class # => CSV
732+
#
733+
# Subsequent calls to +instance+ with that _same_ +string+ or +io+
734+
# retrieve that same cached object:
735+
# csv1 = CSV.instance(s0)
736+
# csv1.class # => CSV
737+
# csv1.equal?(csv0) # => true # Same CSV object
738+
#
739+
# A subsequent call to +instance+ with a _different_ +string+ or +io+
740+
# creates and caches a _different_ \CSV object.
741+
# s1 = 's1'
742+
# csv2 = CSV.instance(s1)
743+
# csv2.equal?(csv0) # => false # Different CSV object
744+
#
745+
# All the cached objects remains available:
746+
# csv3 = CSV.instance(s0)
747+
# csv3.equal?(csv0) # true # Same CSV object
748+
# csv4 = CSV.instance(s1)
749+
# csv4.equal?(csv2) # true # Same CSV object
750+
#
751+
# ---
752+
#
753+
# When a block is given, calls the block with the created or retrieved
754+
# \CSV object; returns the block's return value:
755+
# CSV.instance(s0) {|csv| :foo } # => :foo
689756
def instance(data = $stdout, **options)
690757
# create a _signature_ for this method call, data object and options
691758
sig = [data.object_id] +
@@ -989,42 +1056,6 @@ def generate_line(row, **options)
9891056
# <tt>"rb:UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file but
9901057
# transcode it to UTF-8 before CSV parses it.
9911058
#
992-
# For convenience, an opened CSV object will delegate to many methods in class IO.
993-
# (A few have wrapper "guard code" in \CSV.) You may call:
994-
# * IO#binmode
995-
# * #binmode?
996-
# * IO#close
997-
# * IO#close_read
998-
# * IO#close_write
999-
# * IO#closed?
1000-
# * #eof
1001-
# * #eof?
1002-
# * IO#external_encoding
1003-
# * IO#fcntl
1004-
# * IO#fileno
1005-
# * #flock
1006-
# * IO#flush
1007-
# * IO#fsync
1008-
# * IO#internal_encoding
1009-
# * #ioctl
1010-
# * IO#isatty
1011-
# * #path
1012-
# * IO#pid
1013-
# * IO#pos
1014-
# * IO#pos=
1015-
# * IO#reopen
1016-
# * #rewind
1017-
# * IO#seek
1018-
# * #stat
1019-
# * IO#string
1020-
# * IO#sync
1021-
# * IO#sync=
1022-
# * IO#tell
1023-
# * #to_i
1024-
# * #to_io
1025-
# * IO#truncate
1026-
# * IO#tty?
1027-
#
10281059
def open(filename, mode="r", **options)
10291060
# wrap a File opened with the remaining +args+ with no newline
10301061
# decorator
@@ -1094,11 +1125,12 @@ def parse(str, **options, &block)
10941125
# Returns the new \Array created by parsing the first line of +string+ or +io+
10951126
# using the specified +options+.
10961127
#
1097-
# Argument +string+ should be a \String object;
1098-
# it will be put into a new \StringIO object positioned at the beginning.
1099-
#
1100-
# Argument +io+ should be an \IO object; it will be positioned at the beginning.
1101-
#
1128+
# - Argument +string+ should be a \String object;
1129+
# it will be put into a new StringIO object positioned at the beginning.
1130+
# - Argument +io+ should be an IO object, positioned at the beginning.
1131+
# To position at the end, for appending, use method CSV.generate.
1132+
# For any other positioning, pass a preset \StringIO object instead.
1133+
# - Argument +options+: see {Options for Generating}[#class-CSV-label-Options+for+Generating]
11021134
# For +options+, see {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
11031135
#
11041136
# ---
@@ -1176,23 +1208,19 @@ def table(path, **options)
11761208
# Returns the new \CSV object created using +string+ or +io+
11771209
# and the specified +options+.
11781210
#
1179-
# Argument +string+ should be a \String object;
1180-
# it will be put into a new \StringIO object positioned at the beginning.
1181-
#
1182-
# Argument +io+ should be an \IO object; it will be positioned at the beginning.
1183-
#
1184-
# To position at the end, for appending, use method CSV.generate.
1185-
# For any other positioning, pass a preset StringIO object instead.
1186-
#
1187-
# In addition to the \CSV instance methods, several \IO
1188-
# methods are delegated. See CSV::open for a complete list.
1189-
#
1190-
# For +options+, see:
1191-
# * {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1192-
# * {Options for Generating}[#class-CSV-label-Options+for+Generating]
1193-
#
1194-
# For performance reasons, the options cannot be overridden
1195-
# in a \CSV object, so the options specified here will endure.
1211+
# - Argument +string+ should be a \String object;
1212+
# it will be put into a new StringIO object positioned at the beginning.
1213+
# - Argument +io+ should be an IO object, positioned at the beginning.
1214+
# To position at the end, for appending, use method CSV.generate.
1215+
# For any other positioning, pass a preset StringIO object instead.
1216+
# - Argument +options+: See:
1217+
# * {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1218+
# * {Options for Generating}[#class-CSV-label-Options+for+Generating]
1219+
# For performance reasons, the options cannot be overridden
1220+
# in a \CSV object, so those specified here will endure.
1221+
#
1222+
# In addition to the \CSV instance methods, several \IO methods are delegated.
1223+
# See {Delegated Methods}[#class-CSV-label-Delegated+Methods].
11961224
#
11971225
# ---
11981226
#

0 commit comments

Comments
 (0)