Inherit class-level instance variables in Ruby?
Rails has this built into the framework as a method called class_attribute. You could always check out the source for that method and make your own version or copy it verbatim. The only thing to watch out for is that you don't change the mutable items in place.
What I did in my project for using resque is to define a base
class ResqueBase def self.inherited base base.instance_variable_set(:@queue, :queuename) endend
In the other child jobs, the queue instance will be set by default. Hope it can help.
Use a 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 endend
Including this module in a class, gives it two class methods: inheritable_attributes and inherited.
The inherited class method works the same as the self.included method in the module shown. Whenever a class that includes this module gets subclassed, it sets a class level instance variable for each of declared class level inheritable instance variables (@inheritable_attributes).