17.1. Using RDocRDoc isn't the only doc tool around for Ruby; RDTOOL is older. But in many ways, RDoc is superior; it's also more commonly used, at least in the United States. One great thing about RDoc is that it tries to produce useful output even if there are no comments in the source. It does this by parsing the code and organizing information on all the classes, module, constants, methods, and so on. Therefore you can get reasonably useful HTML out of a program source that doesn't even have any real internal documentation. If you haven't tried this before, I suggest you try it. But it gets better. RDoc also tries to associate the comments it finds with specific parts of the program. The general rule is: A block comment preceding a definition (such as a class or method) will be taken as the description of that definition. If you simply invoke RDoc on a Ruby source, it will create a doc directory with all the files under it. (The default template looks good, but there are others.) Browse to index.html and take a look. Listing 17.1 shows a simple (nearly empty) source file. All the methods are empty, and none of it really does anything. But RDoc will still take it and produce a pretty doc page (see Figure 17.1). Listing 17.1. A Simple Source File
Figure 17.1. RDoc output from source in Listing 17.1.We'll discuss two other useful features in this section. Every method name is linked to a pop-up that displays the source code of that method. This is an absolutely invaluable tool in learning a library; the API documentation is linked directly back to the code itself. Also be aware that when RDoc recognizes a URL, it places a hyperlink in the output. The text of the link defaults to be the same as the URL, but you can change this. If you put descriptive text in braces followed by a URL in brackets ({descriptive text}[myurl]), your descriptive text will be used in the link. If the text is a single word, the braces may be omitted. 17.1.1. Simple MarkupIf you want to "decorate" your output more, you don't have to edit the HTML files. In fact, that's a bad idea since it's so easy to regenerate the output (and overwrite your changes). RDoc has a simple markup facility of its own so that you can embed your own formatting information into your source code. The markup rules are chosen so that the text looks "natural" in an editor, but it can be translated into HTML in a straightforward way. Listing 17.2 shows a few examples of markup capability; for more examples, consult Programming Ruby or any other source of RDoc documentation. The output (bottom frame only) from Listing 17.2 is shown in Figure 17.2. Listing 17.2. Examples of RDoc Markup
Figure 17.2. RDoc output from markup examples in Listing 17.2.Listing 17.2 outlines some of the rules RDoc uses to parse comments. Not all of these are necessarily intuitive. There is a strong tendency for blank lines to terminate a section of comments, even if the blank line is immediately followed by another block comment. Note that inside a block comment starting with #, we can "turn off" the copying of text into the output with a #-- line (and turn it back on again the same way). Not all comments are intended to go into the user docs, after all. Note also that if =begin and =end are used, they must have an rdoc tag after the =begin, or RDoc will ignore the block. This is to avoid conflict with older tools that make heavy use of such blocks. 17.1.2. More Advanced FormattingRDoc gives you relatively fine control over what parts of your source are documented and how they are treated. Special tags in the comments (documentation modifiers) accomplish this. One of the most important of these is :nodoc: (to turn off documentation for something you don't want to appear in output). Typically this is used on the same line when a class or method is introduced. class Alpha # :nodoc: class Beta # ... end # ... end In the preceding example, Alpha would not be documented. However, :nodoc: is not recursive; Beta would still be documented. To make it recursive, use :nodoc: all instead. In the following example, both Gamma and Delta will be ignored: class Alpha # :nodoc: all class Beta # ... end # ... end There's also a :doc: modifier, which is the opposite. It forces documentation for things that ordinarily would not appear in output. The :notnew: modifier is special; it prevents the documenting of a new method (based on an existing initialize method). If you want to give meaningful names to your yield parameters, you can use the yields keyword. For example, your code may use boring parameter names such as x and i; you can change these in the documentation. def iterate # :yields: element, index # ... yield x, i end Some tags are used only inside block comments. Here are most of them:
For more information, consult Programming Ruby or any reference you can find online. |