With no block given, returns the sum of initial_value
and the elements:
(1..100).sum # => 5050 (1..100).sum(1) # => 5051 ('a'..'d').sum('foo') # => "fooabcd"
Generally, the sum is computed using methods +
and each
; for performance optimizations, those methods may not be used, and so any redefinition of those methods may not have effect here.
One such optimization: When possible, computes using Gauss’s summation formula n(n+1)/2:
100 * (100 + 1) / 2 # => 5050
With a block given, calls the block with each element; returns the sum of initial_value
and the block return values:
(1..4).sum {|i| i*i } # => 30 (1..4).sum(100) {|i| i*i } # => 130 h = {a: 0, b: 1, c: 2, d: 3, e: 4, f: 5} h.sum {|key, value| value.odd? ? value : 0 } # => 9 ('a'..'f').sum('x') {|c| c < 'd' ? c : '' } # => "xabc"
Returns true if coverage measurement is supported for the given mode.
The mode should be one of the following symbols: :lines
, :oneshot_lines
, :branches
, :methods
, :eval
.
Example:
Coverage.supported?(:lines) #=> true Coverage.supported?(:all) #=> false
Start/resume the coverage measurement.
Caveat: Currently, only process-global coverage measurement is supported. You cannot measure per-thread coverage. If your process has multiple thread, using Coverage.resume
/suspend to capture code coverage executed from only a limited code block, may yield misleading results.
Suspend the coverage measurement. You can use Coverage.resume
to restart the measurement.
Returns a hash that contains filename as key and coverage array as value. If clear
is true, it clears the counters to zero. If stop
is true, it disables coverage measurement.
Returns a BubbleBabble encoded version of a given string.
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
The path to the running Ruby interpreter.
Suffixes for require-able paths.
Returns the BubbleBabble encoded hash value of a given string.
Returns the flags of ifaddr.
Returns true if there were no errors during parsing and false if there were.
Whether this specification is stubbed - i.e. we have information about the gem from a stub line, without having to evaluate the entire gemspec file.
Returns a Gem::StubSpecification
for every installed gem
A short summary of this gem’s description.
Returns a Gem::StubSpecification
for every specification in the record
Returns:
true
if the process has completed successfully and exited.
false
if the process has completed unsuccessfully and exited.
nil
if the process has not exited.
Returns the resulting hash value in a Bubblebabble encoded form.