From: Ben Giddings Date: 2003-09-12T23:51:00+09:00 Subject: Re: Global object instance? Sean O'Dell wrote: > I'm not much of a purity person, although I do try and never use actual > $globals. Instead, I usually wrap EVERYTHING I code into a top-level > module which contains @variables that sort of act like globals. The > difference is when I interact with other code I've written that is not > part of the application's namespace, it doesn't have access to my main > namespace @variables. This is an anti-global discipline, I think. I'm often too much of a purity person, because of that, I've been using singletons instead of globals. You might like this solution so let me explain. I have a bunch of configuration parameters for something I wrote. There are 5 main files, each of which contains a class which encapsulates some behaviour. A 6th file is also around which can run any of the other 5 tasks in the correct order. There is some configuration data that different classes all use. What I do is I wrap all those variables in a "Config" object. The first time it is needed it is created, and its state is loaded from a config file. It is a singleton, so each class has in its constructor: @conf = Config.instance. If it didn't yet exist, it is created and initialized here, otherwise the existing singleton is simply returned. Then when I need a config setting I just use: "days = @conf.num_days". I don't use any globals, so I don't need to worry about naming clashes, and because of the way singletons work, I always know that my objects are all sharing the same data. > Gavin Sinclair wrote: >> The point is, really, that Kernel contains a bunch of useful methods, as >> well as some that are, frankly, a relic of the past (open, sub). They >> manage no state; they just do stuff. It's a convenient and harmless way >> of adding regular features to the language (e.g. loop, puts, throw, >> catch) >> while keeping the core language simple, as well as exposing necessary >> "kernel" services (local_variables, callcc, syscall). While we're on the subject, I'm curious how many people use the Kernel version of certain methods, rather than the associated object version. How many people use: chomp() vs String#chomp() chop() vs String#chop() gsub() vs String#gsub() open() vs IO#popen()/File#open() gets(), readline(), readlines() vs something using File scan() vs String#scan() select() vs IO.select() split() vs String#split() sub() vs String#sub() test() vs Filetest stuff If most people don't use these, I think it would be good to migrate them out of the language. There are many useful kernel methods that should stick around. I much prefer using puts to $stdout.puts(), but I *really* think the String stuff that works on $_ should go. Any thoughts? Ben