Ruby:是否有带有 attr_initialize 方法的 gem?
是否有一些流行的 ruby gem 定义了 attr_initialize 类方法,其行为如下:
attr_initialize :attr1, :@attr2, [:@attr3, 1] do |arg4|
post_initialization(arg4)
end
将评估为相当于:
attr_accessor :attr1
def initialize(attr1, attr2, attr3 = 1, arg4 = nil)
@attr1 = attr1
@attr2 = attr2
@attr3 = attr3
post_initialization(arg4)
end
Is there some popular ruby gem that defines attr_initialize class method, which behaves like following:
attr_initialize :attr1, :@attr2, [:@attr3, 1] do |arg4|
post_initialization(arg4)
end
would eval to something equivalent to:
attr_accessor :attr1
def initialize(attr1, attr2, attr3 = 1, arg4 = nil)
@attr1 = attr1
@attr2 = attr2
@attr3 = attr3
post_initialization(arg4)
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们的 attr_extras 很接近。
它故意不处理默认参数、可选参数或更多的赋值——如果你想要这样,你可能应该只定义一个常规方法。
Our attr_extras is close.
It intentionally doesn't handle default arguments, optional arguments or more than assignment – if you want that, you should probably just define a regular method.
你不需要那个。只需编写如下内容:
对我来说,它看起来不像太多代码。
You don't need that. Just write something like:
To me, it doesn't look like too much code.