Ruby RDoc
Documentation generator for Ruby code
📚 What is RDoc?
RDoc generates HTML documentation from Ruby source code and comments. It extracts class, module, and method definitions along with their comments to create professional documentation. RDoc helps developers understand and use your code effectively.
# This class represents a person
class Person
# Creates a new person with a name
def initialize(name)
@name = name
end
end
Command:
$ rdoc person.rb
Key RDoc Features
Comments
Document code with comments
# Method description
HTML Output
Generate web documentation
rdoc --format=html
Tags
Use special markup tags
# :call-seq:
Search
Searchable documentation
ri ClassName
🔹 Basic RDoc Comments
Add comments above classes, modules, and methods to document your code. RDoc extracts these comments and formats them into readable documentation with proper structure and formatting.
# This class manages user accounts
# It provides methods for creating and updating users
class User
# Creates a new user with email and password
#
# @param email [String] the user's email address
# @param password [String] the user's password
def initialize(email, password)
@email = email
@password = password
end
# Returns the user's email address
def email
@email
end
end
Generated Documentation:
Class: User
This class manages user accounts
Methods: initialize, email
🔹 Generating Documentation
Use the rdoc command to generate HTML documentation from your Ruby files. RDoc scans your code, extracts documentation comments, and creates a browsable website with all your code documentation.
# Generate docs for a single file
rdoc person.rb
# Generate docs for all Ruby files
rdoc *.rb
# Generate docs for a directory
rdoc lib/
# Specify output directory
rdoc --op doc lib/
# Generate with title
rdoc --title "My Project" lib/
Output:
Parsing sources...
100% [1/1] person.rb
Generating HTML...
Files: 1
Classes: 1
Modules: 0
Methods: 2
🔹 RDoc Markup
RDoc supports special markup syntax to format your documentation. Use these markup elements to create lists, code blocks, links, and emphasized text in your generated documentation.
# = Main Heading
# == Sub Heading
# === Smaller Heading
#
# This is a paragraph with *bold* and _italic_ text.
#
# * Item 1
# * Item 2
# * Item 3
#
# 1. First
# 2. Second
# 3. Third
#
# Code example:
# result = calculate(10, 20)
# puts result
#
# Link: https://ruby-doc.org
class Calculator
end
Formatted Output:
Main Heading
This is a paragraph with
bold
and
italic
text.
• Item 1
• Item 2
🔹 RDoc Tags
Special tags provide additional information about methods and parameters. These tags help create more detailed and structured documentation that's easier for other developers to understand and use.
Common Tags:
- :call-seq: - Method calling syntax
- :category: - Group related methods
- :section: - Organize documentation
- :nodoc: - Skip documentation
- :doc: - Force documentation
# :call-seq:
# add(a, b) -> Integer
# add(a, b, c) -> Integer
#
# Adds numbers together
def add(*numbers)
numbers.sum
end
# :category: Utility Methods
def helper_method
# ...
end
# :nodoc:
def internal_method
# This won't appear in docs
end
Documentation:
add(a, b) → Integer
Adds numbers together
🔹 Using ri Command
The ri command provides quick access to Ruby documentation from the terminal. Search for classes, methods, and modules without opening a browser, making it perfect for quick reference while coding.
# View class documentation
ri String
# View method documentation
ri String#upcase
# View module documentation
ri Enumerable
# Search for methods
ri -i upcase
# List all classes
ri -l
Output:
= String#upcase
Returns a copy of str with all lowercase letters
replaced with their uppercase counterparts.
🔹 Complete Documentation Example
Here's a fully documented class showing best practices for RDoc comments. This example demonstrates proper documentation structure with descriptions, parameters, return values, and usage examples.
# = Calculator
#
# A simple calculator class for basic arithmetic operations.
#
# == Usage
#
# calc = Calculator.new
# result = calc.add(5, 3)
# puts result # => 8
#
# Author:: John Doe
# License:: MIT
class Calculator
# Creates a new Calculator instance
def initialize
@history = []
end
# Adds two numbers together
#
# @param a [Numeric] first number
# @param b [Numeric] second number
# @return [Numeric] sum of a and b
#
# Example:
# calc.add(5, 3) # => 8
def add(a, b)
result = a + b
@history << result
result
end
# Returns calculation history
#
# @return [Array] array of previous results
def history
@history
end
end
Generated Docs:
Calculator
A simple calculator class
Methods:
initialize, add, history