Reads from the IO
until the given pattern
matches or the timeout
is over.
It returns an array with the read buffer, followed by the matches. If a block is given, the result is yielded to the block and returns nil.
When called without a block, it waits until the input that matches the given pattern
is obtained from the IO
or the time specified as the timeout passes. An array is returned when the pattern is obtained from the IO
. The first element of the array is the entire string obtained from the IO
until the pattern matches, followed by elements indicating which the pattern which matched to the anchor in the regular expression.
The optional timeout parameter defines, in seconds, the total time to wait for the pattern. If the timeout expires or eof is found, nil is returned or yielded. However, the buffer in a timeout session is kept for the next expect call. The default timeout is 9999999 seconds.
Immediately writes to disk all data buffered in the stream, via the operating system’s fsync(2)
.
Note this difference:
IO#sync=
: Ensures that data is flushed from the stream’s internal buffers, but does not guarantee that the operating system actually writes the data to disk.
IO#fsync
: Ensures both that data is flushed from internal buffers, and that data is written to disk.
Raises an exception if the operating system does not support fsync(2)
.
Immediately writes to disk all data buffered in the stream, via the operating system’s: fdatasync(2)
, if supported, otherwise via fsync(2)
, if supported; otherwise raises an exception.
Returns the current sync mode of the stream. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered by Ruby internally. See also fsync
.
f = File.open('t.tmp', 'w') f.sync # => false f.sync = true f.sync # => true
Sets the sync mode for the stream to the given value; returns the given value.
Values for the sync mode:
true
: All output is immediately flushed to the underlying operating system and is not buffered internally.
false
: Output may be buffered internally.
Example;
f = File.open('t.tmp', 'w') f.sync # => false f.sync = true f.sync # => true
Related: IO#fsync
.
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" "" ""
Pushes back bytes (passed as a parameter) onto ios, such that a subsequent buffered read will return it. It is only guaranteed to support a single byte, and only if ungetbyte or ungetc has not already been called on ios since the previous read of at least a single byte from ios. However, it can support additional bytes if there is space in the internal buffer to allow for it.
f = File.new("testfile") #=> #<File:testfile> b = f.getbyte #=> 0x38 f.ungetbyte(b) #=> nil f.getbyte #=> 0x38
If given an integer, only uses the lower 8 bits of the integer as the byte to push.
f = File.new("testfile") #=> #<File:testfile> f.ungetbyte(0x102) #=> nil f.getbyte #=> 0x2
Calling this method prepends to the existing buffer, even if the method has already been called previously:
f = File.new("testfile") #=> #<File:testfile> f.ungetbyte("ab") #=> nil f.ungetbyte("cd") #=> nil f.read(5) #=> "cdab8"
Has no effect with unbuffered reads (such as IO#sysread
).
Pushes back characters (passed as a parameter) onto ios, such that a subsequent buffered read will return it. It is only guaranteed to support a single byte, and only if ungetbyte or ungetc has not already been called on ios since the previous read of at least a single byte from ios. However, it can support additional bytes if there is space in the internal buffer to allow for it.
f = File.new("testfile") #=> #<File:testfile> c = f.getc #=> "8" f.ungetc(c) #=> nil f.getc #=> "8"
If given an integer, the integer must represent a valid codepoint in the external encoding of ios.
Calling this method prepends to the existing buffer, even if the method has already been called previously:
f = File.new("testfile") #=> #<File:testfile> f.ungetc("ab") #=> nil f.ungetc("cd") #=> nil f.read(5) #=> "cdab8"
Has no effect with unbuffered reads (such as IO#sysread
).
Returns a string representation of self
:
f = File.open('t.txt') f.inspect # => "#<File:t.txt>"
Returns a string containing a detailed summary of the keys and values.
Returns a string representation of self
, including begin.inspect
and end.inspect
:
(1..4).inspect # => "1..4" (1...4).inspect # => "1...4" (1..).inspect # => "1.." (..4).inspect # => "..4"
Note that returns from to_s
and inspect
may differ:
('a'..'d').to_s # => "a..d" ('a'..'d').inspect # => "\"a\"..\"d\""
Related: Range#to_s
.
Returns true
if object
is an element of self
, false
otherwise:
(1..4).include?(2) # => true (1..4).include?(5) # => false (1..4).include?(4) # => true (1...4).include?(4) # => false ('a'..'d').include?('b') # => true ('a'..'d').include?('e') # => false ('a'..'d').include?('B') # => false ('a'..'d').include?('d') # => true ('a'...'d').include?('d') # => false
If begin and end are numeric, include?
behaves like cover?
(1..3).include?(1.5) # => true (1..3).cover?(1.5) # => true
But when not numeric, the two methods may differ:
('a'..'d').include?('cc') # => false ('a'..'d').cover?('cc') # => true
Related: Range#cover?
.
Range#member?
is an alias for Range#include?
.
Returns the count of elements, based on an argument or block criterion, if given.
With no argument and no block given, returns the number of elements:
(1..4).count # => 4 (1...4).count # => 3 ('a'..'d').count # => 4 ('a'...'d').count # => 3 (1..).count # => Infinity (..4).count # => Infinity
With argument object
, returns the number of object
found in self
, which will usually be zero or one:
(1..4).count(2) # => 1 (1..4).count(5) # => 0 (1..4).count('a') # => 0
With a block given, calls the block with each element; returns the number of elements for which the block returns a truthy value:
(1..4).count {|element| element < 3 } # => 2
Related: Range#size
.
Produce a nicely formatted string-version of rxp. Perhaps surprisingly, #inspect
actually produces the more natural version of the string than #to_s
.
/ab+c/ix.inspect #=> "/ab+c/ix"
Returns true if the set contains the given object.
Note that include?
and member?
do not test member equality using ==
as do other Enumerables.
See also Enumerable#include?
Returns true if the set and the given enumerable have at least one element in common.
Set[1, 2, 3].intersect? Set[4, 5] #=> false Set[1, 2, 3].intersect? Set[3, 4] #=> true Set[1, 2, 3].intersect? 4..5 #=> false Set[1, 2, 3].intersect? [3, 4] #=> true
Equivalent to Set#delete_if
, but returns nil if no changes were made. Returns an enumerator if no block is given.
Deletes every element that appears in the given enumerable object and returns self.
Returns a string containing a human-readable representation of the set (“#<Set: {element1, element2, …}>”).
The opposite of Pathname#absolute?
It returns false
if the pathname begins with a slash.
p = Pathname.new('/im/sure') p.relative? #=> false p = Pathname.new('not/so/sure') p.relative? #=> true
Returns pathname. This method is deprecated and will be removed in Ruby 3.2.