简体   繁体   English

YARD 用于具有默认哈希值的关键字参数

[英]YARD for keyword arguments with default hash

I have a method that looks like this:我有一个看起来像这样的方法:

def get_endpoint(params: {})
end

I want the caller of this method to be able to pass in a few optional parameters.我希望这个方法的调用者能够传入一些可选参数。

I want to write YARD documentation to support this and if I wasn't using keyword arguments I'd use the @option declaration.我想编写 YARD 文档来支持这一点,如果我不使用关键字参数,我将使用@option声明。

However, YARD's own docs say:但是,YARD 自己的文档说:

Note: For keyword parameters, use @param, not @option.注意:对于关键字参数,使用@param,而不是@option。

So I tried:所以我试过:

  # @param params [Hash] options to be used in request
  # @param date [String] date in YYYYMMDD
  # @param start_time [Integer] start_time in Epoch

That fails because YARD only sees the params keyword argument I'm using.这失败了,因为 YARD 只能看到我正在使用的params关键字参数。 The exact failure is:确切的失败是:

@param tag has unknown parameter name: date

So then I tried to use the @option syntax replaced with the param keyword:然后我尝试使用@option语法替换为param关键字:

  # @param params [Hash] options to be used in request
  # @param params [String] :date in YYYYMMDD
  # @param params [Integer] :start_time in Epoch

That results in a different error:这会导致不同的错误:

@param tag has duplicate parameter name: params

Ideally I want to describe the params hash with the 3 or 4 options the user of this method can use.理想情况下,我想用此方法的用户可以使用的 3 或 4 个选项来描述params哈希。 Is there a way to do this?有没有办法做到这一点?

That signature does not use keyword arguments for :date or :start_time .该签名不使用:date:start_time关键字参数。 Keyword arguments for those arguments would be specified as something like:这些参数的关键字参数将被指定为:

def get_endpoint(date:, start_time:)

@option is specifically meant for specifying options that would be contained within an options Hash in your case params . @option专门用于指定将包含在您的案例params的 options Hash中的选项。 Since you are using a keyword argument for params I would recommend adding the @param tag for this as well to clearly identify the keyword argument.由于您对params使用关键字参数,因此我建议为此添加@param标签,以清楚地识别关键字参数。 For Example:例如:

@param params [Hash]  options to be used in request
@option params [String] :date in YYYYMMDD
@option params [Integer] :start_time in Epoch

Documentation for @options just in case. @options文档以防万一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM