From: Francis Cianfrocca Date: 2006-08-09T04:37:20+09:00 Subject: Re: Global variables considered harmful? On 8/8/06, darren kirby wrote: > Hello all, > > I have a Ruby script here that acts like a regular Unix program, including > accepting a large number of command-line options/arguments. After processing > some are simple flags (ie: nil or 1) and some are text/numeric arguments. > Not all are required, and some must be initialized with a default value. I > have currently implemented them as globals, as their values are needed > anywhere in my script, from classes to functions to top-level flow control > code... > > Now I know you are not supposed to rely on globals if you can help it, so I am > trying to rewrite my script to do away with them. The problem is that there > are so many command-line switches that simply passing them to the > class/function(s) that need them would be unwieldy. > > So what do I do? I was thinking of creating and populating a new class that > would act similar to a C 'struct', but it seems to me that this is just a > different way to do the exact same thing as I have now, except that I would > have to qualify the variable with a class instance making my script even more > verbose than it is now...so why shouldn't we use globals again? Is it simply > the namespace issue or are there other reasons? > > Is one global hash better than 15 global scalars? > > I am not going to list my code here because it is 500+ lines but if you want > to see it let me know and I will post it to the web, > > Thanks for consideration, > -d > -- > darren kirby :: Part of the problem since 1976 :: http://badcomputer.org > "...the number of UNIX installations has grown to 10, with more expected..." > - Dennis Ritchie and Ken Thompson, June 1972 > > > Look at optparse for a better way to do command line arguments. Look at OpenStruct for a better way to pass them around your program.