Maintenance of Ruby 2.0.0 ended on February 24, 2016. Read more

In Files

  • coverage/coverage.c


Class/Module Index [+]



Coverage provides coverage measurement feature for Ruby. This feature is experimental, so these APIs may be changed in future.


  1. require “”

  2. do ::start

  3. require or load Ruby source file

  4. ::result will return a hash that contains filename as key and coverage array as value. A coverage array gives, for each line, the number of line execution by the interpreter. A nil value means coverage is disabled for this line (lines like else and end).


s = 0
10.times do |x|
  s += x

if s == 45
  p :ok
  p :ng

require ""
require "foo.rb"
p Coverage.result  #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}

Public Class Methods

result => hash click to toggle source

Returns a hash that contains filename as key and coverage array as value and disables coverage measurement.

               static VALUE
rb_coverage_result(VALUE klass)
    VALUE coverages = rb_get_coverages();
    VALUE ncoverages = rb_hash_new();
    if (!RTEST(coverages)) {
        rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
    st_foreach(RHASH_TBL(coverages), coverage_result_i, ncoverages);
    return ncoverages;
start => nil click to toggle source

Enables coverage measurement.

               static VALUE
rb_coverage_start(VALUE klass)
    if (!RTEST(rb_get_coverages())) {
        if (rb_coverages == Qundef) {
            rb_coverages = rb_hash_new();
            RBASIC(rb_coverages)->klass = 0;
    return Qnil;

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit