Mark a parameter as optional (or has a default) with YARD

brainimus picture brainimus · Oct 21, 2011 · Viewed 8.8k times · Source

I'm using YARD to document my code. I have a method that has an optional parameter with a default value. How do I notate that the parameter is optional and has a default value?

Example:

# Squares a number
# 
# @param the number to square
def square_a_number(number = 2)
  number * number
end

Answer

Joshua Pinter picture Joshua Pinter · Dec 24, 2011

YARD now supports param defaults automatically.

YARD automatically figures out the default value based on the method definition. Sweedish!

For example, the following code documentation will produce the subsequent YARD doc:

Code Documentation

# Squares a number.
# 
# @param number [Integer] The number to square.
#
def square_a_number(number = 2)
  number * number
end

Resulting YARD Documentation

Parameters:
  number (Integer optional) (defaults to: 2)