【问题标题】:Yard doc and `define_method`庭院文档和`define_method`
【发布时间】:2015-02-10 01:40:19
【问题描述】:

有没有办法在YardDoc 中注释用define_method 定义的方法?

我试过这个:

%w(one two three).each do |type|
  # The #{type} way
  # @return [String] the #{type} way
  define_method("#{type}_way") do ... end
end

但不幸的是,它不起作用。

【问题讨论】:

  • 描述“不工作”的含义。你没有输出?你得到输出但它没有格式化?已格式化,但不是您想要的格式?
  • 什么都没有出现:没有方法,也没有这些方法的文档
  • 你不能记录一个动态创建的方法,它必须是静态定义的。 Yard 必须运行您的代码,然后使用自省来生成运行时可用的方法,这是不切实际的。

标签: ruby documentation yard


【解决方案1】:

如果将方法创建移到类方法中,则可以使用宏:

class Foo

  # @!macro [attach] generate
  #   @method $1_way
  #   The $1 way
  #   @return [String] the $1 way
  def self.generate(type)
    define_method("#{type}_way") do
    end
  end

  generate :one
  generate :two
  generate :three

end

场输出:

- (String) one_way

一种方式

返回:

(String) — 一种方式


- (String) three_way

三种方式

返回:

(String)——三种方式


- (String) two_way

两种方式

返回:

(String) — 两种方式

【讨论】:

  • 为什么它不能循环工作? [:one,:two,:three].each {|n| generate(n)}。是吗?
  • OK @Stefan 但是.. 在这种情况下,我们真的必须修改代码才能使用 YARD 吗?我的意思是可能不太可读的代码有利于自动文档生成......或者是否有其他方法来记录动态定义的方法?
  • @masciugo 我想这就是 YARD 的工作方式。此外,我认为循环内的方法定义可读性较差,但是 YMMV。
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 2011-04-13
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多