Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf
for details.
This is a deprecated alias for each_line
.
This is a deprecated alias for each_codepoint
.
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 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 a line as with IO#gets
, 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.
Return a string describing this IO
object.
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 a string containing a detailed summary of the keys and values.
Convert this range object to a printable form (using inspect
to convert the begin and end objects).
Returns true
if obj
is an element of the range, false
otherwise. If begin and end are numeric, comparison is done according to the magnitude of the values.
("a".."z").include?("g") #=> true ("a".."z").include?("A") #=> false ("a".."z").include?("cc") #=> false
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 a true
or false
indicates 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.
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
Returns sym.to_s.match(obj)
.
Returns sym.to_s.match?(obj)
.
Returns the Encoding
object that represents the encoding of sym.
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