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 the size of the given list of nodes.
Fixed by Mike Stok
When invoked with a block, yield all repeated permutations of length n of the elements of the array, then return the array itself.
The implementation makes no guarantees about the order in which the repeated permutations are yielded.
If no block is given, an Enumerator is returned instead.
Examples:
a = [1, 2] a.repeated_permutation(1).to_a #=> [[1], [2]] a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]] a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2], # [2,1,1],[2,1,2],[2,2,1],[2,2,2]] a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
When invoked with a block, yields all repeated combinations of length n of elements from the array and then returns the array itself.
The implementation makes no guarantees about the order in which the repeated combinations are yielded.
If no block is given, an Enumerator is returned instead.
Examples:
a = [1, 2, 3] a.repeated_combination(1).to_a #=> [[1], [2], [3]] a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3], # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]] a.repeated_combination(4).to_a #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3], # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3], # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]] a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
Returns any backtrace associated with the exception. This method is similar to Exception#backtrace, but the backtrace is an array of Thread::Backtrace::Location.
Now, this method is not affected by Exception#set_backtrace().
Requests a connection to be made on the given remote_sockaddr after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if successful, otherwise an exception is raised.
# +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
# Pull down Google's web page require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(80, 'www.google.com') begin # emulate blocking connect socket.connect_nonblock(sockaddr) rescue IO::WaitWritable IO.select(nil, [socket]) # wait 3-way handshake completion begin socket.connect_nonblock(sockaddr) # check connection failure rescue Errno::EISCONN end end socket.write("GET / HTTP/1.0\r\n\r\n") results = socket.read
Refer to Socket#connect for the exceptions that may be thrown if the call to connect_nonblock fails.
Socket#connect_nonblock may raise any error corresponding to connect(2) failure, including Errno::EINPROGRESS.
If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable. So IO::WaitWritable can be used to rescue the exceptions for retrying connect_nonblock.
By specifying a keyword argument exception to false, you can indicate that connect_nonblock should not raise an IO::WaitWritable exception, but return the symbol :wait_writable instead.
# Socket#connect
Returns true if unconverted_fields() to parsed results. See CSV::new for details.
Set options. Takes the same argument as GetoptLong.new.
Raises a RuntimeError if option processing has already started.
‘get_option’ is an alias of ‘get’.
‘each_option’ is an alias of ‘each’.