Last active
February 17, 2016 16:51
-
-
Save AJFaraday/56162fc4fff8c596adce to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# I'm extending two classes with the same module which refers to a | |
# class variable. The trouble is that both of them seem to share the | |
# same variable, instead of having one for each class. | |
# | |
# How do I give them one each? | |
# | |
# (using Ruby 2.2.2) | |
module Widgets | |
def widgets | |
@@widgets ||= {} | |
end | |
def add_widget(name, widget) | |
self.widgets[name] = widget | |
end | |
end | |
class Foo | |
extend Widgets | |
end | |
class Bar | |
extend Widgets | |
end | |
Foo.add_widget('foo', :bar) | |
# How do I stop this happening? | |
Bar.widgets['foo'] | |
# => :bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment