Created
September 30, 2013 17:59
-
-
Save btm/6767611 to your computer and use it in GitHub Desktop.
redefining a class that sets a class variable will recreate the class variable
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
class A | |
@@cvar = {} | |
def mutate | |
@@cvar[:foo] = "bar" | |
end | |
def add(k,v) | |
@@cvar[k] = v | |
end | |
def show | |
p @@cvar | |
end | |
end | |
a = A.new | |
a.mutate | |
p "before:" | |
a.show | |
a.add("yellow","elephant") | |
a.show | |
class A | |
@@cvar = {} | |
def mutate | |
@@cvar[:baz] = "qux" | |
end | |
def show | |
p @@cvar | |
end | |
end | |
a = A.new | |
a.mutate | |
p "after:" | |
a.show |
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
"before:" | |
{:foo=>"bar"} | |
{:foo=>"bar", "yellow"=>"elephant"} | |
"after:" | |
{:baz=>"qux"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment