From: Lionel Thiry Date: 2005-03-17T00:19:56+09:00 Subject: Re: [ruby2] will '@@' disapear in ruby2? Csaba Henk wrote: > On 2005-03-15, Lionel Thiry wrote: > >>>Anyway, the short answer as far as I know is "no" :-) >> >>Too bad! :) > > > I do like having them. In fact, they are globals, wrapped in the > namespace of a class/module. There are situations when you need such a > beast, and it's better than an explicit global... and better than using a > container as a constant (just because constans don't like being > reassigned), that's really ugly. There's still class instance variable. class MyClass @a = "class instance value" attr_reader :a def initialize(value) @a = value end def a_of_the_class self.class.a end class << self attr_reader :a end end my_instance = MyClass.new("instance value") puts my_instance.a # output: instance value puts my_instance.a_of_the_class # output: class instance value other_instance = MyClass.new("other instance value") puts other_instance.a # output: other instance value puts other_instance.a_of_the_class # output: class instance value I prefer that way of doing things over the @@ way. > > My problem is that you can't really access them from the outside, unless > accessor method are set up. By "from the outside" I mean that from code > which is not in the scope of a "class/module Foo ... end" thingy. Does > anyone know, eg, how to define a class variable in a nameless class? With class instance variable, this is easier, I suppose, as you can write: class << self attr_reader :a end -- Lionel Thiry