InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

YARD - Code Metadata And Documentation Generation for Ruby

Posted by Werner Schuster on Jul 29, 2008

Sections
Development
Topics
Ruby ,
IDE ,
Dynamic Languages
Tags
IDEs ,
RubyFringe ,
Merb ,
Documentation
In his talk at RubyFringe, Merb contributor and EngineYard employee Yehuda Katz mentioned a few tools to watch in the future. One of the tools is the documentation generation tool YARD, which allows to add metadata to Ruby documentation, among other features. YARD is designed to play well with RDoc formatted doc strings, but allows to use different markup notations. YARD is extensible, allowing to plug in custom handlers for code constructs and different output backends.

One distinguishing feature is the support for metadata in comment strings. Users of tools like Javadoc will find the meta tag notation familiar (from the YARD Readme):
# Reverses the contents of a String or IO object. 
#
# @param [String, #read] contents the contents to reverse
# @return [String] the contents reversed lexically
def reverse(contents)
contents = contents.read if respond_to? :read
 contents.reverse
end

The contents parameter shows how to add type hints to the method argument. The argument can either be a String, or any class with a #read method (InfoQ previously discussed different approaches to type annotations in relation to protocols and duck typing).

Both RDoc 2.1 and YARD provide ways to document metaprogrammed methods. With RDoc, a comment starting with "##" defines a comment for a metaprogrammed method. The heuristic is to ignore the identifier immediately following the comment and take the following token as the method name:
## 
# Does stuff.
add_method :foo

Methods that don't actually appear in the source code, but are part of the class interface can be documented like this:

##
# :method invisible_method

YARD is built with pluggable handlers - in fact, the basic functionality is implemented as handlers. A new handler can be added by extending the YARD::Handlers::Base class, and overriding the process class, which allows to write handlers for custom constructs, such as internal DSLs or behavior similar to RDoc's solutions.

Running YARD on a project creates a .yardoc database which caches the gathered code structure and data. YARD's yri tool, which works like ri and uses this database to allow for interactive documentation lookup. YARD can also use the cached information in the database to generate output in multiple formats, without having to analyze the repeatedly. YARD's cache is similar to code indexes created by IDEs to allow for advanced code search (i.e. searching for language constructs, not just fulltext search), code browsing, or for refactoring tools that need to be aware of all code in a project.

The idea to provide metadata about methods in comments has been around for some time in the Ruby space. Some projects promote optional type annotations in comments. Merb, for instance, has the following documentation guideline:
All methods (public and private) are required to provide a clear method signature, including the types for any parameters, and the possible values for any options hashes, as well as return types and other information.
Merb currently uses a different notation to specify the method signature.

Another approach is supported in SapphireSteel's Ruby In Steel IDE: Type Assertions The metadata uses a different format than the @tag of Javadoc or YARD, but it's indexed and used for documentation and to help IntelliSense (Screencast describing how to help IntelliSense with Type Assertions).
Change the language, not the documentation style by Daniel Berger Posted
  1. Back to top

    Change the language, not the documentation style

    by Daniel Berger

    One of things that I talked with Charles Nutter about on #jruby is that, if you allowed optional typing and method annotations in the language itself, document generators could use that information instead of forcing authors to adopt a particular documentation style.



    For example, let's say we implement some form of Duby, with annotations to boot. You'll end up with something like this:


    def reverse(String contents) => String
    ...
    end

    Meaning contents is a String, and the method returns a String. In addition to making it easier for document generators, it provides additional, inspectible metadata for that method.

    Note that I'm not even suggesting that the argument type be enforced. It could be strictly for document generation, or it could merely emit a warning or whatever we want it to do.

Educational Content

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?