Skip to content

Instantly share code, notes, and snippets.

@net

net/example.rb Secret

Created November 5, 2015 20:03
Show Gist options
  • Save net/f13fe33096d3aaa90fac to your computer and use it in GitHub Desktop.
Save net/f13fe33096d3aaa90fac to your computer and use it in GitHub Desktop.
# This has issues with inheritance because @@thing isn't idependant to each class
class Parent
@@thing = []
def self.thing
@@thing
end
end
class Child < Parent
end
Parent.thing << :a
Child.thing
# => :a
# This has issues with inheritance because @thing isn't set for child classes
class Parent
@thing = []
def self.thing
@thing
end
end
class Child < Parent
end
Parent.thing << :a
Child.thing
# => nil
# This one has no issues with inheritance
class Parent
def self.thing
@thing ||= []
end
end
class Child << Parent
end
Parent.thing << :a
Child.thing
# => []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment