Returns a hash of parsed elements.
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.
Creates a new Date
object by parsing from a string according to some typical XML Schema formats.
Date.xmlschema('2001-02-03') #=> #<Date: 2001-02-03 ...>
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.
Returns true if the date is on or after the day of calendar reform.
Date.new(1582,10,15).gregorian? #=> true (Date.new(1582,10,15) - 1).gregorian? #=> false
This method is equivalent to new_start
(Date::GREGORIAN
).
This method is equivalent to strftime(‘%F’).
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 time
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 time
is not compliant with the format or if the Time
class cannot represent the specified time.
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 the member names of the Struct
descendant as an array:
Customer = Struct.new(:name, :address, :zip) Customer.members # => [:name, :address, :zip]
Returns the member names from self
as an array:
Customer = Struct.new(:name, :address, :zip) Customer.new.members # => [:name, :address, :zip]
Related: to_a
.
Returns number of bytes that can be read without blocking. Returns zero if no information available.
Returns true
if input available without blocking, or false
.
Executes the block for every line in the named I/O port, where lines are separated by sep.
If no block is given, an enumerator is returned instead.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.foreach
to disable the behavior of subprocess invocation.
File.foreach("testfile") {|x| print "GOT ", x } IO.foreach("| cat testfile") {|x| print "GOT ", x }
produces:
GOT This is line one GOT This is line two GOT This is line three GOT And so on...
If the last argument is a hash, it’s the keyword argument to open. See IO.readlines
for details about getline_args. And see also IO.read
for details about open_args.
Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.readlines
to disable the behavior of subprocess invocation.
a = File.readlines("testfile") a[0] #=> "This is line one\n" b = File.readlines("testfile", chomp: true) b[0] #=> "This is line one" IO.readlines("|ls -a") #=> [".\n", "..\n", ...]
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 name
and open_args.
Opens the file, optionally seeks to the given offset
, then returns length
bytes (defaulting to the rest of the file). read
ensures the file is closed before returning.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.read
to disable the behavior of subprocess invocation.
The options hash accepts the following keys:
string or encoding
Specifies the encoding of the read string. :encoding
will be ignored if length
is specified. See Encoding.aliases
for possible encodings.
string or integer
Specifies the mode argument for open(). It must start with an “r”, otherwise it will cause an error. See IO.new
for the list of possible modes.
array
Specifies arguments for open() as an array. This key can not be used in combination with either :encoding
or :mode
.
Examples:
File.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" File.read("testfile", 20) #=> "This is line one\nThi" File.read("testfile", 20, 10) #=> "ne one\nThis is line " File.read("binfile", mode: "rb") #=> "\xF7\x00\x00\x0E\x12" IO.read("|ls -a") #=> ".\n..\n"...
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"
.
If name
starts with a pipe character ("|"
) and the receiver is the IO
class, a subprocess is created in the same way as Kernel#open
, and its output is returned. Consider to use File.binread
to disable the behavior of subprocess invocation.
File.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" File.binread("testfile", 20) #=> "This is line one\nThi" File.binread("testfile", 20, 10) #=> "ne one\nThis is line " IO.binread("| cat testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
See also IO.read
for details about name
and open_args.
Reassociates ios with the I/O stream given in other_IO or to a new stream opened on path. This may dynamically change the actual class of this stream. The mode
and opt
parameters accept the same values as IO.open
.
f1 = File.new("testfile") f2 = File.new("testfile") f2.readlines[0] #=> "This is line one\n" f2.reopen(f1) #=> #<File:testfile> f2.readlines[0] #=> "This is line one\n"
Reads maxlen bytes from ios using a low-level read and returns them as a string. Do not mix with other methods that read from ios or you may get unpredictable results.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
Raises SystemCallError
on error and EOFError
at end of file.
f = File.new("testfile") f.sysread(16) #=> "This is line one"
Reads maxlen bytes from ios using the pread system call and returns them as a string without modifying the underlying descriptor offset. This is advantageous compared to combining IO#seek
and IO#read
in that it is atomic, allowing multiple threads/process to share the same IO
object for reading the file at various locations. This bypasses any userspace buffering of the IO
layer. If the optional outbuf argument is present, it must reference a String
, which will receive the data. Raises SystemCallError
on error, EOFError
at end of file and NotImplementedError
if platform does not implement the system call.
File.write("testfile", "This is line one\nThis is line two\n") File.open("testfile") do |f| p f.read # => "This is line one\nThis is line two\n" p f.pread(12, 0) # => "This is line" p f.pread(9, 8) # => "line one\n" end
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.
Reads up to maxlen
bytes from the stream; returns a string (either a new string or the given out_string
). Its encoding is:
The unchanged encoding of out_string
, if out_string
is given.
ASCII-8BIT, otherwise.
Contains maxlen
bytes from the stream, if available.
Otherwise contains all available bytes, if any available.
Otherwise is an empty string.
With the single non-negative integer argument maxlen
given, returns a new string:
f = File.new('t.txt') f.readpartial(30) # => "This is line one.\nThis is the" f.readpartial(30) # => " second line.\nThis is the thi" f.readpartial(30) # => "rd line.\n" f.eof # => true f.readpartial(30) # Raises EOFError.
With both argument maxlen
and string argument out_string
given, returns modified out_string
:
f = File.new('t.txt') s = 'foo' f.readpartial(30, s) # => "This is line one.\nThis is the" s = 'bar' f.readpartial(0, s) # => ""
This method is useful for a stream such as a pipe, a socket, or a tty. It blocks only when no data is immediately available. This means that it blocks only when all of the following are true:
The byte buffer in the stream is empty.
The content of the stream is empty.
The stream is not at EOF.
When blocked, the method waits for either more data or EOF on the stream:
If more data is read, the method returns the data.
If EOF is reached, the method raises EOFError
.
When not blocked, the method responds immediately:
Returns data from the buffer if there is any.
Otherwise returns data from the stream if there is any.
Otherwise raises EOFError
if the stream has reached EOF.
Note that this method is similar to sysread. The differences are:
If the byte buffer is not empty, read from the byte buffer instead of “sysread for buffered IO
(IOError
)”.
It doesn’t cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retries the system call.
The latter means that readpartial is non-blocking-flag insensitive. It blocks on the situation IO#sysread
causes Errno::EWOULDBLOCK as if the fd is blocking mode.
Examples:
# # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc". r.readpartial(4096) # => "abc" "" "" r.readpartial(4096) # (Blocks because buffer and pipe are empty.) # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << 'abc' # "" "abc" w.close # "" "abc" EOF r.readpartial(4096) # => "abc" "" EOF r.readpartial(4096) # raises EOFError # # Returned Buffer Content Pipe Content r, w = IO.pipe # w << "abc\ndef\n" # "" "abc\ndef\n" r.gets # => "abc\n" "def\n" "" w << "ghi\n" # "def\n" "ghi\n" r.readpartial(4096) # => "def\n" "" "ghi\n" r.readpartial(4096) # => "ghi\n" "" ""
Reads bytes from the stream (in binary mode):
If maxlen
is nil
, reads all bytes.
Otherwise reads maxlen
bytes, if available.
Otherwise reads all bytes.
Returns a string (either a new string or the given out_string
) containing the bytes read. The encoding of the string depends on both maxLen
and out_string
:
maxlen
is nil
: uses internal encoding of self
(regardless of whether out_string
was given).
maxlen
not nil
:
out_string
given: encoding of out_string
not modified.
out_string
not given: ASCII-8BIT is used.
Without Argument out_string
When argument out_string
is omitted, the returned value is a new string:
f = File.new('t.txt') f.read # => "This is line one.\nThis is the second line.\nThis is the third line.\n" f.rewind f.read(40) # => "This is line one.\r\nThis is the second li" f.read(40) # => "ne.\r\nThis is the third line.\r\n" f.read(40) # => nil
If maxlen
is zero, returns an empty string.
With Argument out_string
When argument out_string
is given, the returned value is out_string
, whose content is replaced:
f = File.new('t.txt') s = 'foo' # => "foo" f.read(nil, s) # => "This is line one.\nThis is the second line.\nThis is the third line.\n" s # => "This is line one.\nThis is the second line.\nThis is the third line.\n" f.rewind s = 'bar' f.read(40, s) # => "This is line one.\r\nThis is the second li" s # => "This is line one.\r\nThis is the second li" s = 'baz' f.read(40, s) # => "ne.\r\nThis is the third line.\r\n" s # => "ne.\r\nThis is the third line.\r\n" s = 'bat' f.read(40, s) # => nil s # => ""
Note that this method behaves like the fread() function in C. This means it retries to invoke read(2) system calls to read data with the specified maxlen (or until EOF).
This behavior is preserved even if the stream is in non-blocking mode. (This method is non-blocking-flag insensitive as other methods.)
If you need the behavior like a single read(2) system call, consider readpartial
, read_nonblock
, and sysread
.