Converts the contents of the database to an in-memory Hash
, then calls Hash#reject
with the specified code block, returning a new Hash
.
Replaces the contents of the database with the contents of the specified object. Takes any object which implements the each_pair
method, including Hash
and DBM
objects.
Converts the contents of the database to an array of [key, value] arrays, and returns it.
Returns the values for this struct as an Array.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a[1] #=> "123 Maple, Anytown NC"
Returns a Hash
containing the names and values for the struct’s members.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_h[:address] #=> "123 Maple, Anytown NC"
Returns a description of this struct as a string.
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. Returns nil if no information available.
Returns status information for ios as an object of type File::Stat
.
f = File.new("testfile") s = f.stat "%o" % s.mode #=> "100644" s.blksize #=> 4096 s.atime #=> Wed Apr 09 08:53:54 CDT 2003
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"
If the last argument is a hash, it’s the keyword argument to open. See IO.read
for detail.
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 ("|"
), a subprocess is created in the same way as Kernel#open
, and its output is returned.
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
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:
IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" IO.read("testfile", 20) #=> "This is line one\nThi" IO.read("testfile", 20, 10) #=> "ne one\nThis is line " IO.read("binfile", mode: "rb") #=> "\xF7\x00\x00\x0E\x12"
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 "
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.
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 all of the lines in ios, and returns them in anArray. 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 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"
Reads at most maxlen bytes from the I/O stream. It blocks only if ios has no data immediately available. It doesn’t block if some data available.
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.
It raises EOFError
on end of file.
readpartial is designed for streams such as pipe, socket, tty, etc. It blocks only when no data immediately available. This means that it blocks only when following all conditions hold.
the byte buffer in the IO
object is empty.
the content of the stream is empty.
the stream is not reached to EOF.
When readpartial blocks, it waits data or EOF on the stream. If some data is reached, readpartial returns with the data. If EOF is reached, readpartial raises EOFError
.
When readpartial doesn’t blocks, it returns or raises immediately. If the byte buffer is not empty, it returns the data in the buffer. Otherwise if the stream has some content, it returns the data in the stream. Otherwise if the stream is reached to EOF, it raises EOFError
.
r, w = IO.pipe # buffer pipe content w << "abc" # "" "abc". r.readpartial(4096) #=> "abc" "" "" r.readpartial(4096) # blocks because buffer and pipe is empty. r, w = IO.pipe # buffer pipe content w << "abc" # "" "abc" w.close # "" "abc" EOF r.readpartial(4096) #=> "abc" "" EOF r.readpartial(4096) # raises EOFError r, w = IO.pipe # buffer pipe content 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" "" ""
Note that readpartial behaves 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 retry the system call.
The latter means that readpartial is nonblocking-flag insensitive. It blocks on the situation IO#sysread
causes Errno::EWOULDBLOCK as if the fd is blocking mode.
Reads length bytes from the I/O stream.
length must be a non-negative integer or nil
.
If length is a positive integer, read
tries to read length bytes without any conversion (binary mode). It returns nil
if an EOF is encountered before anything can be read. Fewer than length bytes are returned if an EOF is encountered during the read. In the case of an integer length, the resulting string is always in ASCII-8BIT encoding.
If length is omitted or is nil
, it reads until EOF and the encoding conversion is applied, if applicable. A string is returned even if EOF is encountered before any data is read.
If length is zero, it returns an empty string (""
).
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.
When this method is called at end of file, it returns nil
or ""
, depending on length: read
, read(nil)
, and read(0)
return ""
, read(positive_integer)
returns nil
.
f = File.new("testfile") f.read(16) #=> "This is line one" # read whole file open("file") do |f| data = f.read # This returns a string even if the file is empty. # ... end # iterate over fixed length records open("fixed-record-file") do |f| while record = f.read(256) # ... end end # iterate over variable length records, # each record is prefixed by its 32-bit length open("variable-record-file") do |f| while len = f.read(4) len = len.unpack("N")[0] # 32-bit length record = f.read(len) # This returns a string even if len is 0. end end
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 length (or until EOF). This behavior is preserved even if ios 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
.
Reads a line as with IO#gets
, but raises an EOFError
on end of file.
Reads a one-character string from ios. Raises an EOFError
on end of file.
f = File.new("testfile") f.readchar #=> "h" f.readchar #=> "e"
Reads a byte as with IO#getbyte
, but raises an EOFError
on end of file.
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.