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.
Returns strongly connected components as an array of arrays of nodes. The array is sorted from children to parents. Each elements of the array represents a strongly connected component.
class G include TSort def initialize(g) @g = g end def tsort_each_child(n, &b) @g[n].each(&b) end def tsort_each_node(&b) @g.each_key(&b) end end graph = G.new({1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]}) p graph.strongly_connected_components #=> [[4], [2], [3], [1]] graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}) p graph.strongly_connected_components #=> [[4], [2, 3], [1]]
Returns strongly connected components as an array of arrays of nodes. The array is sorted from children to parents. Each elements of the array represents a strongly connected component.
The graph is represented by each_node and each_child. each_node should have call
method which yields for each node in the graph. each_child should have call
method which takes a node argument and yields for each child node.
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]} each_node = lambda {|&b| g.each_key(&b) } each_child = lambda {|n, &b| g[n].each(&b) } p TSort.strongly_connected_components(each_node, each_child) #=> [[4], [2], [3], [1]] g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} each_node = lambda {|&b| g.each_key(&b) } each_child = lambda {|n, &b| g[n].each(&b) } p TSort.strongly_connected_components(each_node, each_child) #=> [[4], [2, 3], [1]]
Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient r = self % other # Remainder
Examples:
11.divmod(4) # => [2, 3] 11.divmod(-4) # => [-3, -1] -11.divmod(4) # => [-3, 1] -11.divmod(-4) # => [2, -3] 12.divmod(4) # => [3, 0] 12.divmod(-4) # => [-3, 0] -12.divmod(4) # => [-3, 0] -12.divmod(-4) # => [3, 0] 13.divmod(4.0) # => [3, 1.0] 13.divmod(Rational(4, 1)) # => [3, (1/1)]
Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient r = self % other # Remainder
Of the Core and Standard Library classes, only Rational
uses this implementation.
Examples:
Rational(11, 1).divmod(4) # => [2, (3/1)] Rational(11, 1).divmod(-4) # => [-3, (-1/1)] Rational(-11, 1).divmod(4) # => [-3, (1/1)] Rational(-11, 1).divmod(-4) # => [2, (-3/1)] Rational(12, 1).divmod(4) # => [3, (0/1)] Rational(12, 1).divmod(-4) # => [-3, (0/1)] Rational(-12, 1).divmod(4) # => [-3, (0/1)] Rational(-12, 1).divmod(-4) # => [3, (0/1)] Rational(13, 1).divmod(4.0) # => [3, 1.0] Rational(13, 1).divmod(Rational(4, 11)) # => [35, (3/11)]
Returns a 2-element array [q, r]
, where
q = (self/other).floor # Quotient r = self % other # Remainder
Examples:
11.0.divmod(4) # => [2, 3.0] 11.0.divmod(-4) # => [-3, -1.0] -11.0.divmod(4) # => [-3, 1.0] -11.0.divmod(-4) # => [2, -3.0] 12.0.divmod(4) # => [3, 0.0] 12.0.divmod(-4) # => [-3, 0.0] -12.0.divmod(4) # => [-3, -0.0] -12.0.divmod(-4) # => [3, -0.0] 13.0.divmod(4.0) # => [3, 1.0] 13.0.divmod(Rational(4, 1)) # => [3, 1.0]
Changes permission bits on the named file(s) to the bit pattern represented by mode_int. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2)
for details. Returns the number of files processed.
File.chmod(0644, "testfile", "out") #=> 2
Equivalent to File::chmod
, but does not follow symbolic links (so it will change the permissions associated with the link, not the file referenced by the link). Often not available.
Changes permission bits on file to the bit pattern represented by mode_int. Actual effects are platform dependent; on Unix systems, see chmod(2)
for details. Follows symbolic links. Also see File#lchmod.
f = File.new("out", "w"); f.chmod(0644) #=> 0
Creates an infinite enumerator from any block, just called over and over. The result of the previous iteration is passed to the next one. If initial
is provided, it is passed to the first iteration, and becomes the first element of the enumerator; if it is not provided, the first iteration receives nil
, and its result becomes the first element of the iterator.
Raising StopIteration
from the block stops an iteration.
Enumerator.produce(1, &:succ) # => enumerator of 1, 2, 3, 4, .... Enumerator.produce { rand(10) } # => infinite random number sequence ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration } enclosing_section = ancestors.find { |n| n.type == :section }
Using ::produce
together with Enumerable
methods like Enumerable#detect
, Enumerable#slice_after
, Enumerable#take_while
can provide Enumerator-based alternatives for while
and until
cycles:
# Find next Tuesday require "date" Enumerator.produce(Date.today, &:succ).detect(&:tuesday?) # Simple lexer: require "strscan" scanner = StringScanner.new("7+38/6") PATTERN = %r{\d+|[-/+*]} Enumerator.produce { scanner.scan(PATTERN) }.slice_after { scanner.eos? }.first # => ["7", "+", "38", "/", "6"]
Returns an integer representing the mode settings for exception handling and rounding.
These modes control exception handling:
BigDecimal::EXCEPTION_NaN.
BigDecimal::EXCEPTION_INFINITY.
BigDecimal::EXCEPTION_UNDERFLOW.
BigDecimal::EXCEPTION_OVERFLOW.
BigDecimal::EXCEPTION_ZERODIVIDE.
BigDecimal::EXCEPTION_ALL.
Values for setting
for exception handling:
true
: sets the given mode
to true
.
false
: sets the given mode
to false
.
nil
: does not modify the mode settings.
You can use method BigDecimal.save_exception_mode
to temporarily change, and then automatically restore, exception modes.
For clarity, some examples below begin by setting all exception modes to false
.
This mode controls the way rounding is to be performed:
BigDecimal::ROUND_MODE
You can use method BigDecimal.save_rounding_mode
to temporarily change, and then automatically restore, the rounding mode.
NaNs
Mode BigDecimal::EXCEPTION_NaN controls behavior when a BigDecimal NaN is created.
Settings:
false
(default): Returns BigDecimal('NaN')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal('NaN') # => NaN BigDecimal.mode(BigDecimal::EXCEPTION_NaN, true) # => 2 BigDecimal('NaN') # Raises FloatDomainError
Infinities
Mode BigDecimal::EXCEPTION_INFINITY controls behavior when a BigDecimal Infinity or -Infinity is created. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal('Infinity') # => Infinity BigDecimal('-Infinity') # => -Infinity BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, true) # => 1 BigDecimal('Infinity') # Raises FloatDomainError BigDecimal('-Infinity') # Raises FloatDomainError
Underflow
Mode BigDecimal::EXCEPTION_UNDERFLOW controls behavior when a BigDecimal underflow occurs. Settings:
false
(default): Returns BigDecimal('0')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 def flow_under x = BigDecimal('0.1') 100.times { x *= x } end flow_under # => 100 BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, true) # => 4 flow_under # Raises FloatDomainError
Overflow
Mode BigDecimal::EXCEPTION_OVERFLOW controls behavior when a BigDecimal overflow occurs. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 def flow_over x = BigDecimal('10') 100.times { x *= x } end flow_over # => 100 BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true) # => 1 flow_over # Raises FloatDomainError
Zero Division
Mode BigDecimal::EXCEPTION_ZERODIVIDE controls behavior when a zero-division occurs. Settings:
false
(default): Returns BigDecimal('Infinity')
or BigDecimal('-Infinity')
.
true
: Raises FloatDomainError
.
Examples:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 one = BigDecimal('1') zero = BigDecimal('0') one / zero # => Infinity BigDecimal.mode(BigDecimal::EXCEPTION_ZERODIVIDE, true) # => 16 one / zero # Raises FloatDomainError
All Exceptions
Mode BigDecimal::EXCEPTION_ALL controls all of the above:
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false) # => 0 BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true) # => 23
Rounding
Mode BigDecimal::ROUND_MODE controls the way rounding is to be performed; its setting
values are:
ROUND_UP
: Round away from zero. Aliased as :up
.
ROUND_DOWN
: Round toward zero. Aliased as :down
and :truncate
.
ROUND_HALF_UP
: Round toward the nearest neighbor; if the neighbors are equidistant, round away from zero. Aliased as :half_up
and :default
.
ROUND_HALF_DOWN
: Round toward the nearest neighbor; if the neighbors are equidistant, round toward zero. Aliased as :half_down
.
ROUND_HALF_EVEN
(Banker’s rounding): Round toward the nearest neighbor; if the neighbors are equidistant, round toward the even neighbor. Aliased as :half_even
and :banker
.
ROUND_CEILING
: Round toward positive infinity. Aliased as :ceiling
and :ceil
.
ROUND_FLOOR
: Round toward negative infinity. Aliased as :floor:
.
Divides by the specified value, and returns the quotient and modulus as BigDecimal
numbers. The quotient is rounded towards negative infinity.
For example:
require 'bigdecimal' a = BigDecimal("42") b = BigDecimal("9") q, m = a.divmod(b) c = q * b + m a == c #=> true
The quotient q is (a/b).floor, and the modulus is the amount that must be added to q * b to get a.
Puts ios into binary mode. Once a stream is in binary mode, it cannot be reset to nonbinary mode.
newline conversion disabled
encoding conversion disabled
content is treated as ASCII-8BIT
Returns true
if ios is binmode.