From: Bertram Scharpf Date: 2009-07-04T23:34:10+09:00 Subject: Re: How to remove/delete/destroy class variable in Ruby 1.9? Hi, Am Samstag, 04. Jul 2009, 16:30:05 +0900 schrieb dzw!: > Ok, code below shows better what I am talking about, I understand that > @@var is acting like global variable. > But how to get rid of it when is not needed anymore? > > class A > @@var = 'var in A' > class < def item ; @@var ; end > def item= var ; @@var = var ; end > end > end > class B > @@var = 'var in B' > class < def item ; @@var ; end > def item= var ; @@var = var ; end > end > end > > > A.item #=> "var in A" > B.item #=> "var in B" > > A.item='test' > A.item #=> "test" > B.item #=> "var in B" > > @@var ='ltl fkr' #here is the funny part > A.item #=> "ltl fkr" > B.item #=> "ltl fkr" > > A.item='test' #and here is getting even funnier > A.item #=> "test" > B.item #=> "test" # i wish it to stay 'ltl fkr' I cannot reproduce it here with Ruby 1.8.6. Are you using 1.9? I guess A's and B's @@var gets superseded by Object's @@var. Try to check these calls: Object.class_variables class Object ; remove_class_variable "@@var" ; end Object.instance_eval { remove_class_variable "@@var" } (Object.methods - Object.instance_methods).grep /var/ #=> ["class_variables", "class_variable_defined?"] (Object.private_methods - Object.private_instance_methods).grep /var/ #=> ["remove_class_variable", "class_variable_get", "class_variable_set"] Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de