Reads a one-character string from ios. Raises an EOFError
on end of file.
f = File.new("testfile") f.readchar #=> "h" f.readchar #=> "e"
Seeks to a given offset anInteger in the stream according to the value of whence:
:CUR or IO::SEEK_CUR | Seeks to _amount_ plus current position ----------------------+-------------------------------------------------- :END or IO::SEEK_END | Seeks to _amount_ plus end of stream (you | probably want a negative value for _amount_) ----------------------+-------------------------------------------------- :SET or IO::SEEK_SET | Seeks to the absolute location given by _amount_
Example:
f = File.new("testfile") f.seek(-13, IO::SEEK_END) #=> 0 f.readline #=> "And so on...\n"
Closes ios and flushes any pending writes to the operating system. The stream is unavailable for any further data operations; an IOError
is raised if such an attempt is made. I/O streams are automatically closed when they are claimed by the garbage collector.
If ios is opened by IO.popen
, close
sets $?
.
Calling this method on closed IO
object is just ignored since Ruby 2.3.
Returns true
if ios is completely closed (for duplex streams, both reader and writer), false
otherwise.
f = File.new("testfile") f.close #=> nil f.closed? #=> true f = IO.popen("/bin/sh","r+") f.close_write #=> nil f.closed? #=> false f.close_read #=> nil f.closed? #=> true
Seeks to a given offset in the stream according to the value of whence (see IO#seek
for values of whence). Returns the new offset into the file.
f = File.new("testfile") f.sysseek(-13, IO::SEEK_END) #=> 53 f.sysread(10) #=> "And so on."
Announce an intention to access data from the current file in a specific pattern. On platforms that do not support the posix_fadvise(2) system call, this method is a no-op.
advice is one of the following symbols:
No advice to give; the default assumption for an open file.
The data will be accessed sequentially with lower offsets read before higher ones.
The data will be accessed in random order.
The data will be accessed in the near future.
The data will not be accessed in the near future.
The data will only be accessed once.
The semantics of a piece of advice are platform-dependent. See man 2 posix_fadvise for details.
“data” means the region of the current file that begins at offset and extends for len bytes. If len is 0, the region ends at the last byte of the file. By default, both offset and len are 0, meaning that the advice applies to the entire file.
If an error occurs, one of the following exceptions will be raised:
IOError
The IO
stream is closed.
The file descriptor of the current file is invalid.
An invalid value for advice was given.
The file descriptor of the current file refers to a FIFO or pipe. (Linux raises Errno::EINVAL in this case).
TypeError
Either advice was not a Symbol
, or one of the other arguments was not an Integer
.
RangeError
One of the arguments given was too big/small.
Errno
exceptions are also possible.
Provides a mechanism for issuing low-level commands to control or query I/O devices. Arguments and results are platform dependent. If arg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes. On Unix platforms, see ioctl(2)
for details. Not implemented on all platforms.
Returns true
if the underlying file descriptor of ios will be closed automatically at its finalization, otherwise false
.
Sets auto-close flag.
f = open("/dev/null") IO.for_fd(f.fileno) # ... f.gets # may cause Errno::EBADF f = open("/dev/null") IO.for_fd(f.fileno).autoclose = false # ... f.gets # won't cause Errno::EBADF
If called without a block, this is synonymous to GDBM::new
. If a block is given, the new GDBM
instance will be passed to the block as a parameter, and the corresponding database file will be closed after the execution of the block code has been finished.
Example for an open call with a block:
require 'gdbm' GDBM.open("fruitstore.db") do |gdbm| gdbm.each_pair do |key, value| print "#{key}: #{value}\n" end end
Closes the associated database file.
Returns true if the associated database file has been closed.
Returns a new array of all key-value pairs of the database for which block evaluates to true.
Returns true if the database is empty.
Removes all the key-value pairs within gdbm.
Returns a hash created by using gdbm’s values as keys, and the keys as values.
Returns true if the given key k exists within the database. Returns false otherwise.
Returns the first object in the range, or an array of the first n
elements.
(10..20).first #=> 10 (10..20).first(3) #=> [10, 11, 12]
Returns true
if obj
is an element of the range, false
otherwise.
("a".."z").include?("g") #=> true ("a".."z").include?("A") #=> false ("a".."z").include?("cc") #=> false
If you need to ensure obj
is between begin
and end
, use cover?
("a".."z").cover?("cc") #=> true
If begin and end are numeric, include?
behaves like cover?
(1..3).include?(1.5) # => true
Returns true
if obj
is between the begin and end of the range.
This tests begin <= obj <= end
when exclude_end?
is false
and begin <= obj < end
when exclude_end?
is true
.
If called with a Range
argument, returns true
when the given range is covered by the receiver, by comparing the begin and end values. If the argument can be treated as a sequence, this method treats it that way. In the specific case of (a..b).cover?(c...d)
with a <= c && b < d
, the end of the sequence must be calculated, which may exhibit poor performance if c
is non-numeric. Returns false
if the begin value of the range is larger than the end value. Also returns false
if one of the internal calls to <=>
returns nil
(indicating the objects are not comparable).
("a".."z").cover?("c") #=> true ("a".."z").cover?("5") #=> false ("a".."z").cover?("cc") #=> true ("a".."z").cover?(1) #=> false (1..5).cover?(2..3) #=> true (1..5).cover?(0..6) #=> false (1..5).cover?(1...6) #=> true
Returns the value of the case-insensitive flag.
/a/.casefold? #=> false /a/i.casefold? #=> true /(?i:a)/.casefold? #=> false
Returns true if the set contains no elements.
Removes all elements and returns self.
set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}> set.clear #=> #<Set: {}> set #=> #<Set: {}>
Returns true if the set is a subset of the given set.