在Ruby中,我经常发现自己写如下:成语对象创建
class Foo
def initialize(bar, baz)
@bar = bar
@baz = baz
end
<< more stuff >>
end
甚至
class Foo
attr_accessor :bar, :baz
def initialize(bar, baz)
@bar = bar
@baz = baz
end
<< more stuff >>
end
我总是热衷于尽可能减少样板 - 所以是在Ruby中创建对象有更习惯的方式吗?
另见:http:// stacko veroo.com/questions/12763016/how-to-cleanly-initialize-attributes-in-ruby-with-new – 2014-01-07 23:49:09