gpt4 book ai didi

ruby - 堆场文档和 `define_method`

转载 作者:数据小太阳 更新时间:2023-10-29 06:59:02 25 4
gpt4 key购买 nike

有没有办法在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

但是,不幸的是,没有工作。

最佳答案

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

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

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

关于ruby - 堆场文档和 `define_method`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com