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.
Same as IO.write
except opening the file in binary mode and ASCII-8BIT encoding ("wb: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.binwrite
to disable the behavior of subprocess invocation.
See also IO.read
for details about name
and open_args.
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.
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.
Repositions the stream to its beginning, setting both the position and the line number to zero; see Position and Line Number:
f = File.open('t.txt') f.tell # => 0 f.lineno # => 0 f.readline # => "This is line one.\n" f.tell # => 19 f.lineno # => 1 f.rewind # => 0 f.tell # => 0 f.lineno # => 0
Note that this method cannot be used with streams such as pipes, ttys, and sockets.
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 the object that defines the beginning of self
.
(1..4).begin # => 1 (..2).begin # => nil
Related: Range#first
, Range#end
.
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 a MatchData
object describing the match, or nil
if there was no match. This is equivalent to retrieving the value of the special variable $~
following a normal match. If the second parameter is present, it specifies the position in the string to begin the search.
/(.)(.)(.)/.match("abc")[2] #=> "b" /(.)(.)/.match("abc", 1)[2] #=> "c"
If a block is given, invoke the block with MatchData
if match succeed, so that you can write
/M(.*)/.match("Matz") do |m| puts m[0] puts m[1] end
instead of
if m = /M(.*)/.match("Matz") puts m[0] puts m[1] end
The return value is a value from block execution in this case.
Returns true
or false
to indicate whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
/R.../.match?("Ruby") #=> true /R.../.match?("Ruby", 1) #=> false /P.../.match?("Ruby") #=> false $& #=> nil
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 the Encoding
object that represents the encoding of obj.
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
Returns true if the set and the given enumerable have no element in common. This method is the opposite of intersect?
.
Set[1, 2, 3].disjoint? Set[3, 4] #=> false Set[1, 2, 3].disjoint? Set[4, 5] #=> true Set[1, 2, 3].disjoint? [3, 4] #=> false Set[1, 2, 3].disjoint? 4..5 #=> true
Returns a string created by converting each element of the set to a string See also: Array#join
Returns a string containing a human-readable representation of the set (“#<Set: {element1, element2, …}>”).