Last active
December 18, 2015 04:09
-
-
Save sxross/5723831 to your computer and use it in GitHub Desktop.
Demonstrates inheritance of singleton vars (@@vars)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MotionModel | |
module Model | |
def self.included(base) | |
base.extend(PublicClassMethods) | |
end | |
module PublicClassMethods | |
def inherited(subclass) | |
p "inherited from #{subclass}" | |
end | |
end | |
end | |
end | |
class MyModel | |
include MotionModel::Model | |
@@attribute = {model: "MyModel", other_stuff: {name: 'whatever'}} | |
def attr | |
p "attribute: #{@@attribute}" | |
end | |
end | |
my_model = MyModel.new | |
my_model.attr | |
class MySubclass < MyModel | |
@@attribute = {model: "MySubclass", other_stuff: {name: 'something else'}} | |
end | |
my_model = MyModel.new | |
my_model.attr | |
my_subclass = MySubclass.new | |
my_model.attr | |
my_subclass.attr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example using singleton instance variables which are clone when inherited.