From: "Hal E. Fulton" Date: 2003-05-09T09:33:23+09:00 Subject: Re: Can a global be a constant? ----- Original Message ----- From: "Jim Freeze" To: "ruby-talk ML" Sent: Thursday, May 08, 2003 7:22 PM Subject: Re: Can a global be a constant? > On Friday, 9 May 2003 at 9:16:35 +0900, dblack@superlink.net wrote: > > Hi -- > > > > On Fri, 9 May 2003, Jim Freeze wrote: > > > > > On Friday, 9 May 2003 at 8:57:15 +0900, dblack@superlink.net wrote: > > > > > > > You can use instance variables (of the object State) instead of class > > > > variables, and then State becomes: > > > > > > > > class State > > > > class << self > > > > attr_accessor :debug, :verbose > > > > end > > > > end > > > > > > Why does this not define the class method State.debug to > > > set the instance variable @debug? > > > > It does; it creates #debug and #debug= in State's singleton class, and > > those methods get/set State's instance variable @debug. > > So, this is something I don't understand. State is never instantiated. > How does it have an instance variable? It's not an instance variable in State, it's an instance variable in the singleton class of State. Or have I confused myself again? :) Note that a class X *cannot* have a class method that references an instance variable of X. Think about it. A class-level method knows nothing about any particular instantiation of X, so it can't get/set instance-level data. But class X itself is an object, and you can add a singleton method to an object. When the object you add a singleton method to is a class, the method is then a class method. "Cogito cogito, ergo cogito sum." (I think I think, therefore I think I am.) Cheers, Hal