如何在 Ruby 中生成初始化程序? [英] How to generate initializer in Ruby?
本文介绍了如何在 Ruby 中生成初始化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是时候缩短它了:
class Foo
attr_accessor :a, :b, :c, :d, :e
def initialize(a, b, c, d, e)
@a = a
@b = b
@c = c
@d = d
@e = e
end
end
我们有 'attr_accessor' 来生成 getter 和 setter.
We have 'attr_accessor' to generate getters and setters.
我们有什么东西可以通过属性生成初始化器吗?
Do we have anything to generate initializers by attributes?
推荐答案
最简单:
Foo = Struct.new( :a, :b, :c )
生成访问器和初始化器.您可以通过以下方式进一步自定义您的课程:
Generates both accessors and initializer. You can further customize your class with:
Foo = Struct.new( … ) do
def some_method
…
end
end
这篇关于如何在 Ruby 中生成初始化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文