Ruby Logo

Documentation with YARD

Document Ruby APIs with YARD tags, generate HTML references, and embed docs into engineering workflows.

Home Ruby Documentation with YARD

Documentation with YARD

YARD turns Ruby source into navigable API docs. We annotate service objects, public libraries, and helper modules so contributors can discover contracts quickly. Use YARD in tandem with RuboCop’s documentation cops to keep interfaces discoverable.

Install & Initialize

# Gemfile (development group) gem 'yard', require: false # First-time setup bundle install bundle exec yard config --gem # adds .yardopts if missing

Store `.yardopts` at the project root. Include directories we want documented (e.g., `app/services`, `lib`).

Annotate Public APIs

# @!group Enrollment APIs # @param user [User] the learner we are enrolling # @param lesson [Lesson] the lesson to mark as started # @return [Progress] the persisted progress record # @raise [Progress::DuplicateError] when the lesson is already complete def enroll!(user:, lesson:) Progress::Creator.call(user:, lesson:) end

Use YARD’s tags (`@param`, `@return`, `@raise`, `@example`) to document contracts. Group related methods with `@!group` so the generated docs reveal module cohesion.

Generate & Publish

bundle exec yard doc app/services app/queries lib open doc/index.html # macOS shortcut to preview docs # Continuous documentation in CI (pseudo-step) bundle exec yard doc --output-dir tmp/yard aws s3 sync tmp/yard s3://docs.rubydev.io --delete

Publish docs alongside deployments or as part of an internal handbook. If we ship a gem, add `yard` to the release pipeline so external consumers get fresh docs with every version.

Review Checklist

  • Every public method in a service object has `@param` and `@return` annotations.
  • Callbacks, background jobs, and CLI entry points include `@example` snippets.
  • Docs rebuild cleanly (`bundle exec yard stats`) with zero undocumented public objects.
  • Link YARD sections from README or internal docs so developers know where to look.

Quick Navigation

Related Topics

Video Tutorial

Watch and learn documentation with yard

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!