Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save floriandejonckheere/1c0315d3998e684fc433 to your computer and use it in GitHub Desktop.
Save floriandejonckheere/1c0315d3998e684fc433 to your computer and use it in GitHub Desktop.
Ruby snippets
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment