问题描述
我的类将一个方法委托给另一个对象(我将其称为辅助对象)。我想在委托类中包含该方法的文档,而不仅仅是委托类。程序员不应该使用辅助对象,只使用主对象,所以在辅助对象中记录不是很有用。
考虑这个例子。 Rdoc 输出关于 Node#initialize
和 Helper#hello
的文档。我想要有关 Node#hello
的文档,就好像它只是另一种方法一样。有没有办法做到这一点?
require 'forwardable'
class Node
extend Forwardable
delegate %w(hello) => :@helper
# Takes no params.
def initialize
@helper = Helper.new()
end
end
class Helper
# Outputs 'hello world'
def hello
puts 'hello world'
end
end
-- 更新 --
我试过院子。 yard 命令如下所示:
yardoc --no-private --protected app/**/*.rb -
我编辑了 ruby 代码以尝试为 Node#hello 添加文档:
class Node
extend Forwardable
delegate %w(hello) => :@helper
# Takes no params.
def initialize
@helper = Helper.new()
end
# @!method hello
# Documentation for Node#hello
end
当我运行yardoc的时候,好像说它处理了三个方法:
Files: 1
Modules: 0 ( 0 undocumented)
Classes: 2 ( 2 undocumented)
Constants: 0 ( 0 undocumented)
Attributes: 0 ( 0 undocumented)
Methods: 3 ( 0 undocumented)
60.00% documented
如果我没有 # @!method
指令,那么它说它只处理两种方法:
Files: 1
Modules: 0 ( 0 undocumented)
Classes: 2 ( 2 undocumented)
Constants: 0 ( 0 undocumented)
Attributes: 0 ( 0 undocumented)
Methods: 2 ( 0 undocumented)
50.00% documented
所以看起来似乎是 Node#hello
的文档,但它没有出现在实际文档中:
所以我不确定从那里去哪里。
解决方法
根据@max 的建议,我切换到了院子里。使用 yard,我可以在没有任何实际方法的情况下创建方法文档,如下所示:
# @!method hello()
# Outputs "hello world"
delegate %w(hello) => :@helper
需要注意的是,文档必须在实际代码之上。如果单独使用它是行不通的。