3
我正在寻找更短的方式来定义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
红宝石是否有任何建造功能,它可以让以避免这种猴子的工作?
# 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
用这种方法定义类有什么好处? –
你的意思是哪条路? –
我的意思是你在你的答案中使用的方式。 –