When invoked with a block, yield all permutations of elements of self
; returns self
. The order of permutations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple permutations of self
.
Example:
a = [0, 1, 2] a.permutation(2) {|permutation| p permutation }
Output:
[0, 1] [0, 2] [1, 0] [1, 2] [2, 0] [2, 1]
Another example:
a = [0, 1, 2] a.permutation(3) {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
When n
is zero, calls the block once with a new empty Array
:
a = [0, 1, 2] a.permutation(0) {|permutation| p permutation }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.permutation(-1) {|permutation| fail 'Cannot happen' } a.permutation(4) {|permutation| fail 'Cannot happen' }
When a block given but no argument, behaves the same as a.permutation(a.size)
:
a = [0, 1, 2] a.permutation {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.permutation # => #<Enumerator: [0, 1, 2]:permutation> a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
Calls the block, if given, with combinations of elements of self
; returns self
. The order of combinations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple combinations of self
.
Example:
a = [0, 1, 2] a.combination(2) {|combination| p combination }
Output:
[0, 1] [0, 2] [1, 2]
Another example:
a = [0, 1, 2] a.combination(3) {|combination| p combination }
Output:
[0, 1, 2]
When n
is zero, calls the block once with a new empty Array
:
a = [0, 1, 2] a1 = a.combination(0) {|combination| p combination }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.combination(-1) {|combination| fail 'Cannot happen' } a.combination(4) {|combination| fail 'Cannot happen' }
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
Returns the value as a rational. The optional argument eps
is always ignored.
Returns a Rational
object whose value is exactly or approximately equivalent to that of self.real
.
With no argument epsilon
given, returns a Rational object whose value is exactly equal to that of self.real.rationalize
:
Complex.rect(1, 0).rationalize # => (1/1) Complex.rect(1, Rational(0, 1)).rationalize # => (1/1) Complex.rect(3.14159, 0).rationalize # => (314159/100000)
With argument epsilon
given, returns a Rational object whose value is exactly or approximately equal to that of self.real
to the given precision:
Complex.rect(3.14159, 0).rationalize(0.1) # => (16/5) Complex.rect(3.14159, 0).rationalize(0.01) # => (22/7) Complex.rect(3.14159, 0).rationalize(0.001) # => (201/64) Complex.rect(3.14159, 0).rationalize(0.0001) # => (333/106) Complex.rect(3.14159, 0).rationalize(0.00001) # => (355/113) Complex.rect(3.14159, 0).rationalize(0.000001) # => (7433/2366) Complex.rect(3.14159, 0).rationalize(0.0000001) # => (9208/2931) Complex.rect(3.14159, 0).rationalize(0.00000001) # => (47460/15107) Complex.rect(3.14159, 0).rationalize(0.000000001) # => (76149/24239) Complex.rect(3.14159, 0).rationalize(0.0000000001) # => (314159/100000) Complex.rect(3.14159, 0).rationalize(0.0) # => (3537115888337719/1125899906842624)
Related: Complex#to_r
.
Returns zero as a Rational:
nil.rationalize # => (0/1)
Argument eps
is ignored.
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r
.
With no argument, or if the argument is the same as the receiver, return the receiver. Otherwise, create a new exception object of the same class as the receiver, but with a message equal to string.to_str
.
With no argument, or if the argument is the same as the receiver, return the receiver. Otherwise, create a new exception object of the same class as the receiver, but with a message equal to string.to_str
.
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Returns a new regexp that is the union of the given patterns:
r = Regexp.union(%w[cat dog]) # => /cat|dog/ r.match('cat') # => #<MatchData "cat"> r.match('dog') # => #<MatchData "dog"> r.match('cog') # => nil
For each pattern that is a string, Regexp.new(pattern)
is used:
Regexp.union('penzance') # => /penzance/ Regexp.union('a+b*c') # => /a\+b\*c/ Regexp.union('skiing', 'sledding') # => /skiing|sledding/ Regexp.union(['skiing', 'sledding']) # => /skiing|sledding/
For each pattern that is a regexp, it is used as is, including its flags:
Regexp.union(/foo/i, /bar/m, /baz/x) # => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/ Regexp.union([/foo/i, /bar/m, /baz/x]) # => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/
With no arguments, returns /(?!)/
:
Regexp.union # => /(?!)/
If any regexp pattern contains captures, the behavior is unspecified.
Returns an integer whose bits show the options set in self
.
The option bits are:
Regexp::IGNORECASE # => 1 Regexp::EXTENDED # => 2 Regexp::MULTILINE # => 4
Examples:
/foo/.options # => 0 /foo/i.options # => 1 /foo/x.options # => 2 /foo/m.options # => 4 /foo/mix.options # => 7
Note that additional bits may be set in the returned integer; these are maintained internally in self
, are ignored if passed to Regexp.new
, and may be ignored by the caller:
Returns the set of bits corresponding to the options used when creating this regexp (see Regexp::new
for details). Note that additional bits may be set in the returned options: these are used internally by the regular expression code. These extra bits are ignored if the options are passed to Regexp::new
:
r = /\xa1\xa2/e # => /\xa1\xa2/ r.source # => "\\xa1\\xa2" r.options # => 16 Regexp.new(r.source, r.options) # => /\xa1\xa2/
Sets optional filename and line number that will be used in ERB
code evaluation and error reporting. See also filename=
and lineno=
erb = ERB.new('<%= some_x %>') erb.render # undefined local variable or method `some_x' # from (erb):1 erb.location = ['file.erb', 3] # All subsequent error reporting would use new location erb.render # undefined local variable or method `some_x' # from file.erb:4
Returns x/y
or arg
as a Rational
.
Rational(2, 3) #=> (2/3) Rational(5) #=> (5/1) Rational(0.5) #=> (1/2) Rational(0.3) #=> (5404319552844595/18014398509481984) Rational("2/3") #=> (2/3) Rational("0.3") #=> (3/10) Rational("10 cents") #=> ArgumentError Rational(nil) #=> TypeError Rational(1, nil) #=> TypeError Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ; rational = [ sign ] , unsigned rational ; unsigned rational = numerator | numerator , "/" , denominator ; numerator = integer part | fractional part | integer part , fractional part ; denominator = digits ; integer part = digits ; fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ; sign = "-" | "+" ; digits = digit , { digit | "_" , digit } ; digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; extra spaces = ? \s* ? ;
See also String#to_r
.
Returns an array of the string keyword names:
FileUtils.options.take(3) # => ["noop", "verbose", "force"]
Raises PStore::Error
if the calling code is not in a PStore#transaction
or if the code is in a read-only PStore#transaction
.
Returns the status of the global “abort on exception” condition.
The default is false
.
When set to true
, if any thread is aborted by an exception, the raised exception will be re-raised in the main thread.
Can also be specified by the global $DEBUG flag or command line option -d
.
See also ::abort_on_exception=
.
There is also an instance level method to set this for a specific thread, see abort_on_exception
.
When set to true
, if any thread is aborted by an exception, the raised exception will be re-raised in the main thread. Returns the new state.
Thread.abort_on_exception = true t1 = Thread.new do puts "In new thread" raise "Exception from thread" end sleep(1) puts "not reached"
This will produce:
In new thread prog.rb:4: Exception from thread (RuntimeError) from prog.rb:2:in `initialize' from prog.rb:2:in `new' from prog.rb:2
See also ::abort_on_exception
.
There is also an instance level method to set this for a specific thread, see abort_on_exception=
.
Returns the status of the global “report on exception” condition.
The default is true
since Ruby 2.5.
All threads created when this flag is true will report a message on $stderr if an exception kills the thread.
Thread.new { 1.times { raise } }
will produce this output on $stderr:
#<Thread:...> terminated with exception (report_on_exception is true): Traceback (most recent call last): 2: from -e:1:in `block in <main>' 1: from -e:1:in `times'
This is done to catch errors in threads early. In some cases, you might not want this output. There are multiple ways to avoid the extra output:
If the exception is not intended, the best is to fix the cause of the exception so it does not happen anymore.
If the exception is intended, it might be better to rescue it closer to where it is raised rather then let it kill the Thread
.
If it is guaranteed the Thread
will be joined with Thread#join
or Thread#value
, then it is safe to disable this report with Thread.current.report_on_exception = false
when starting the Thread
. However, this might handle the exception much later, or not at all if the Thread
is never joined due to the parent thread being blocked, etc.
See also ::report_on_exception=
.
There is also an instance level method to set this for a specific thread, see report_on_exception=
.
Returns the new state. When set to true
, all threads created afterwards will inherit the condition and report a message on $stderr if an exception kills a thread:
Thread.report_on_exception = true t1 = Thread.new do puts "In new thread" raise "Exception from thread" end sleep(1) puts "In the main thread"
This will produce:
In new thread #<Thread:...prog.rb:2> terminated with exception (report_on_exception is true): Traceback (most recent call last): prog.rb:4:in `block in <main>': Exception from thread (RuntimeError) In the main thread
See also ::report_on_exception
.
There is also an instance level method to set this for a specific thread, see report_on_exception=
.
Returns the status of the thread-local “abort on exception” condition for this thr
.
The default is false
.
See also abort_on_exception=
.
There is also a class level method to set this for all threads, see ::abort_on_exception
.
When set to true
, if this thr
is aborted by an exception, the raised exception will be re-raised in the main thread.
See also abort_on_exception
.
There is also a class level method to set this for all threads, see ::abort_on_exception=
.
Returns the status of the thread-local “report on exception” condition for this thr
.
The default value when creating a Thread
is the value of the global flag Thread.report_on_exception
.
See also report_on_exception=
.
There is also a class level method to set this for all new threads, see ::report_on_exception=
.
When set to true
, a message is printed on $stderr if an exception kills this thr
. See ::report_on_exception
for details.
See also report_on_exception
.
There is also a class level method to set this for all new threads, see ::report_on_exception=
.
Starts tracing object allocations from the ObjectSpace
extension module.
For example:
require 'objspace' class C include ObjectSpace def foo trace_object_allocations do obj = Object.new p "#{allocation_sourcefile(obj)}:#{allocation_sourceline(obj)}" end end end C.new.foo #=> "objtrace.rb:8"
This example has included the ObjectSpace
module to make it easier to read, but you can also use the ::trace_object_allocations
notation (recommended).
Note that this feature introduces a huge performance decrease and huge memory consumption.