给出以下ruby文件,foo.rb:
# this is a module comment
module A
# this is a constant comment
B = 'hi'
# this is a class comment
class C
# this is a method comment
# @param [String] name Who to say hi to
# @return [String]
def self.hi(name)
"#{B}, #{name}"
end
end
end
如何以编程方式获取与特定对象相关联的评论(例如{A::C => 'this is a class comment'}
,{B => 'this is a constant comment'}
)?
我希望YARD.parse(File.read('/path/to/foo.rb'))
或YARD::Parser::SourceParser.parse(File.read('/path/to/foo.rb'))
做一些事情,但它们会返回空数组。 YARD::Parser::Ruby::RubyParser.parse(File.read('/path/to/foo.rb'))
返回一个似乎是AST的YARD::Parser::Ruby::RipperParser
实例,但我宁愿避免编写AST遍历器(YARD必须具有此功能才能构建HTML文档,但我没有能够找到它。)
(如果有帮助,我正在使用YARD v0.9.9。)