Skip to content

Instantly share code, notes, and snippets.

@chetan
Last active November 11, 2024 08:27
Show Gist options
  • Save chetan/1827484 to your computer and use it in GitHub Desktop.
Save chetan/1827484 to your computer and use it in GitHub Desktop.
YARD cheatsheet

YARD CHEATSHEET http://yardoc.org

cribbed from http://pastebin.com/xgzeAmBn

Templates to remind you of the options and formatting for the different types of objects you might want to document using YARD.

Modules

# Namespace for classes and modules that handle serving documentation over HTTP
# @since 0.6.0

Classes

# Abstract base class for CLI utilities. Provides some helper methods for
# the option parser
# 
# @author Full Name
# @abstract
# @since 0.6.0
# @attr [Types] attribute_name a full description of the attribute
# @attr_reader [Types] name description of a readonly attribute
# @attr_writer [Types] name description of writeonly attribute
# @deprecated Describe the reason or provide alt. references here

Methods

# An alias to {Parser::SourceParser}'s parsing method
# 
# @author Donovan Bray
#
# @see http://example.com Description of URL
# @see SomeOtherClass#method
#
# @deprecated Use {#my_new_method} instead of this method because
#   it uses a library that is no longer supported in Ruby 1.9. 
#   The new method accepts the same parameters.
#
# @abstract
# @private
#
# @param opts [Hash] the options to create a message with.
# @option opts [String] :subject The subject
# @option opts [String] :from ('nobody') From address
# @option opts [String] :to Recipient email
# @option opts [String] :body ('') The email's body 
#
# @param (see User#initialize)
# @param [OptionParser] opts the option parser object
# @param [Array<String>] args the arguments passed from input. This
#   array will be modified.
# @param [Array<String, Symbol>] list the list of strings and symbols.
#
# The options parsed out of the commandline.
# Default options are:
#   :format => :dot
#
# @example Reverse a string
#   "mystring.reverse" #=> "gnirtsym"
#
# @example Parse a glob of files
#   YARD.parse('lib/**/*.rb')
#  
# @raise [ExceptionClass] description
#
# @return [optional, types, ...] description
# @return [true] always returns true
# @return [void]
# @return [String, nil] the contents of our object or nil
#   if the object has not been filled with data.
#
# We don't care about the "type" here:
# @return the object
#
# @return [String, #read] a string or object that responds to #read
# @return description here with no types

Anywhere

# @todo Add support for Jabberwocky service
#   There is an open source Jabberwocky library available 
#   at http://somesite.com that can be integrated easily
#   into the project.

Blocks

# for block {|a, b, c| ... }
# @yield [a, b, c] Description of block
#
# @yieldparam [optional, types, ...] argname description
# @yieldreturn [optional, types, ...] description
@VirtuosoJoel
Copy link

I've started to use this kind of notation between closely related methods:

{MyClass#uniq!}

def uniq ...

@axsuul
Copy link

axsuul commented Jan 11, 2018

Note that this reference sheet is outdated as of 2018

@nimylian
Copy link

@axsuul can you provide some reference for that assertion?

@ForeverZer0
Copy link

@nimylian To answer your question, I assume what @axsuul was referring to was primarily the "Class" section above, while not technically "incorrect", is outdated and deprecated.

It clearly states that the @attr, @attr_reader, and @attr_writer tags are deprecated in favor of @!attribute [r|w] tag.
The former tags still work perfectly well, and do not issue any errors or warnings, but are technically deprecated. Those new to YARD and learning its syntax using a cheat-sheet would be better served to learn the the "modern" tags, not deprecated tags that may or may not exist in future releases.

Reference for Assertion

@tiagopog
Copy link

tiagopog commented Nov 19, 2018

@chetan can you add an example for documenting the key and value types of a Hash? e.g. { foo: "bar" } #=> Hash<Symbol, String>

@phansch
Copy link

phansch commented Jan 29, 2019

I have been keeping a forked version of this cheatsheet up to date here: https://gist.github.com/phansch/db18a595d2f5f1ef16646af72fe1fb0e Let me know if there's anything else that needs updating.

@arthurmde
Copy link

arthurmde commented May 11, 2020

Sometimes, you may want to add a small block of code to your documentation with <code></code> tags. You can do this with the following structure:

+my code+ will produce <code>my code</code>

@deepgreenAntennas
Copy link

nice job, man. this looks very nice. You put in a great amount of time to produce this I am very happy for you. Any advice on swift integration with beta tools Big Sur would be helpful. ive got wine firing off just fine in Big Sur on command line level but building it as a scattergorry (spelling) is not something I tend to wonder will function with anything that rivals my R compendium. Thanks for sharing!

@entrity
Copy link

entrity commented Aug 23, 2024

@chetan would you consider updating @param to match to the documentation indicated by yardoc.org?

I see https://yardoc.org/guides/ links "Getting Started" to https://rubydoc.info/gems/yard/file/docs/GettingStarted.md, and that page gives the following, with the name of the param (format) coming before the type of the param ([Symbol]):

@param format [Symbol] the format type, `:text` or `:html`

The present version of this gist has the name of the param (opts) coming after the type of the param ([Hash]):

@param [Hash] opts the options to create a message with.

@chetan
Copy link
Author

chetan commented Aug 26, 2024

@entrity 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment