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)
Sign up to request clarification or add additional context in comments.

3 Comments

How should I document that the outhandler and errhandler parameters may be nil?
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.
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.
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 Comment

Over the years, I've actually switched to YARD as well.
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.

2 Comments

Having used mostly C++, Java, Scala and PHP, I find the @tag notation very familiar.
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.)
19

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

Comments

9

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

4 Comments

FWIW, this one is specified in the GitHub style guide - github.com/styleguide/ruby
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.
TomDoc hasn't been kept up to date. Last commit was May 2012.
@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.
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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.