干燥场参数

我已经得到了我认为是一个相当基本的码宏用法,其中我有一个Hash结构,其中包含多个函数之间共享的多个选项。 我希望使用一个宏只是为了防止我不得不在整个地方复制这个结构,但它似乎并没有像那样工作。

我期望工作的是:

# @macro [new] my_hash # @param $1 [Hash,Array] Inputted my_hash # @option $1 [String] :a Value for A. # @option $1 [String] :b Value for B. # @option $1 [String] :c Value for C. ## # my_func does stuff # @macro my_hash h1 def my_func h1 end ## # other_func does stuff # @macro my_hash h2 def other_func h2, a, b end 

并正确记录了h1和h2。 我想我已经发现,至少1美元不能像那样工作而不是从函数本身,但我可以调用两个参数h1并用宏中的h1替换$ 1,我仍然没有得到我想要的。

http://rubydoc.info/docs/yard/file/docs/Tags.md#macro上的“定义简单宏”示例表明我可以这样做(注意我找到了带有@!宏的示例,有些没有但是似乎都不起作用)。

关于院子我不是很了解但是这不起作用吗? 有什么类似的东西,我可以做到实现我的结果? 是否有调试function可以解释这一点,因为院子服务器控制台没有出现错误?

谢谢

正如您已在宏中指定的那样,它应该针对方法的第一个参数进行扩展,而不是在扩展宏时不需要指定它。 此外,似乎无法确定扩展宏的参数。 另一件事是你可以在这种情况下跳过[new]并且似乎最好使用@!macro而不是@macro

 # @!macro my_hash # @param $1 [Hash,Array] Inputted my_hash # @option $1 [String] :a Value for A. # @option $1 [String] :b Value for B. # @option $1 [String] :c Value for C. ## # my_func does stuff # @!macro my_hash def my_func h1 end ## # other_func does stuff # @!macro my_hash def other_func h2, a, b end 

我相信你可以使用YARDs 参考标签完成你想要的东西。

来自文档:

如果标签的数据以(see OBJECT)开头,则它被视为“参考标签”。 参考标记从指定的OBJECT按照给定的标记名称逐字复制标记数据。 例如,方法可以使用引用标记语法从给定对象复制所有@param标记:

 # @param [String] user the username for the operation # @param [String] host the host that this user is associated with # @param [Time] time the time that this operation took place def clean(user, host, time = Time.now) end # @param (see #clean) def activate(user, host, time = Time.now) end 

这适用于yardoc 0.9.8(可能是任何版本):

 # @!macro my_hash # @param $1 [Hash,Array] Inputted my_hash # @option $1 [String] :a Value for A. # @option $1 [String] :b Value for B. # @option $1 [String] :c Value for C. ## # my_func does stuff # @macro my_hash def my_func h1 end ## # other_func does stuff # @macro my_hash def other_func h2, a, b end 

注意丢失的感叹号! 用@macro调用宏时。 这是正确的yardoc语法 。