From: dblack@... Date: 2003-05-09T09:32:57+09:00 Subject: Re: Can a global be a constant? Hi -- On Fri, 9 May 2003, Jim Freeze wrote: > 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 *itself* has an instance variable. State is an instance (of Class), so it can have instance variables. Defining these methods in State's singleton class is actually a variation of defining them generally for all Class objects. If one did this: class Class attr_accessor :debug end then every Class object would have that method: class String self.debug = true puts debug # true end (Note that instance *of* String don't have the methods; String *itself* has them, in its capacity as an instance of Class, to which the methods have been added.) The way I did it was a variant which put the accessor methods only in State's singleton class (since other Class objects don't need those methods). David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav