Class RDoc::Parser::C
In: lib/rdoc/parser/c.rb
Parent: RDoc::Parser

We attempt to parse C extension files. Basically we look for the standard patterns that you find in extensions: rb_define_class, rb_define_method and so on. We also try to find the corresponding C source for the methods and extract comments, but if we fail we don’t worry too much.

The comments associated with a Ruby method are extracted from the C comment block associated with the routine that implements that method, that is to say the method whose name is given in the rb_define_method call. For example, you might write:

 /*
  * Returns a new array that is a one-dimensional flattening of this
  * array (recursively). That is, for every element that is an array,
  * extract its elements into the new array.
  *
  *    s = [ 1, 2, 3 ]           #=> [1, 2, 3]
  *    t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
  *    a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
  *    a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  */
  static VALUE
  rb_ary_flatten(ary)
      VALUE ary;
  {
      ary = rb_obj_dup(ary);
      rb_ary_flatten_bang(ary);
      return ary;
  }

  ...

  void
  Init_Array()
  {
    ...
    rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);

Here RDoc will determine from the rb_define_method line that there’s a method called “flatten” in class Array, and will look for the implementation in the method rb_ary_flatten. It will then use the comment from that method in the HTML output. This method must be in the same source file as the rb_define_method.

C classes can be diagrammed (see /tc/dl/ruby/ruby/error.c), and RDoc integrates C and Ruby source into one tree

The comment blocks may include special directives:

Document-class: name
This comment block is documentation for the given class. Use this when the Init_xxx method is not named after the class.
Document-method: name
This comment documents the named method. Use when RDoc cannot automatically find the method from it’s declaration
call-seq: text up to an empty line
Because C source doesn’t give descripive names to Ruby-level parameters, you need to document the calling sequence explicitly

In addition, RDoc assumes by default that the C method implementing a Ruby function is in the same source file as the rb_define_method call. If this isn’t the case, add the comment:

   rb_define_method(....);  // in: filename

As an example, we might have an extension that defines multiple classes in its Init_xxx method. We could document them using

  /*
   * Document-class:  MyClass
   *
   * Encapsulate the writing and reading of the configuration
   * file. ...
   */

  /*
   * Document-method: read_value
   *
   * call-seq:
   *   cfg.read_value(key)            -> value
   *   cfg.read_value(key} { |key| }  -> value
   *
   * Return the value corresponding to +key+ from the configuration.
   * In the second form, if the key isn't found, invoke the
   * block and return its value.
   */

Methods

Public Class methods

Public Instance methods

Look for includes of the form:

  rb_include_module(rb_cArray, rb_mEnumerable);

Look for class or module documentation above Init_+class_name+(void), in a Document-class class_name (or module) comment or above an rb_define_class (or module). If a comment is supplied above a matching Init_ and a rb_define_class the Init_ comment is used.

  /*
   * This is a comment for Foo
   */
  Init_Foo(void) {
      VALUE cFoo = rb_define_class("Foo", rb_cObject);
  }

  /*
   * Document-class: Foo
   * This is a comment for Foo
   */
  Init_foo(void) {
      VALUE cFoo = rb_define_class("Foo", rb_cObject);
  }

  /*
   * This is a comment for Foo
   */
  VALUE cFoo = rb_define_class("Foo", rb_cObject);

Finds a comment matching type and const_name either above the comment or in the matching Document- section.

If the comment block contains a section that looks like:

use it for the parameters.

Adds constant comments. By providing some_value: at the start ofthe comment you can override the C value of the comment to give a friendly definition.

  /* 300: The perfect score in bowling */
  rb_define_const(cFoo, "PERFECT", INT2FIX(300);

Will override +INT2FIX(300)+ with the value 300 in the output RDoc. Values may include quotes and escaped colons (:).

Removes ifdefs that would otherwise confuse us

Remove the /*’s and leading asterisks from C comments

Removes lines that are commented out that might otherwise get picked up when scanning for classes and methods

Extract the classes/modules and methods from a C file and return the corresponding top-level object

[Validate]

ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.