Reads the next “line” from the I/O stream; lines are separated by sep. A separator of nil
reads the entire contents, and a zero-length separator reads the input a paragraph at a time (two successive newlines in the input separate paragraphs). The stream must be opened for reading or an IOError
will be raised. The line read in will be returned and also assigned to $_
. Returns nil
if called at end of file. 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.
File.new("testfile").gets #=> "This is line one\n" $_ #=> "This is line one\n" File.new("testfile").gets(4)#=> "This"
If IO
contains multibyte characters byte then gets(1)
returns character entirely:
# Russian characters take 2 bytes File.write("testfile", "\u{442 435 441 442}") File.open("testfile") {|f|f.gets(1)} #=> "\u0442" File.open("testfile") {|f|f.gets(2)} #=> "\u0442" File.open("testfile") {|f|f.gets(3)} #=> "\u0442\u0435" File.open("testfile") {|f|f.gets(4)} #=> "\u0442\u0435"
Reads a one-character string from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getc #=> "h" f.getc #=> "e"
Gets the next 8-bit byte (0..255) from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getbyte #=> 84 f.getbyte #=> 104
Pushes back bytes (passed as a parameter) onto ios, such that a subsequent buffered read will return it. Only one byte may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several bytes that have been pushed back). Has no effect with unbuffered reads (such as IO#sysread
).
f = File.new("testfile") #=> #<File:testfile> b = f.getbyte #=> 0x38 f.ungetbyte(b) #=> nil f.getbyte #=> 0x38
Pushes back one character (passed as a parameter) onto ios, such that a subsequent buffered character read will return it. Only one character may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several characters that have been pushed back). Has no effect with unbuffered reads (such as IO#sysread
).
f = File.new("testfile") #=> #<File:testfile> c = f.getc #=> "8" f.ungetc(c) #=> nil f.getc #=> "8"
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
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
Closes the associated database file.
Returns true if the associated database file has been closed.
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 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 true if the set is a superset of the given set.
Returns true if the set and the given set 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
Equivalent to Set#select!
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
Callback invoked whenever a subclass of the current class is created.
Example:
class Foo def self.inherited(subclass) puts "New subclass: #{subclass}" end end class Bar < Foo end class Baz < Bar end
produces:
New subclass: Bar New subclass: Baz
Allocates space for a new object of class’s class and does not call initialize on the new instance. The returned object must be an instance of class.
klass = Class.new do def initialize(*args) @initialized = true end def initialized? @initialized || false end end klass.allocate.initialized? #=> false