Skip to content

Instantly share code, notes, and snippets.

@AJFaraday AJFaraday/foobar.rb
Last active Feb 17, 2016

Embed
What would you like to do?
# 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
You can’t perform that action at this time.