Returns a hash of parsed elements.
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
Creates a new Date
object by parsing from a string according to some typical XML
Schema formats.
Date.xmlschema('2001-02-03') #=> #<Date: 2001-02-03 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
This method is equivalent to strftime(‘%F’).
Creates a new DateTime
object by parsing from a string according to some typical XML
Schema formats.
DateTime.xmlschema('2001-02-03T04:05:06+07:00') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
Raise an ArgumentError
when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.
This method is equivalent to strftime(‘%FT%T%:z’). The optional argument n
is the number of digits for fractional seconds.
DateTime.parse('2001-02-03T04:05:06.123456789+07:00').iso8601(9) #=> "2001-02-03T04:05:06.123456789+07:00"
Parses date
as a dateTime defined by the XML
Schema and converts it to a Time
object. The format is a restricted version of the format defined by ISO 8601.
ArgumentError
is raised if date
is not compliant with the format or if the Time
class cannot represent specified date.
See xmlschema
for more information on this format.
require 'time' Time.xmlschema("2011-10-05T22:26:12-04:00") #=> 2011-10-05 22:26:12-04:00
You must require ‘time’ to use this method.
Returns a string which represents the time as a dateTime defined by XML
Schema:
CCYY-MM-DDThh:mm:ssTZD CCYY-MM-DDThh:mm:ss.sssTZD
where TZD is Z or [+-]hh:mm.
If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
fractional_digits
specifies a number of digits to use for fractional seconds. Its default value is 0.
require 'time' t = Time.now t.iso8601 # => "2011-10-05T22:26:12-04:00"
You must require ‘time’ to use this method.
Return a value from the database by locating the key string provided. If the key is not found, returns ifnone
. If ifnone
is not given, raises IndexError
.
Calls the block once for each [key, value] pair in the database. Returns self.
Yields the value of each struct member in order. If no block is given an enumerator is returned.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each {|x| puts(x) }
Produces:
Joe Smith 123 Maple, Anytown NC 12345
Reads and returns a character in raw mode.
See IO#raw
for details on the parameters.
You must require ‘io/console’ to use this method.
Enables/disables echo back. On some platforms, all combinations of this flags and raw/cooked mode may not be valid.
You must require ‘io/console’ to use this method.
Yields self
with disabling echo back.
STDIN.noecho(&:gets)
will read and return a line without echo back.
You must require ‘io/console’ to use this method.
Executes the block for every line in the named I/O port, where lines are separated by sep.
If no block is given, an enumerator is returned instead.
IO.foreach("testfile") {|x| print "GOT ", x }
produces:
GOT This is line one GOT This is line two GOT This is line three GOT And so on...
If the last argument is a hash, it’s the keyword argument to open. See IO.readlines
for details about getline_args. And see also IO.read
for details about open_args.
Executes the block for every line in ios, where lines are separated by sep. ios must be opened for reading or an IOError
will be raised.
If no block is given, an enumerator is returned instead.
f = File.new("testfile") f.each {|line| puts "#{f.lineno}: #{line}" }
produces:
1: This is line one 2: This is line two 3: This is line three 4: And so on...
See IO.readlines
for details about getline_args.
Reads a one-character string from ios. Raises an EOFError
on end of file.
f = File.new("testfile") f.readchar #=> "h" f.readchar #=> "e"
Retrieves the value corresponding to key. If there is no value associated with key, default will be returned instead.
Executes block for each key in the database, passing the key and the corresponding value as a parameter.
Sets the size of the internal bucket cache to size.
Iterates over the elements of range, passing each in turn to the block.
The each
method can only be used if the begin object of the range supports the succ
method. A TypeError
is raised if the object does not have succ
method defined (like Float
).
If no block is given, an enumerator is returned instead.
(10..15).each {|n| print n, ' ' } # prints: 10 11 12 13 14 15 (2.5..5).each {|n| print n, ' ' } # raises: TypeError: can't iterate from Float
By using binary search, finds a value in range which meets the given condition in O(log n) where n is the size of the range.
You can use this method in two use cases: a find-minimum mode and a find-any mode. In either case, the elements of the range must be monotone (or sorted) with respect to the block.
In find-minimum mode (this is a good choice for typical use case), the block must return true or false, and there must be a value x so that:
the block returns false for any value which is less than x, and
the block returns true for any value which is greater than or equal to x.
If x is within the range, this method returns the value x. Otherwise, it returns nil.
ary = [0, 4, 7, 10, 12] (0...ary.size).bsearch {|i| ary[i] >= 4 } #=> 1 (0...ary.size).bsearch {|i| ary[i] >= 6 } #=> 2 (0...ary.size).bsearch {|i| ary[i] >= 8 } #=> 3 (0...ary.size).bsearch {|i| ary[i] >= 100 } #=> nil (0.0...Float::INFINITY).bsearch {|x| Math.log(x) >= 0 } #=> 1.0
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number, and there must be two values x and y (x <= y) so that:
the block returns a positive number for v if v < x,
the block returns zero for v if x <= v < y, and
the block returns a negative number for v if y <= v.
This method returns any value which is within the intersection of the given range and x…y (if any). If there is no value that satisfies the condition, it returns nil.
ary = [0, 100, 100, 100, 200] (0..4).bsearch {|i| 100 - ary[i] } #=> 1, 2 or 3 (0..4).bsearch {|i| 300 - ary[i] } #=> nil (0..4).bsearch {|i| 50 - ary[i] } #=> nil
You must not mix the two modes at a time; the block must always return either true/false, or always return a number. It is undefined which value is actually picked up at each iteration.
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