Closes the current file and skips to the next file in ARGV. If there are no more files to open, just closes the current file. STDIN
will not be closed.
For example:
$ ruby argf.rb foo bar ARGF.filename #=> "foo" ARGF.close ARGF.filename #=> "bar" ARGF.close
Returns true if the current file has been closed; false otherwise. Use ARGF.close
to actually close the current file.
Reads CSV input and writes CSV output.
For each input row:
Forms the data into:
A CSV::Row
object, if headers are in use.
An Array of Arrays, otherwise.
Calls the block with that object.
Appends the block’s return value to the output.
Arguments:
CSV source:
Argument in_string
, if given, should be a String object; it will be put into a new StringIO
object positioned at the beginning.
Argument in_io
, if given, should be an IO
object that is open for reading; on return, the IO
object will be closed.
If neither in_string
nor in_io
is given, the input stream defaults to ARGF.
CSV output:
Argument out_string
, if given, should be a String object; it will be put into a new StringIO
object positioned at the beginning.
Argument out_io
, if given, should be an IO
object that is ppen for writing; on return, the IO
object will be closed.
If neither out_string
nor out_io
is given, the output stream defaults to $stdout
.
Argument options
should be keyword arguments.
Each argument name that is prefixed with in_
or input_
is stripped of its prefix and is treated as an option for parsing the input. Option input_row_sep
defaults to $INPUT_RECORD_SEPARATOR
.
Each argument name that is prefixed with out_
or output_
is stripped of its prefix and is treated as an option for generating the output. Option output_row_sep
defaults to $INPUT_RECORD_SEPARATOR
.
Each argument not prefixed as above is treated as an option both for parsing the input and for generating the output.
Example:
in_string = "foo,0\nbar,1\nbaz,2\n" out_string = '' CSV.filter(in_string, out_string) do |row| row[0] = row[0].upcase row[1] *= 4 end out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
Returns the value that determines whether headers are used; used for parsing; see {Option headers
}:
CSV.new('').headers # => nil
With no block, installs a field converter (a Proc).
With a block, defines and installs a custom field converter.
Returns the Array of installed field converters.
Argument converter_name
, if given, should be the name of an existing field converter.
See Field Converters.
With no block, installs a field converter:
csv = CSV.new('') csv.convert(:integer) csv.convert(:float) csv.convert(:date) csv.converters # => [:integer, :float, :date]
The block, if given, is called for each field:
Argument field
is the field value.
Argument field_info
is a CSV::FieldInfo
object containing details about the field.
The examples here assume the prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Example giving a block:
csv = CSV.open(path) csv.convert {|field, field_info| p [field, field_info]; field.upcase } csv.read # => [["FOO", "0"], ["BAR", "1"], ["BAZ", "2"]]
Output:
["foo", #<struct CSV::FieldInfo index=0, line=1, header=nil>] ["0", #<struct CSV::FieldInfo index=1, line=1, header=nil>] ["bar", #<struct CSV::FieldInfo index=0, line=2, header=nil>] ["1", #<struct CSV::FieldInfo index=1, line=2, header=nil>] ["baz", #<struct CSV::FieldInfo index=0, line=3, header=nil>] ["2", #<struct CSV::FieldInfo index=1, line=3, header=nil>]
The block need not return a String object:
csv = CSV.open(path) csv.convert {|field, field_info| field.to_sym } csv.read # => [[:foo, :"0"], [:bar, :"1"], [:baz, :"2"]]
If converter_name
is given, the block is not called:
csv = CSV.open(path) csv.convert(:integer) {|field, field_info| fail 'Cannot happen' } csv.read # => [["foo", 0], ["bar", 1], ["baz", 2]]
Raises a parse-time exception if converter_name
is not the name of a built-in field converter:
csv = CSV.open(path) csv.convert(:nosuch) => [nil] # Raises NoMethodError (undefined method `arity' for nil:NilClass) csv.read
This method must be overridden by subclasses and should return the object method calls are being delegated to.
Returns the current object method calls are being delegated to.
Returns revision information for the erb.rb module.
Sets optional filename and line number that will be used in ERB
code evaluation and error reporting. See also filename=
and lineno=
erb = ERB.new('<%= some_x %>') erb.render # undefined local variable or method `some_x' # from (erb):1 erb.location = ['file.erb', 3] # All subsequent error reporting would use new location erb.render # undefined local variable or method `some_x' # from file.erb:4
Set
the handling of the ordering of options and arguments. A RuntimeError
is raised if option processing has already started.
The supplied value must be a member of GetoptLong::ORDERINGS
. It alters the processing of options as follows:
REQUIRE_ORDER :
Options are required to occur before non-options.
Processing of options ends as soon as a word is encountered that has not been preceded by an appropriate option flag.
For example, if -a and -b are options which do not take arguments, parsing command line arguments of ‘-a one -b two’ would result in ‘one’, ‘-b’, ‘two’ being left in ARGV, and only (‘-a’, ”) being processed as an option/arg pair.
This is the default ordering, if the environment variable POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
PERMUTE :
Options can occur anywhere in the command line parsed. This is the default behavior.
Every sequence of words which can be interpreted as an option (with or without argument) is treated as an option; non-option words are skipped.
For example, if -a does not require an argument and -b optionally takes an argument, parsing ‘-a one -b two three’ would result in (‘-a’,”) and (‘-b’, ‘two’) being processed as option/arg pairs, and ‘one’,‘three’ being left in ARGV.
If the ordering is set to PERMUTE but the environment variable POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for compatibility with GNU getopt_long.
RETURN_IN_ORDER :
All words on the command line are processed as options. Words not preceded by a short or long option flag are passed as arguments with an option of ” (empty string).
For example, if -a requires an argument but -b does not, a command line of ‘-a one -b two three’ would result in option/arg pairs of (‘-a’, ‘one’) (‘-b’, ”), (”, ‘two’), (”, ‘three’) being processed.
Explicitly terminate option processing.
Returns true if option processing has terminated, false otherwise.
Get next option name and its argument, as an Array
of two elements.
The option name is always converted to the first (preferred) name given in the original options to GetoptLong.new
.
Example: [‘–option’, ‘value’]
Returns nil if the processing is complete (as determined by STATUS_TERMINATED
).
Returns true if the ipaddr is a loopback address.
Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.
Returns the bound receiver of the binding object.
Returns true
if this is an hermitian matrix. Raises an error if matrix is not square.
Returns true
if this is an orthogonal matrix Raises an error if matrix is not square.