From: Kent Dahl Date: 2002-10-12T01:37:11+09:00 Subject: Re: Different Behavior of Class Variables w.r.t ||= William Djaja Tjokroaminata wrote: > Can you elaborate more on what sort of bugs is likely to occur? (I.e., > under what situation does "@@a ||= value" create a danger of creating the > class variable at the wrong level?) I am no expert at this stuff, but I ran into something like this when I used the "unless defined?" way. A quick example: #================================= class Base def show puts @@a end end class Derived < Base @@a = 2 unless defined? @@a def show_derived puts @@a end end class Base @@a = 1 unless defined? @@a end Base.new.show #=> 1 Derived.new.show #=> 1 Derived.new.show_derived #=> 2 #================================= > And if it is a problem with class > variables, why is it not a problem with instance variables? Because instance variables simply belong to the current object. Class variables has a whole nother dimension to get lost in; the class hierarchy. A class variable is visible only to the class it was defined in and below, IIRC. Defining a class variable above in the hierarchy creates a new one. (Such as shown in the example above) The short version then is that the ||= is so short and inconspicous that it would help to hide such bugs from you. Using "unless defined?" is more verbose, fitting to the amount of headache it can induce. (In my case, it was while reloading libraries using "load" in a program I was editing while it was running.) The earlier threads on class variables versus class instance variables might shine more light on the subject. -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 5. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)