Yard doc and `define_method`

让人想犯罪 __ 提交于 2019-12-23 08:04:32

问题


Is there a way to comment methods defined with define_method in YardDoc?

I tried this:

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

But, unfortunately, not working.


回答1:


If you move the method creation into a class method, you could use a macro:

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

YARD Output:

- (String) one_way

The one way

Returns:

(String) — the one way


- (String) three_way

The three way

Returns:

(String) — the three way


- (String) two_way

The two way

Returns:

(String) — the two way



来源:https://stackoverflow.com/questions/28413074/yard-doc-and-define-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!