From: "Hal E. Fulton" Date: 2003-05-09T08:23:52+09:00 Subject: Re: Can a global be a constant? ----- Original Message ----- From: "Jim Freeze" To: "ruby-talk ML" Sent: Thursday, May 08, 2003 6:02 PM Subject: Re: Can a global be a constant? > > I've also noticed that we don't mind that a class is > > global, if you know what I mean. You could always > > encapsulate all your global data in a class called > > State (with class variables and accessors for them). > > > > Then you could pretend to be doing something clever > > when actually you're just using one big fancy global > > variable. :) > > Ok, so how would you > access the state object in the code below without > making it a global as I did? > > class App > State = ::Struct.new(:debug, :verbose) > $state = State.new(true, false) > def main > Work.new > end > end > > class Work > def initialize > puts $state.debug > end > end > > App.new.main Not a stupid question... maybe my assumptions don't work for you, but what I was thinking was something like: class State @@debug = nil @@verbose = nil def State.debug @@debug end def State.debug=(val) @@debug = val end def State.verbose @@verbose end def State.verbose=(val) @@verbose = val end class App def main State.debug = true State.verbose = false Work.new end end class Work def initialize puts State.debug end end App.new.main Hal