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 time
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 time
is not compliant with the format or if the Time
class cannot represent the specified time.
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.
fraction_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.
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.
Returns a 2-element array containing the minimum and maximum value in self
, either according to comparison method <=>
or a given block.
With no block given, returns the minimum and maximum values, using <=>
for comparison:
(1..4).minmax # => [1, 4] (1...4).minmax # => [1, 3] ('a'..'d').minmax # => ["a", "d"] (-4..-1).minmax # => [-4, -1]
With a block given, the block must return an integer:
Negative if a
is smaller than b
.
Zero if a
and b
are equal.
Positive if a
is larger than b
.
The block is called self.size
times to compare elements; returns a 2-element Array
containing the minimum and maximum values from self
, per the block:
(1..4).minmax {|a, b| -(a <=> b) } # => [4, 1]
Returns [nil, nil]
if:
The begin value of the range is larger than the end value:
(4..1).minmax # => [nil, nil] (4..1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
The begin value of an exclusive range is equal to the end value:
(1...1).minmax # => [nil, nil] (1...1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
Raises an exception if self
is a beginless or an endless range.
Scans one character and returns it. This method is multibyte character sensitive.
s = StringScanner.new("ab") s.getch # => "a" s.getch # => "b" s.getch # => nil s = StringScanner.new("\244\242".force_encoding("euc-jp")) s.getch # => "\x{A4A2}" # Japanese hira-kana "A" in EUC-JP s.getch # => nil
Returns the value for the given key
, if found.
h = {foo: 0, bar: 1, baz: 2} h.fetch(:bar) # => 1
If key
is not found and no block was given, returns default_value
:
{}.fetch(:nosuch, :default) # => :default
If key
is not found and a block was given, yields key
to the block and returns the block’s return value:
{}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
Raises KeyError
if neither default_value
nor a block was given.
Note that this method does not use the values of either default
or default_proc
.
If name
is the name of an environment variable, returns its value:
ENV['foo'] = '0' ENV.fetch('foo') # => '0'
Otherwise if a block is given (but not a default value), yields name
to the block and returns the block’s return value:
ENV.fetch('foo') { |name| :need_not_return_a_string } # => :need_not_return_a_string
Otherwise if a default value is given (but not a block), returns the default value:
ENV.delete('foo') ENV.fetch('foo', :default_need_not_be_a_string) # => :default_need_not_be_a_string
If the environment variable does not exist and both default and block are given, issues a warning (“warning: block supersedes default value argument”), yields name
to the block, and returns the block’s return value:
ENV.fetch('foo', :default) { |name| :block_return } # => :block_return
Raises KeyError
if name
is valid, but not found, and neither default value nor block is given:
ENV.fetch('foo') # Raises KeyError (key not found: "foo")
Raises an exception if name
is invalid. See Invalid Names and Values.
This is a convenience method which is same as follows:
begin q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... q.flush output end
Like []
, except that it accepts a default value for the store. If the key
does not exist:
Raises an exception if default
is PStore::Error
.
Returns the value of default
otherwise:
example_store do |store| store.transaction do store.fetch(:nope, nil) # => nil store.fetch(:nope) # Raises an exception. end end
Raises an exception if called outside a transaction block.
Returns a fiber-local for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise a KeyError
exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned. See Thread#[]
and Hash#fetch
.
Returns the string resulting from formatting objects
into format_string
.
For details on format_string
, see Format Specifications.
Returns a 2-element array containing the minimum and maximum elements according to a given criterion. The ordering of equal elements is indeterminate and may be unstable.
With no argument and no block, returns the minimum and maximum elements, using the elements’ own method <=>
for comparison:
(1..4).minmax # => [1, 4] (-4..-1).minmax # => [-4, -1] %w[d c b a].minmax # => ["a", "d"] {foo: 0, bar: 1, baz: 2}.minmax # => [[:bar, 1], [:foo, 0]] [].minmax # => [nil, nil]
With a block given, returns the minimum and maximum elements as determined by the block:
%w[xxx x xxxx xx].minmax {|a, b| a.size <=> b.size } # => ["x", "xxxx"] h = {foo: 0, bar: 1, baz: 2} h.minmax {|pair1, pair2| pair1[1] <=> pair2[1] } # => [[:foo, 0], [:baz, 2]] [].minmax {|a, b| a <=> b } # => [nil, nil]
Invokes the block with a Benchmark::Report object, which may be used to collect and report on the results of individual benchmark tests. Reserves label_width
leading spaces for labels on each line. Prints caption
at the top of the report, and uses format
to format each line. (Note: caption
must contain a terminating newline character, see the default Benchmark::Tms::CAPTION for an example.)
Returns an array of Benchmark::Tms
objects.
If the block returns an array of Benchmark::Tms
objects, these will be used to format additional lines of output. If labels
parameter are given, these are used to label these extra lines.
Note: Other methods provide a simpler interface to this one, and are suitable for nearly all benchmarking requirements. See the examples in Benchmark
, and the bm
and bmbm
methods.
Example:
require 'benchmark' include Benchmark # we need the CAPTION and FORMAT constants n = 5000000 Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x| tf = x.report("for:") { for i in 1..n; a = "1"; end } tt = x.report("times:") { n.times do ; a = "1"; end } tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end } [tf+tt+tu, (tf+tt+tu)/3] end
Generates:
user system total real for: 0.970000 0.000000 0.970000 ( 0.970493) times: 0.990000 0.000000 0.990000 ( 0.989542) upto: 0.970000 0.000000 0.970000 ( 0.972854) >total: 2.930000 0.000000 2.930000 ( 2.932889) >avg: 0.976667 0.000000 0.976667 ( 0.977630)
Invokes the block with a Benchmark::Report object, which may be used to collect and report on the results of individual benchmark tests. Reserves label_width
leading spaces for labels on each line. Prints caption
at the top of the report, and uses format
to format each line. (Note: caption
must contain a terminating newline character, see the default Benchmark::Tms::CAPTION for an example.)
Returns an array of Benchmark::Tms
objects.
If the block returns an array of Benchmark::Tms
objects, these will be used to format additional lines of output. If labels
parameter are given, these are used to label these extra lines.
Note: Other methods provide a simpler interface to this one, and are suitable for nearly all benchmarking requirements. See the examples in Benchmark
, and the bm
and bmbm
methods.
Example:
require 'benchmark' include Benchmark # we need the CAPTION and FORMAT constants n = 5000000 Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x| tf = x.report("for:") { for i in 1..n; a = "1"; end } tt = x.report("times:") { n.times do ; a = "1"; end } tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end } [tf+tt+tu, (tf+tt+tu)/3] end
Generates:
user system total real for: 0.970000 0.000000 0.970000 ( 0.970493) times: 0.990000 0.000000 0.990000 ( 0.989542) upto: 0.970000 0.000000 0.970000 ( 0.972854) >total: 2.930000 0.000000 2.930000 ( 2.932889) >avg: 0.976667 0.000000 0.976667 ( 0.977630)
Returns the currently set formatter. By default, it is set to DidYouMean::Formatter
.
Updates the primary formatter used to format the suggestions.
Compile a MatchPredicateNode
node
Dispatch enter and leave events for MatchPredicateNode
nodes and continue walking the tree.
Inspect a MatchPredicateNode
node.
@return [Array] specs of default gems that are ‘==` to the given `spec`.
Return all files in this gem that match for glob
.