Returns running OLE Automation object or WIN32OLE
object from moniker. 1st argument should be OLE program id or class id or moniker.
WIN32OLE.connect('Excel.Application') # => WIN32OLE object which represents running Excel.
Returns revision information for the erb.rb module.
Returns the values in self
as an array, to use in pattern matching:
Measure = Data.define(:amount, :unit) distance = Measure[10, 'km'] distance.deconstruct #=> [10, "km"] # usage case distance in n, 'km' # calls #deconstruct underneath puts "It is #{n} kilometers away" else puts "Don't know how to handle it" end # prints "It is 10 kilometers away"
Or, with checking the class, too:
case distance in Measure(n, 'km') puts "It is #{n} kilometers away" # ... end
Returns the array of captures, which are all matches except m[0]
:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.captures # => ["H", "X", "113", "8"]
Related: MatchData.to_a
.
Establishes proc on thr as the handler for tracing, or disables tracing if the parameter is nil
.
Adds proc as a handler for tracing.
Establishes proc as the handler for tracing, or disables tracing if the parameter is nil
.
Note: this method is obsolete, please use TracePoint
instead.
proc takes up to six parameters:
an event name string
a filename string
a line number
a method name symbol, or nil
a binding, or nil
the class, module, or nil
proc is invoked whenever an event occurs.
Events are:
"c-call"
call a C-language routine
"c-return"
return from a C-language routine
"call"
call a Ruby method
"class"
start a class or module definition
"end"
finish a class or module definition
"line"
execute code on a new line
"raise"
raise an exception
"return"
return from a Ruby method
Tracing is disabled within the context of proc.
class Test def test a = 1 b = 2 end end set_trace_func proc { |event, file, line, id, binding, class_or_module| printf "%8s %s:%-2d %16p %14p\n", event, file, line, id, class_or_module } t = Test.new t.test
Produces:
c-return prog.rb:8 :set_trace_func Kernel line prog.rb:11 nil nil c-call prog.rb:11 :new Class c-call prog.rb:11 :initialize BasicObject c-return prog.rb:11 :initialize BasicObject c-return prog.rb:11 :new Class line prog.rb:12 nil nil call prog.rb:2 :test Test line prog.rb:3 :test Test line prog.rb:4 :test Test return prog.rb:5 :test Test
Returns the class for the given object
.
class A def foo ObjectSpace::trace_object_allocations do obj = Object.new p "#{ObjectSpace::allocation_class_path(obj)}" end end end A.new.foo #=> "Class"
See ::trace_object_allocations
for more information and examples.
Iterates over strongly connected component in the subgraph reachable from node.
Return value is unspecified.
each_strongly_connected_component_from
doesn’t call tsort_each_node
.
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=>[]}) graph.each_strongly_connected_component_from(2) {|scc| p scc } #=> [4] # [2] graph = G.new({1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}) graph.each_strongly_connected_component_from(2) {|scc| p scc } #=> [4] # [2, 3]
Iterates over strongly connected components in a graph. The graph is represented by node and each_child.
node is the first node. each_child should have call
method which takes a node argument and yields for each child node.
Return value is unspecified.
TSort.each_strongly_connected_component_from is a class method and it doesn’t need a class to represent a graph which includes TSort
.
graph = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} each_child = lambda {|n, &b| graph[n].each(&b) } TSort.each_strongly_connected_component_from(1, each_child) {|scc| p scc } #=> [4] # [2, 3] # [1]
Create a new RescueModifierNode
node
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:
.