Skip to content

Instantly share code, notes, and snippets.

@btm
Created September 30, 2013 17:59
Show Gist options
  • Save btm/6767611 to your computer and use it in GitHub Desktop.
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
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
"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