From: "Shot (Piotr Szotkowski)" Date: 2008-06-26T22:34:37+09:00 Subject: Re: Is it ellegant to use a global variable to store a Logger object? --Pd0ReVV5GZGQvF3a Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I=C3=B1aki Baz Castillo: > Hi, I use Logger class in a programm and since I need to log in lot > of different places (into classes, into methods...) I use a global > variable in this way: > $log =3D Logger.new > $log.debug ... > so I can use $log anywhere in the code. > Is it ellegant and "the Ruby way"? One more elegant (IMHO) opinion, assuming you have a common namespace (module) for your program, is to create a singleton with the below approach: module MyProgram class Logger class << self def debug =E2=80=A6 =E2=80=A6 end end end end This way the global namespace is not cluttered, there are no conflicts with other programs=E2=80=99 Logger classes/objects, while anywhere in your program (i.e., anywhere in module MyProgram) you can simply call Logger.debug =E2=80=A6 which is the clearest syntax to my eyes. I have created a Config class with this approach and it=E2=80=99s a joy to use: do_something if Config.some_flag? case Config.setting when :value_a then do_a when :value_b then do_b when :value_c then do_c end (I=E2=80=99ve yet to hook Trollop to my Config class so that the command-li= ne options are nicely parsed and taken into account, but it=E2=80=99s on my to= do.) -- Shot --=20 Only Irish coffee provides in a single glass all four essential food groups: alcohol, caffeine, sugar, and fat. -- Alex Levine --Pd0ReVV5GZGQvF3a Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFIY5cKi/mCfdEo8UoRAuimAKDFpiriUyw+ou5wC6QmXyFUJm52pwCfZ4BT XoKHNnteBgFMcAhlFMQFPy4= =K/QY -----END PGP SIGNATURE----- --Pd0ReVV5GZGQvF3a--