Skip to content

Instantly share code, notes, and snippets.

@AJFaraday
Last active February 17, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AJFaraday/56162fc4fff8c596adce to your computer and use it in GitHub Desktop.
Save AJFaraday/56162fc4fff8c596adce to your computer and use it in GitHub Desktop.
# 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