【发布时间】:2016-06-17 23:54:33
【问题描述】:
我正在寻找在initialize 方法中定义实例变量的更短的方法:
class MyClass
attr_accessor :foo, :bar, :baz, :qux
# Typing same stuff all the time is boring
def initialize(foo, bar, baz, qux)
@foo, @bar, @baz, @qux = foo, bar, baz, qux
end
end
Ruby 是否有任何内置功能可以避免这种笨拙的工作?
# e. g.
class MyClass
attr_accessor :foo, :bar, :baz, :qux
# Typing same stuff all the time is boring
def initialize(foo, bar, baz, qux)
# Leveraging built-in language feature
# instance variables are defined automatically
end
end
【问题讨论】: