2022-05-09 11:45:50 -04:00
|
|
|
# Documentation Guide
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
This guide discusses recommendations for documenting
|
|
|
|
classes, modules, and methods
|
|
|
|
in the Ruby core and in the Ruby standard library.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
## Generating documentation
|
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
Most Ruby documentation lives in the source files and is written in
|
2025-01-16 14:03:45 +09:00
|
|
|
[RDoc format](rdoc-ref:RDoc::MarkupReference).
|
2022-05-09 11:45:50 -04:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
Some pages live under the `doc` folder and can be written in either
|
|
|
|
`.rdoc` or `.md` format, determined by the file extension.
|
2022-05-09 11:45:50 -04:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
To generate the output of documentation changes in HTML in the
|
|
|
|
`{build folder}/.ext/html` directory, run the following inside your
|
|
|
|
build directory:
|
2022-05-09 11:45:50 -04:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
```sh
|
2022-05-09 11:45:50 -04:00
|
|
|
make html
|
|
|
|
```
|
|
|
|
|
2023-10-19 20:45:07 -03:00
|
|
|
If you don't have a build directory, follow the [quick start
|
|
|
|
guide](building_ruby.md#label-Quick+start+guide) up to step 4.
|
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
Then you can preview your changes by opening
|
|
|
|
`{build folder}/.ext/html/index.html` file in your browser.
|
2022-05-09 11:45:50 -04:00
|
|
|
|
|
|
|
## Goal
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The goal of Ruby documentation is to impart the most important
|
2023-03-01 17:08:02 -05:00
|
|
|
and relevant information in the shortest time.
|
2021-07-18 17:43:34 -05:00
|
|
|
The reader should be able to quickly understand the usefulness
|
|
|
|
of the subject code and how to use it.
|
|
|
|
|
|
|
|
Providing too little information is bad, but providing unimportant
|
|
|
|
information or unnecessary examples is not good either.
|
|
|
|
Use your judgment about what the user needs to know.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
## General Guidelines
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
- Keep in mind that the reader may not be fluent in \English.
|
|
|
|
- Write short declarative or imperative sentences.
|
|
|
|
- Group sentences into (ideally short) paragraphs,
|
|
|
|
each covering a single topic.
|
2023-06-28 08:05:43 -05:00
|
|
|
- Organize material with
|
|
|
|
[headings](rdoc-ref:RDoc::MarkupReference@Headings).
|
2021-07-18 17:43:34 -05:00
|
|
|
- Refer to authoritative and relevant sources using
|
2023-06-28 08:05:43 -05:00
|
|
|
[links](rdoc-ref:RDoc::MarkupReference@Links).
|
2021-07-18 17:43:34 -05:00
|
|
|
- Use simple verb tenses: simple present, simple past, simple future.
|
|
|
|
- Use simple sentence structure, not compound or complex structure.
|
|
|
|
- Avoid:
|
2022-05-09 11:45:50 -04:00
|
|
|
- Excessive comma-separated phrases;
|
2023-06-28 08:05:43 -05:00
|
|
|
consider a [list](rdoc-ref:RDoc::MarkupReference@Lists).
|
2022-05-09 11:45:50 -04:00
|
|
|
- Idioms and culture-specific references.
|
2023-06-28 08:05:43 -05:00
|
|
|
- Overuse of headings.
|
2022-05-09 11:45:50 -04:00
|
|
|
- Using US-ASCII-incompatible characters in C source files;
|
|
|
|
see [Characters](#label-Characters) below.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Characters
|
2022-03-11 10:46:47 -06:00
|
|
|
|
|
|
|
Use only US-ASCII-compatible characters in a C source file.
|
|
|
|
(If you use other characters, the Ruby CI will gently let you know.)
|
|
|
|
|
|
|
|
If want to put ASCII-incompatible characters into the documentation
|
|
|
|
for a C-coded class, module, or method, there are workarounds
|
2022-05-19 17:07:56 +09:00
|
|
|
involving new files `doc/*.rdoc`:
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
- For class `Foo` (defined in file `foo.c`),
|
|
|
|
create file `doc/foo.rdoc`, declare `class Foo; end`,
|
2022-03-11 10:46:47 -06:00
|
|
|
and place the class documentation above that declaration:
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
```ruby
|
2022-03-11 10:46:47 -06:00
|
|
|
# Documentation for class Foo goes here.
|
|
|
|
class Foo; end
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
- Similarly, for module `Bar` (defined in file `bar.c`,
|
|
|
|
create file `doc/bar.rdoc`, declare `module Bar; end`,
|
2022-03-11 10:46:47 -06:00
|
|
|
and place the module documentation above that declaration:
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
```ruby
|
2022-03-11 10:46:47 -06:00
|
|
|
# Documentation for module Bar goes here.
|
|
|
|
module Bar; end
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2022-03-21 14:58:00 -05:00
|
|
|
- For a method, things are different.
|
|
|
|
Documenting a method as above disables the "click to toggle source" feature
|
2022-04-07 16:47:07 +09:00
|
|
|
in the rendered documentation.
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
Therefore it's best to use file inclusion:
|
2022-05-19 17:07:56 +09:00
|
|
|
|
|
|
|
- Retain the `call-seq` in the C code.
|
2022-05-09 11:45:50 -04:00
|
|
|
- Use file inclusion (`:include:`) to include text from an .rdoc file.
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
Example:
|
2022-03-11 10:46:47 -06:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```c
|
2022-03-21 14:58:00 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* each_byte {|byte| ... } -> self
|
|
|
|
* each_byte -> enumerator
|
|
|
|
*
|
2022-05-19 17:07:56 +09:00
|
|
|
* :include: doc/string/each_byte.rdoc
|
2022-03-21 14:58:00 -05:00
|
|
|
*
|
|
|
|
*/
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### \RDoc
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
Ruby is documented using RDoc.
|
|
|
|
For information on \RDoc syntax and features, see the
|
2025-01-16 14:03:45 +09:00
|
|
|
[RDoc Markup Reference](rdoc-ref:RDoc::MarkupReference).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
### Output from `irb`
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
For code examples, consider using interactive Ruby,
|
2022-05-09 11:45:50 -04:00
|
|
|
[irb](https://ruby-doc.org/stdlib/libdoc/irb/rdoc/IRB.html).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
For a code example that includes `irb` output,
|
2022-05-19 17:07:56 +09:00
|
|
|
consider aligning `# => ...` in successive lines.
|
2021-07-18 17:43:34 -05:00
|
|
|
Alignment may sometimes aid readability:
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
```ruby
|
|
|
|
a = [1, 2, 3] #=> [1, 2, 3]
|
|
|
|
a.shuffle! #=> [2, 3, 1]
|
|
|
|
a #=> [2, 3, 1]
|
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2023-06-28 08:05:43 -05:00
|
|
|
### Headings
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2023-08-23 09:09:24 -05:00
|
|
|
Organize a long discussion for a class or module with [headings](rdoc-ref:RDoc::MarkupReference@Headings).
|
|
|
|
|
|
|
|
Do not use formal headings in the documentation for a method or constant.
|
|
|
|
|
|
|
|
In the rare case where heading-like structures are needed
|
|
|
|
within the documentation for a method or constant, use
|
|
|
|
[bold text](rdoc-ref:RDoc::MarkupReference@Bold)
|
|
|
|
as pseudo-headings.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Blank Lines
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
A blank line begins a new paragraph.
|
|
|
|
|
2023-06-28 08:05:43 -05:00
|
|
|
A [code block](rdoc-ref:RDoc::MarkupReference@Code+Blocks)
|
|
|
|
or [list](rdoc-ref:RDoc::MarkupReference@Lists)
|
2021-07-18 17:43:34 -05:00
|
|
|
should be preceded by and followed by a blank line.
|
2022-05-09 11:45:50 -04:00
|
|
|
This is unnecessary for the HTML output, but helps in the `ri` output.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-10-24 08:56:13 -05:00
|
|
|
### \Method Names
|
|
|
|
|
|
|
|
For a method name in text:
|
|
|
|
|
|
|
|
- For a method in the current class or module,
|
|
|
|
use a double-colon for a singleton method,
|
|
|
|
or a hash mark for an instance method:
|
|
|
|
<tt>::bar</tt>, <tt>#baz</tt>.
|
|
|
|
- Otherwise, include the class or module name
|
|
|
|
and use a dot for a singleton method,
|
|
|
|
or a hash mark for an instance method:
|
|
|
|
<tt>Foo.bar</tt>, <tt>Foo#baz</tt>.
|
|
|
|
|
2023-08-23 09:09:24 -05:00
|
|
|
### Embedded Code and Commands
|
|
|
|
|
|
|
|
Code or commands embedded in running text (i.e., not in a code block)
|
|
|
|
should marked up as
|
|
|
|
[monofont](rdoc-ref:RDoc::MarkupReference@Monofont).
|
|
|
|
|
|
|
|
Code that is a simple string should include the quote marks.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Auto-Linking
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2023-08-23 09:09:24 -05:00
|
|
|
Most often, the name of a class, module, or method
|
2024-07-31 15:09:15 -05:00
|
|
|
is auto-linked:
|
2023-08-23 09:09:24 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-07-31 15:09:15 -05:00
|
|
|
- Float.
|
2023-08-23 09:09:24 -05:00
|
|
|
- Enumerable.
|
|
|
|
- File.new
|
|
|
|
- File#read.
|
2024-07-31 15:09:15 -05:00
|
|
|
```
|
2024-12-12 20:49:45 -05:00
|
|
|
|
2024-07-31 15:09:15 -05:00
|
|
|
renders as:
|
2023-08-23 09:09:24 -05:00
|
|
|
|
2024-07-31 15:09:15 -05:00
|
|
|
> - Float.
|
|
|
|
> - Enumerable.
|
|
|
|
> - File.new
|
|
|
|
> - File#read.
|
|
|
|
|
|
|
|
In general, \RDoc's auto-linking should not be suppressed.
|
|
|
|
For example, we should write just plain _Float_ (which is auto-linked):
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-07-31 15:09:15 -05:00
|
|
|
Returns a Float.
|
|
|
|
```
|
|
|
|
|
|
|
|
which renders as:
|
|
|
|
|
|
|
|
> Returns a Float.
|
|
|
|
|
|
|
|
However, _do_ suppress auto-linking when the word in question
|
|
|
|
does not refer to a Ruby entity (e.g., some uses of _Class_ or _English_):
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2025-01-02 12:36:06 +09:00
|
|
|
Class variables can be tricky.
|
2024-07-31 15:09:15 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
renders as:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
> Class variables can be tricky.
|
2024-07-31 15:09:15 -05:00
|
|
|
|
|
|
|
Also, _do_ suppress auto-linking when the word in question
|
|
|
|
refers to the current document
|
|
|
|
(e.g., _Float_ in the documentation for class Float).
|
|
|
|
|
|
|
|
In this case you may consider forcing the name to
|
|
|
|
[monofont](rdoc-ref:RDoc::MarkupReference@Monofont),
|
|
|
|
which suppresses auto-linking, and also emphasizes that the word is a class name:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-07-31 15:09:15 -05:00
|
|
|
A +Float+ object represents ....
|
|
|
|
```
|
|
|
|
|
|
|
|
renders as:
|
|
|
|
|
|
|
|
> A `Float` object represents ....
|
|
|
|
|
|
|
|
For a _very_ few, _very_ often-discussed classes,
|
|
|
|
you might consider avoiding the capitalized class name altogether.
|
|
|
|
For example, for some mentions of arrays,
|
|
|
|
you might write simply the lowercase _array_.
|
|
|
|
|
|
|
|
Instead of:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-07-31 15:09:15 -05:00
|
|
|
For an empty Array, ....
|
|
|
|
```
|
|
|
|
|
|
|
|
which renders as:
|
|
|
|
|
|
|
|
> For an empty Array, ....
|
|
|
|
|
|
|
|
you might write:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-07-31 15:09:15 -05:00
|
|
|
For an empty array, ....
|
|
|
|
```
|
|
|
|
|
|
|
|
which renders as:
|
|
|
|
|
|
|
|
> For an empty array, ....
|
|
|
|
|
|
|
|
This more casual usage avoids both auto-linking and distracting font changes,
|
|
|
|
and is unlikely to cause confusion.
|
|
|
|
|
|
|
|
This principle may be usefully applied, in particular, for:
|
|
|
|
|
|
|
|
- An array.
|
|
|
|
- An integer.
|
|
|
|
- A hash.
|
|
|
|
- A string.
|
|
|
|
|
|
|
|
However, it should be applied _only_ when referring to an _instance_ of the class,
|
|
|
|
and _never_ when referring to the class itself.
|
2023-08-23 09:09:24 -05:00
|
|
|
|
2023-11-23 22:39:24 -06:00
|
|
|
### Explicit Links
|
|
|
|
|
|
|
|
When writing an explicit link, follow these guidelines.
|
|
|
|
|
|
|
|
#### +rdoc-ref+ Scheme
|
|
|
|
|
|
|
|
Use the +rdoc-ref+ scheme for:
|
|
|
|
|
|
|
|
- A link in core documentation to other core documentation.
|
|
|
|
- A link in core documentation to documentation in a standard library package.
|
|
|
|
- A link in a standard library package to other documentation in that same
|
|
|
|
standard library package.
|
|
|
|
|
|
|
|
See section "+rdoc-ref+ Scheme" in {Links}[rdoc-ref:RDoc::MarkupReference@Links].
|
|
|
|
|
|
|
|
#### URL-Based Link
|
|
|
|
|
|
|
|
Use a full URL-based link for:
|
|
|
|
|
|
|
|
- A link in standard library documentation to documentation in the core.
|
|
|
|
- A link in standard library documentation to documentation in a different
|
|
|
|
standard library package.
|
|
|
|
|
|
|
|
Doing so ensures that the link will valid even when the package documentation
|
|
|
|
is built independently (separately from the core documentation).
|
|
|
|
|
|
|
|
The link should lead to a target in https://docs.ruby-lang.org/en/master/.
|
|
|
|
|
|
|
|
Also use a full URL-based link for a link to an off-site document.
|
|
|
|
|
2023-08-23 09:09:24 -05:00
|
|
|
### Variable Names
|
|
|
|
|
|
|
|
The name of a variable (as specified in its call-seq) should be marked up as
|
|
|
|
[monofont](rdoc-ref:RDoc::MarkupReference@Monofont).
|
|
|
|
|
|
|
|
Also, use monofont text for the name of a transient variable
|
|
|
|
(i.e., one defined and used only in the discussion, such as +n+).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-10-04 13:50:33 -05:00
|
|
|
### HTML Tags
|
|
|
|
|
|
|
|
In general, avoid using HTML tags (even in formats where it's allowed)
|
|
|
|
because `ri` (the Ruby Interactive reference tool)
|
|
|
|
may not render them properly.
|
|
|
|
|
|
|
|
### Tables
|
|
|
|
|
2022-10-06 15:02:37 -05:00
|
|
|
In particular, avoid building tables with HTML tags
|
|
|
|
(<tt><table></tt>, etc.).
|
2022-10-04 13:50:33 -05:00
|
|
|
|
2023-09-24 16:57:21 -05:00
|
|
|
Alternatives:
|
2022-10-04 13:50:33 -05:00
|
|
|
|
|
|
|
- A {verbatim text block}[rdoc-ref:RDoc::MarkupReference@Verbatim+Text+Blocks],
|
2023-09-24 16:57:21 -05:00
|
|
|
using spaces and punctuation to format the text;
|
|
|
|
note that {text markup}[rdoc-ref:RDoc::MarkupReference@Text+Markup]
|
|
|
|
will not be honored:
|
|
|
|
|
|
|
|
- Example {source}[https://github.com/ruby/ruby/blob/34d802f32f00df1ac0220b62f72605827c16bad8/file.c#L6570-L6596].
|
|
|
|
- Corresponding {output}[rdoc-ref:File@Read-2FWrite+Mode].
|
|
|
|
|
|
|
|
- (Markdown format only): A {Github Flavored Markdown (GFM) table}[https://github.github.com/gfm/#tables-extension-],
|
|
|
|
using special formatting for the text:
|
|
|
|
|
|
|
|
- Example {source}[https://github.com/ruby/ruby/blob/34d802f32f00df1ac0220b62f72605827c16bad8/doc/contributing/glossary.md?plain=1].
|
|
|
|
- Corresponding {output}[https://docs.ruby-lang.org/en/master/contributing/glossary_md.html].
|
2022-10-04 13:50:33 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
## Documenting Classes and Modules
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The general structure of the class or module documentation should be:
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
- Synopsis
|
|
|
|
- Common uses, with examples
|
|
|
|
- "What's Here" summary (optional)
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Synopsis
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The synopsis is a short description of what the class or module does
|
|
|
|
and why the reader might want to use it.
|
|
|
|
Avoid details in the synopsis.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Common Uses
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
Show common uses of the class or module.
|
|
|
|
Depending on the class or module, this section may vary greatly
|
|
|
|
in both length and complexity.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### What's Here Summary
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The documentation for a class or module may include a "What's Here" section.
|
|
|
|
|
|
|
|
Guidelines:
|
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
- The section title is `What's Here`.
|
2021-07-18 17:43:34 -05:00
|
|
|
- Consider listing the parent class and any included modules; consider
|
2023-06-28 08:05:43 -05:00
|
|
|
[links](rdoc-ref:RDoc::MarkupReference@Links)
|
2021-07-18 17:43:34 -05:00
|
|
|
to their "What's Here" sections if those exist.
|
2023-12-02 11:46:57 -06:00
|
|
|
- All methods mentioned in the left-pane table of contents
|
|
|
|
should be listed (including any methods extended from another class).
|
|
|
|
- Attributes (which are not included in the TOC) may also be listed.
|
|
|
|
- Display methods as items in one or more bullet lists:
|
2022-03-07 05:04:16 -06:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
- Begin each item with the method name, followed by a colon
|
|
|
|
and a short description.
|
|
|
|
- If the method has aliases, mention them in parentheses before the colon
|
|
|
|
(and do not list the aliases separately).
|
|
|
|
- Check the rendered documentation to determine whether \RDoc has recognized
|
|
|
|
the method and linked to it; if not, manually insert a
|
2023-06-28 08:05:43 -05:00
|
|
|
[link](rdoc-ref:RDoc::MarkupReference@Links).
|
2022-03-07 05:04:16 -06:00
|
|
|
|
2023-06-28 08:05:43 -05:00
|
|
|
- If there are numerous entries, consider grouping them into subsections with headings.
|
2021-07-18 17:43:34 -05:00
|
|
|
- If there are more than a few such subsections,
|
|
|
|
consider adding a table of contents just below the main section title.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
## Documenting Methods
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### General Structure
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The general structure of the method documentation should be:
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
- Calling sequence (for methods written in C).
|
|
|
|
- Synopsis (short description).
|
2024-08-07 23:21:11 +01:00
|
|
|
- In-brief examples (optional)
|
2022-05-09 11:45:50 -04:00
|
|
|
- Details and examples.
|
|
|
|
- Argument description (if necessary).
|
|
|
|
- Corner cases and exceptions.
|
|
|
|
- Related methods (optional).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Calling Sequence (for methods written in C)
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
For methods written in Ruby, \RDoc documents the calling sequence automatically.
|
|
|
|
|
|
|
|
For methods written in C, \RDoc cannot determine what arguments
|
|
|
|
the method accepts, so those need to be documented using \RDoc directive
|
2023-06-28 08:05:43 -05:00
|
|
|
[`call-seq:`](rdoc-ref:RDoc::MarkupReference@Directives+for+Method+Documentation).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
For a singleton method, use the form:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2022-07-12 20:49:24 -05:00
|
|
|
class_name.method_name(method_args) {|block_args| ... } -> return_type
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
Example:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2022-07-12 20:49:24 -05:00
|
|
|
* call-seq:
|
|
|
|
* Hash.new(default_value = nil) -> new_hash
|
|
|
|
* Hash.new {|hash, key| ... } -> new_hash
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
For an instance method, use the form
|
|
|
|
(omitting any prefix, just as RDoc does for a Ruby-coded method):
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2022-07-12 20:49:24 -05:00
|
|
|
method_name(method_args) {|block_args| ... } -> return_type
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2024-12-12 20:49:45 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
For example, in Array, use:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2022-07-12 20:49:24 -05:00
|
|
|
* call-seq:
|
|
|
|
* count -> integer
|
|
|
|
* count(obj) -> integer
|
|
|
|
* count {|element| ... } -> integer
|
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* call-seq:
|
2022-07-12 20:49:24 -05:00
|
|
|
* <=> other -> -1, 0, 1, or nil
|
2022-05-09 11:45:50 -04:00
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-07-27 16:18:09 +01:00
|
|
|
For a binary-operator style method (e.g., Array#&),
|
|
|
|
cite `self` in the call-seq (not, e.g., `array` or `receiver`):
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* call-seq:
|
|
|
|
* self & other_array -> new_array
|
2024-07-27 16:18:09 +01:00
|
|
|
```
|
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
Arguments:
|
|
|
|
|
|
|
|
- If the method does not accept arguments, omit the parentheses.
|
|
|
|
- If the method accepts optional arguments:
|
|
|
|
|
|
|
|
- Separate each argument name and its default value with ` = `
|
|
|
|
(equal-sign with surrounding spaces).
|
|
|
|
- If the method has the same behavior with either an omitted
|
|
|
|
or an explicit argument, use a `call-seq` with optional arguments.
|
|
|
|
For example, use:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* call-seq:
|
|
|
|
* respond_to?(symbol, include_all = false) -> true or false
|
2022-07-12 20:49:24 -05:00
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
- If the behavior is different with an omitted or an explicit argument,
|
|
|
|
use a `call-seq` with separate lines.
|
|
|
|
For example, in Enumerable, use:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* call-seq:
|
2022-07-12 20:49:24 -05:00
|
|
|
* max -> element
|
|
|
|
* max(n) -> array
|
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
Block:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
- If the method does not accept a block, omit the block.
|
|
|
|
- If the method accepts a block, the `call-seq` should have `{|args| ... }`,
|
|
|
|
not `{|args| block }` or `{|args| code }`.
|
2024-08-22 16:49:28 +01:00
|
|
|
- If the method accepts a block, but returns an Enumerator when the block is omitted,
|
|
|
|
the `call-seq` should show both forms:
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* call-seq:
|
|
|
|
* array.select {|element| ... } -> new_array
|
|
|
|
* array.select -> new_enumerator
|
|
|
|
```
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-07-12 20:49:24 -05:00
|
|
|
Return types:
|
|
|
|
|
|
|
|
- If the method can return multiple different types,
|
|
|
|
separate the types with "or" and, if necessary, commas.
|
|
|
|
- If the method can return multiple types, use +object+.
|
|
|
|
- If the method returns the receiver, use +self+.
|
|
|
|
- If the method returns an object of the same class,
|
|
|
|
prefix `new_` if an only if the object is not +self+;
|
|
|
|
example: `new_array`.
|
|
|
|
|
|
|
|
Aliases:
|
|
|
|
|
2023-02-19 11:58:00 -08:00
|
|
|
- Omit aliases from the `call-seq`, unless the alias is an
|
|
|
|
operator method. If listing both a regular method and an
|
|
|
|
operator method in the `call-seq`, explain in the details and
|
|
|
|
examples section when it is recommended to use the regular method
|
|
|
|
and when it is recommended to use the operator method.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Synopsis
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
The synopsis comes next, and is a short description of what the
|
|
|
|
method does and why you would want to use it. Ideally, this
|
|
|
|
is a single sentence, but for more complex methods it may require
|
|
|
|
an entire paragraph.
|
|
|
|
|
2022-05-19 17:07:56 +09:00
|
|
|
For `Array#count`, the synopsis is:
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
> Returns a count of specified elements.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
This is great as it is short and descriptive. Avoid documenting
|
|
|
|
too much in the synopsis, stick to the most important information
|
|
|
|
for the benefit of the reader.
|
|
|
|
|
2024-08-07 23:21:11 +01:00
|
|
|
### In-Brief Examples
|
|
|
|
|
|
|
|
For a method whose documentation is lengthy,
|
|
|
|
consider adding an "in-brief" passage,
|
|
|
|
showing examples that summarize the method's uses.
|
|
|
|
|
|
|
|
The passage may answer some users' questions
|
|
|
|
(without their having to read long documentation);
|
|
|
|
see Array#[] and Array#[]=.
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Details and Examples
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
Most non-trivial methods benefit from examples, as well as details
|
|
|
|
beyond what is given in the synopsis. In the details and examples
|
|
|
|
section, you can document how the method handles different types
|
|
|
|
of arguments, and provides examples on proper usage. In this
|
|
|
|
section, focus on how to use the method properly, not on how the
|
|
|
|
method handles improper arguments or corner cases.
|
|
|
|
|
|
|
|
Not every behavior of a method requires an example. If the method
|
2022-05-09 11:45:50 -04:00
|
|
|
is documented to return `self`, you don't need to provide an example
|
2021-07-18 17:43:34 -05:00
|
|
|
showing the return value is the same as the receiver. If the method
|
2022-05-09 11:45:50 -04:00
|
|
|
is documented to return `nil`, you don't need to provide an example
|
|
|
|
showing that it returns `nil`. If the details mention that for a
|
2021-07-18 17:43:34 -05:00
|
|
|
certain argument type, an empty array is returned, you don't need
|
|
|
|
to provide an example for that.
|
|
|
|
|
|
|
|
Only add an example if it provides the user additional information,
|
|
|
|
do not add an example if it provides the same information given
|
|
|
|
in the synopsis or details. The purpose of examples is not to prove
|
|
|
|
what the details are stating.
|
|
|
|
|
2024-08-22 16:49:28 +01:00
|
|
|
Many methods that can take an optional block call the block if it is given,
|
|
|
|
but return a new Enumerator if the block is not given;
|
|
|
|
in that case, do not provide an example,
|
|
|
|
but do state the fact (with the auto-linking uppercase Enumerator):
|
|
|
|
|
2024-12-12 20:49:45 -05:00
|
|
|
```rdoc
|
2024-08-22 16:49:28 +01:00
|
|
|
* With no block given, returns a new Enumerator.
|
|
|
|
```
|
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Argument Description (if necessary)
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
For methods that require arguments, if not obvious and not explicitly
|
|
|
|
mentioned in the details or implicitly shown in the examples, you can
|
|
|
|
provide details about the types of arguments supported. When discussing
|
|
|
|
the types of arguments, use simple language even if less-precise, such
|
|
|
|
as "level must be an integer", not "level must be an Integer-convertible
|
|
|
|
object". The vast majority of use will be with the expected type, not an
|
|
|
|
argument that is explicitly convertible to the expected type, and
|
|
|
|
documenting the difference is not important.
|
|
|
|
|
|
|
|
For methods that take blocks, it can be useful to document the type of
|
|
|
|
argument passed if it is not obvious, not explicitly mentioned in the
|
|
|
|
details, and not implicitly shown in the examples.
|
|
|
|
|
|
|
|
If there is more than one argument or block argument, use a
|
2023-06-28 08:05:43 -05:00
|
|
|
[labeled list](rdoc-ref:RDoc::MarkupReference@Labeled+Lists).
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Corner Cases and Exceptions
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
For corner cases of methods, such as atypical usage, briefly mention
|
|
|
|
the behavior, but do not provide any examples.
|
|
|
|
|
|
|
|
Only document exceptions raised if they are not obvious. For example,
|
|
|
|
if you have stated earlier than an argument type must be an integer,
|
2022-05-19 17:07:56 +09:00
|
|
|
you do not need to document that a `TypeError` is raised if a non-integer
|
2021-07-18 17:43:34 -05:00
|
|
|
is passed. Do not provide examples of exceptions being raised unless
|
2022-05-19 17:07:56 +09:00
|
|
|
that is a common case, such as `Hash#fetch` raising a `KeyError`.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Related Methods (optional)
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
In some cases, it is useful to document which methods are related to
|
2022-05-09 11:45:50 -04:00
|
|
|
the current method. For example, documentation for `Hash#[]` might
|
|
|
|
mention `Hash#fetch` as a related method, and `Hash#merge` might mention
|
2022-08-22 13:17:05 -05:00
|
|
|
`Hash#merge!` as a related method.
|
|
|
|
|
|
|
|
- Consider which methods may be related
|
|
|
|
to the current method, and if you think the reader would benefit it,
|
|
|
|
at the end of the method documentation, add a line starting with
|
|
|
|
"Related: " (e.g. "Related: #fetch.").
|
|
|
|
- Don't list more than three related methods.
|
|
|
|
If you think more than three methods are related,
|
|
|
|
list the three you think are most important.
|
|
|
|
- Consider adding:
|
|
|
|
|
|
|
|
- A phrase suggesting how the related method is similar to,
|
|
|
|
or different from,the current method.
|
|
|
|
See an example at Time#getutc.
|
|
|
|
- Example code that illustrates the similarities and differences.
|
|
|
|
See examples at Time#ctime, Time#inspect, Time#to_s.
|
2021-07-18 17:43:34 -05:00
|
|
|
|
2022-05-09 11:45:50 -04:00
|
|
|
### Methods Accepting Multiple Argument Types
|
2021-07-18 17:43:34 -05:00
|
|
|
|
|
|
|
For methods that accept multiple argument types, in some cases it can
|
|
|
|
be useful to document the different argument types separately. It's
|
|
|
|
best to use a separate paragraph for each case you are discussing.
|