Mark a parameter as optional (or has a default) with YARD
题
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
解决方案
To mark a parameter is option you can simply use @param optional
(see http://rubydoc.info/docs/yard/file/docs/Tags.md). As far as I know, there's now way to notate a default value; you're probably best to put it in the description ("the number to square, defaults to 2")
其他提示
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)
不隶属于 StackOverflow