wmj*_*att 19 ruby oop inheritance scope instance-variables
我希望子类从其父级继承类级实例变量,但我似乎无法弄明白.基本上我正在寻找这样的功能:
class Alpha
class_instance_inheritable_accessor :foo #
@foo = [1, 2, 3]
end
class Beta < Alpha
@foo << 4
def self.bar
@foo
end
end
class Delta < Alpha
@foo << 5
def self.bar
@foo
end
end
class Gamma < Beta
@foo << 'a'
def self.bar
@foo
end
end
Run Code Online (Sandbox Code Playgroud)
然后我想要这样输出:
> Alpha.bar
# [1, 2, 3]
> Beta.bar
# [1, 2, 3, 4]
> Delta.bar
# [1, 2, 3, 5]
> Gamma.bar
# [1, 2, 3, 4, 'a']
Run Code Online (Sandbox Code Playgroud)
显然,这段代码不起作用.基本上我想为父类继承的类级实例变量定义一个默认值.对于子子类,子类中的更改将是默认值.我希望这一切都能在不改变影响其父母或兄弟姐妹的一个类的价值的情况下发生.Class_inheritable_accessor给出了我想要的行为......但是对于一个类变量.
我觉得我可能会问得太多.有任何想法吗?
Pet*_*own 12
Rails将它作为一个名为class_attribute的方法构建到框架中.您可以随时查看该方法的来源并制作您自己的版本或逐字复制.唯一需要注意的是,您不要更改可变项目.
Osc*_*ang 10
我在使用resque的项目中所做的是定义一个基础
class ResqueBase
def self.inherited base
base.instance_variable_set(:@queue, :queuename)
end
end
Run Code Online (Sandbox Code Playgroud)
在其他子作业中,默认情况下将设置队列实例.希望它可以提供帮助.
使用mixin:
module ClassLevelInheritableAttributes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def inheritable_attributes(*args)
@inheritable_attributes ||= [:inheritable_attributes]
@inheritable_attributes += args
args.each do |arg|
class_eval %(
class << self; attr_accessor :#{arg} end
)
end
@inheritable_attributes
end
def inherited(subclass)
@inheritable_attributes.each do |inheritable_attribute|
instance_var = "@#{inheritable_attribute}"
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
将这个模块包含在一个类中,给它两个类方法:inheritable_attributes和inherited.
继承的类方法与所示模块中的self.included方法的工作方式相同.每当包含此模块的类获得子类时,它就为每个声明的类级可继承实例变量(@inheritable_attributes)设置类级实例变量.