Returns the value as a string for inspection.
Date.new(2001,2,3).inspect #=> "#<Date: 2001-02-03>" DateTime.new(2001,2,3,4,5,6,'-7').inspect #=> "#<DateTime: 2001-02-03T04:05:06-07:00>"
This method is equivalent to strftime(‘%F’).
Creates a DateTime
object denoting the given ordinal date.
DateTime.ordinal(2001,34) #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...> DateTime.ordinal(2001,34,4,5,6,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.ordinal(2001,-332,-20,-55,-54,'+7') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Creates a new DateTime
object by parsing from a string according to some typical XML
Schema formats.
DateTime.xmlschema('2001-02-03T04:05:06+07:00') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
This method is equivalent to strftime(‘%FT%T%:z’). The optional argument n
is the number of digits for fractional seconds.
DateTime.parse('2001-02-03T04:05:06.123456789+07:00').iso8601(9) #=> "2001-02-03T04:05:06.123456789+07:00"
Parses date
as a dateTime defined by the XML
Schema and converts it to a Time
object. The format is a restricted version of the format defined by ISO 8601.
ArgumentError
is raised if date
is not compliant with the format or if the Time
class cannot represent specified date.
See xmlschema
for more information on this format.
require 'time' Time.xmlschema("2011-10-05T22:26:12-04:00") #=> 2011-10-05 22:26:12-04:00
You must require ‘time’ to use this method.
Returns a string which represents the time as a dateTime defined by XML
Schema:
CCYY-MM-DDThh:mm:ssTZD CCYY-MM-DDThh:mm:ss.sssTZD
where TZD is Z or [+-]hh:mm.
If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
fractional_digits
specifies a number of digits to use for fractional seconds. Its default value is 0.
require 'time' t = Time.now t.iso8601 # => "2011-10-05T22:26:12-04:00"
You must require ‘time’ to use this method.
Returns a detailed string representing time. Unlike to_s
, preserves nanoseconds in the representation for easier debugging.
t = Time.now t.inspect #=> "2012-11-10 18:16:12.261257655 +0100" t.strftime "%Y-%m-%d %H:%M:%S.%N %z" #=> "2012-11-10 18:16:12.261257655 +0100" t.utc.inspect #=> "2012-11-10 17:16:12.261257655 UTC" t.strftime "%Y-%m-%d %H:%M:%S.%N UTC" #=> "2012-11-10 17:16:12.261257655 UTC"
Returns a Hash
(not a DBM
database) created by using each value in the database as a key, with the corresponding key as its value.
Returns true if the database contains the specified key, false otherwise.
Returns a description of this struct as a string.
Tries to set console size. The effect depends on the platform and the running environment.
You must require ‘io/console’ to use this method.
Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep.
a = IO.readlines("testfile") a[0] #=> "This is line one\n" b = IO.readlines("testfile", chomp: true) b[0] #=> "This is line one"
If the last argument is a hash, it’s the keyword argument to open.
The options hash accepts the following keys:
When the optional chomp
keyword argument has a true value, \n
, \r
, and \r\n
will be removed from the end of each line.
See also IO.read
for details about open_args.
Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). binread ensures the file is closed before returning. The open mode would be "rb:ASCII-8BIT"
.
IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" IO.binread("testfile", 20) #=> "This is line one\nThi" IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
Same as IO.write
except opening the file in binary mode and ASCII-8BIT encoding ("wb:ASCII-8BIT"
).
Writes the given object(s) to ios. Returns nil
.
The stream must be opened for writing. Each given object that isn’t a string will be converted by calling its to_s
method. When called without arguments, prints the contents of $_
.
If the output field separator ($,
) is not nil
, it is inserted between objects. If the output record separator ($\
) is not nil
, it is appended to the output.
$stdout.print("This is ", 100, " percent.\n")
produces:
This is 100 percent.
Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf
for details.
This is a deprecated alias for each_codepoint
.
Returns the current line number in ios. The stream must be opened for reading. lineno
counts the number of times gets
is called rather than the number of newlines encountered. The two values will differ if gets
is called with a separator other than newline.
Methods that use $/
like each
, lines
and readline
will also increment lineno
.
See also the $.
variable.
f = File.new("testfile") f.lineno #=> 0 f.gets #=> "This is line one\n" f.lineno #=> 1 f.gets #=> "This is line two\n" f.lineno #=> 2
Manually sets the current line number to the given value. $.
is updated only on the next read.
f = File.new("testfile") f.gets #=> "This is line one\n" $. #=> 1 f.lineno = 1000 f.lineno #=> 1000 $. #=> 1 # lineno of last read f.gets #=> "This is line two\n" $. #=> 1001 # lineno of last read
Reads all of the lines in ios, and returns them in an array. Lines are separated by the optional sep. If sep is nil
, the rest of the stream is returned as a single record. If the first argument is an integer, or an optional second argument is given, the returning string would not be longer than the given value in bytes. The stream must be opened for reading or an IOError
will be raised.
f = File.new("testfile") f.readlines[0] #=> "This is line one\n" f = File.new("testfile", chomp: true) f.readlines[0] #=> "This is line one"
See IO.readlines
for details about getline_args.
Positions ios to the beginning of input, resetting lineno
to zero.
f = File.new("testfile") f.readline #=> "This is line one\n" f.rewind #=> 0 f.lineno #=> 0 f.readline #=> "This is line one\n"
Note that it cannot be used with streams such as pipes, ttys, and sockets.