Writes the given objects to the stream; returns nil
. Appends the output record separator $OUTPUT_RECORD_SEPARATOR
($\
), if it is not nil
. See Line IO.
With argument objects
given, for each object:
Converts via its method to_s
if not a string.
Writes to the stream.
If not the last object, writes the output field separator $OUTPUT_FIELD_SEPARATOR
($,
) if it is not nil
.
With default separators:
f = File.open('t.tmp', 'w+') objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero'] p $OUTPUT_RECORD_SEPARATOR p $OUTPUT_FIELD_SEPARATOR f.print(*objects) f.rewind p f.read f.close
Output:
nil nil "00.00/10+0izerozero"
With specified separators:
$\ = "\n" $, = ',' f.rewind f.print(*objects) f.rewind p f.read
Output:
"0,0.0,0/1,0+0i,zero,zero\n"
With no argument given, writes the content of $_
(which is usually the most recent user input):
f = File.open('t.tmp', 'w+') gets # Sets $_ to the most recent user input. f.print f.close
Formats and writes objects
to the stream.
For details on format_string
, see Format Specifications.
Returns the current line number of ARGF
as a whole. This value can be set manually with ARGF.lineno=
.
For example:
ARGF.lineno #=> 0 ARGF.readline #=> "This is line 1\n" ARGF.lineno #=> 1
Sets the line number of ARGF
as a whole to the given Integer
.
ARGF
sets the line number automatically as you read data, so normally you will not need to set it explicitly. To access the current line number use ARGF.lineno
.
For example:
ARGF.lineno #=> 0 ARGF.readline #=> "This is line 1\n" ARGF.lineno #=> 1 ARGF.lineno = 0 #=> 0 ARGF.lineno #=> 0
Returns “ARGF”.
Creates or retrieves cached CSV objects. For arguments and options, see CSV.new
.
This API is not Ractor-safe.
With no block given, returns a CSV object.
The first call to instance
creates and caches a CSV object:
s0 = 's0' csv0 = CSV.instance(s0) csv0.class # => CSV
Subsequent calls to instance
with that same string
or io
retrieve that same cached object:
csv1 = CSV.instance(s0) csv1.class # => CSV csv1.equal?(csv0) # => true # Same CSV object
A subsequent call to instance
with a different string
or io
creates and caches a different CSV object.
s1 = 's1' csv2 = CSV.instance(s1) csv2.equal?(csv0) # => false # Different CSV object
All the cached objects remains available:
csv3 = CSV.instance(s0) csv3.equal?(csv0) # true # Same CSV object csv4 = CSV.instance(s1) csv4.equal?(csv2) # true # Same CSV object
When a block is given, calls the block with the created or retrieved CSV object; returns the block’s return value:
CSV.instance(s0) {|csv| :foo } # => :foo
Alias for CSV.read
.
Returns the count of the rows parsed or generated.
Parsing:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string) CSV.open(path) do |csv| csv.each do |row| p [csv.lineno, row] end end
Output:
[1, ["foo", "0"]] [2, ["bar", "1"]] [3, ["baz", "2"]]
Generating:
CSV.generate do |csv| p csv.lineno; csv << ['foo', 0] p csv.lineno; csv << ['bar', 1] p csv.lineno; csv << ['baz', 2] end
Output:
0 1 2
Returns the line most recently read:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string) CSV.open(path) do |csv| csv.each do |row| p [csv.lineno, csv.line] end end
Output:
[1, "foo,0\n"] [2, "bar,1\n"] [3, "baz,2\n"]
Rewinds the underlying IO
object and resets CSV’s lineno() counter.
Returns a String showing certain properties of self
:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" csv = CSV.new(string, headers: true) s = csv.inspect s # => "#<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:\",\" row_sep:\"\\n\" quote_char:\"\\\"\" headers:true>"
Sets the ordering; see Ordering; returns the new ordering.
If the given ordering
is PERMUTE
and environment variable POSIXLY_CORRECT
is defined, sets the ordering to REQUIRE_ORDER
; otherwise sets the ordering to ordering
:
options = GetoptLong.new options.ordering == GetoptLong::PERMUTE # => true options.ordering = GetoptLong::RETURN_IN_ORDER options.ordering == GetoptLong::RETURN_IN_ORDER # => true ENV['POSIXLY_CORRECT'] = 'true' options.ordering = GetoptLong::PERMUTE options.ordering == GetoptLong::REQUIRE_ORDER # => true
Raises an exception if ordering
is invalid.
Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, “255.255.255.0”, etc.)
Returns true if the given ipaddr is in the range.
e.g.:
require 'ipaddr' net1 = IPAddr.new("192.168.2.0/24") net2 = IPAddr.new("192.168.2.100") net3 = IPAddr.new("192.168.3.0") net4 = IPAddr.new("192.168.2.0/16") p net1.include?(net2) #=> true p net1.include?(net3) #=> false p net1.include?(net4) #=> false p net4.include?(net1) #=> true
Returns a string containing a human-readable representation of the ipaddr. (“#<IPAddr: family:address/mask>”)
Returns the netmask in string format e.g. 255.255.0.0
Set
current netmask to given mask.
Returns true
if the log level allows entries with severity Logger::INFO to be written, false
otherwise. See Log Level.
Sets the log level to Logger::INFO. See Log Level.
Equivalent to calling add
with severity Logger::INFO
.
Returns an incremented value of default
according to arg
.
Puts option summary into to
and returns to
. Yields each line if a block is given.
to
Output destination, which must have method <<. Defaults to [].
width
Width of left side, defaults to @summary_width.
max
Maximum length allowed for left side, defaults to width
- 1.
indent
Indentation, defaults to @summary_indent.