Ruby 中的多重继承是通过包含模块来模拟的,但是不可能直接从模块(不是类)继承属性。我想出的解决方案是在模块初始化时定义属性(下面的代码)。与下面的代码(继承方法和属性)相比,是否有更好的方法来实现多重继承?
module MyCustomMixin
attr_accessor :a
def initialize
self.a = 1
end
def b
"something"
end
end
class MyCreateView < CreateView
include MyCustomMixin
end
class MyReadView < ReadView
include MyCustomMixin
end
class MyUpdateView < UpdateView
include MyCustomMixin
end
class MyDeleteView < DeleteView
include MyCustomMixin
end