224

Are there certain code conventions when documenting ruby code? For example I have the following code snippet:

require 'open3'

module ProcessUtils

  # Runs a subprocess and applies handlers for stdout and stderr
  # Params:
  # - command: command line string to be executed by the system
  # - outhandler: proc object that takes a pipe object as first and only param (may be nil)
  # - errhandler: proc object that takes a pipe object as first and only param (may be nil)
  def execute_and_handle(command, outhandler, errhandler)
    Open3.popen3(command) do |_, stdout, stderr|
      if (outhandler)
        outhandler.call(stdout)
      end
      if (errhandler)
        errhandler.call(stderr)
      end
    end
  end
end

This guess this is okay, but perhaps there are better/superior documentation practices?

1
  • shop.oreilly.com/product/9780596516178.do has a nice little example in the source code. Look in chapter 2 listing. It's about like the answer here. I've played with rdoc just to show source code. You can make your file extension something like my_code.rb to my_code.rb.txt and then run rdoc on it. > rdoc my_code.rb.txt then it won't matter about classes and modules because rdoc will render html for it anyway. Have fun with it. Commented Apr 29, 2014 at 9:11

7 Answers 7

219

You should target your documentation for the RDoc processor, which can find your documentation and generate HTML from it. You've put your comment in the right place for that, but you should have a look at the RDoc documentation to learn about the kinds of tags that RDoc knows how to format. To that end, I'd reformat your comment as follows:

  # Runs a subprocess and applies handlers for stdout and stderr
  # Params:
  # +command+:: command line string to be executed by the system
  # +outhandler+:: +Proc+ object that takes a pipe object as first and only param (may be nil)
  # +errhandler+:: +Proc+ object that takes a pipe object as first and only param (may be nil)
3
  • How should I document that the outhandler and errhandler parameters may be nil? Commented Nov 5, 2009 at 16:18
  • 10
    YARD's annotations may be more powerful, but until it's included in the standard Ruby distribution instead of RDoc, its annotations are not the standard.
    – Ken Bloom
    Commented Nov 5, 2009 at 17:35
  • The RDoc link is broken try this: github.com/ruby/rdoc. I'll request to edit the answer if everyone is happy with that link. Commented Sep 27, 2017 at 3:27
31

I would highly suggest using RDoc. It is pretty much the standard. It is easy to read the code comments, and it allows you to easily create web-based documentation for your project.

1
  • Over the years, I've actually switched to YARD as well. Commented Dec 14, 2023 at 18:04
27

I would suggest getting to know RDoc as is stated. But don't ignore the very popular YARD A Ruby Document tool, as well. A lot of the documentation you will see online for Ruby uses Yard. RVM knows Yard and uses it for generating your documentation on your machine if it is available.

RDoc would still be required, as Yard uses it.

3
  • 1
    Having used mostly C++, Java, Scala and PHP, I find the @tag notation very familiar.
    – doub1ejack
    Commented Dec 30, 2016 at 23:13
  • 1
    It has been four years and YARD has evolved greatly. It's a pity that YARD is still not included in Ruby. (By the way YARD homepage accepts HTTPS.) Commented Oct 25, 2017 at 15:30
  • 1
    YARD seems to be lighter than RDoc! Thanks :) Commented Nov 9, 2019 at 14:19
19

Rails has some API Documentation Guidelines. That's probably a good starting point.

9

You can also check TomDoc for Ruby - still Version 1.0.0-rc1 as of 19.09.2022.

4
  • FWIW, this one is specified in the GitHub style guide - github.com/styleguide/ruby Commented Dec 7, 2013 at 23:12
  • Thanks, tomdoc seems to be a good source for best current practices when it comes to documenting ruby code. Answers the "how" and "why" that's apparently missing from rdoc documentation. Commented Oct 27, 2014 at 16:27
  • TomDoc hasn't been kept up to date. Last commit was May 2012.
    – maasha
    Commented Apr 9, 2015 at 9:12
  • 1
    @maasha By 2017 I believe the best bet besides plain RDoc would be YARD, now that it parses the content and makes some fancy hyperlinks to classes and methods. Commented Oct 25, 2017 at 15:41
2

The canonical is RDoc it is very similar to the one you've posted.

See the sample section on the link I sent you

2

Here is the documentation for the ruby documentation system (RDOC)

Not the answer you're looking for? Browse other questions tagged or ask your own question.