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

2

34

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
Healall answered 21/10, 2011 at 16:47 Comment(1)
YARD now supports Param Defaults automatically. I added an answer to help new comers landing here.Beverage
B
-1

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")

Boatyard answered 21/10, 2011 at 17:13 Comment(2)
I don't see this as an option in the documentation, and all the permutations I've tried of adding optional resulted in the string 'optional' being added to the output in various places without any special format. Maybe I'm missing something but this doesn't seem to be supported?Leff
maybe the other answer should be the accepted answer, as it does support based on the method parameter definitionNonprofit
B
70

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)
Beverage answered 23/12, 2011 at 23:59 Comment(5)
Automatic inference doesn't help if you're using something like the contracts gem to enforce required or optional parameters. Would be nice if YARD allowed being explicit here.Worldshaking
@AbeVoelker I haven't used the Contracts gem but from what I can tell, it enforces the expected type of the parameters, not whether they are optional or required, that is still handled by whether a value is assigned in the method parameters.Beverage
I do use it, and there is a KeywordArgs contract which allows nested Optional contract type. Example: github.com/abevoelker/scrapinghub/blob/…Worldshaking
@AbeVoelker Ahh, I gotcha, so situations where you're defining a required method parameter of type Hash, like def list(args), and some of the keys in args are required and some are optional.Beverage
@AbeVoelker You can use @!method to provide a signature that includes a default in that case, and the default will be read from that just as if it were in the def <method-name> in the code.Throwback
B
-1

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")

Boatyard answered 21/10, 2011 at 17:13 Comment(2)
I don't see this as an option in the documentation, and all the permutations I've tried of adding optional resulted in the string 'optional' being added to the output in various places without any special format. Maybe I'm missing something but this doesn't seem to be supported?Leff
maybe the other answer should be the accepted answer, as it does support based on the method parameter definitionNonprofit

© 2022 - 2024 — McMap. All rights reserved.