From: Chad Perrin Date: 2010-02-20T03:57:10+09:00 Subject: Re: Global variables usage --jRHKVT23PllUwdXP Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 19, 2010 at 09:00:47PM +0900, Robert Klemme wrote: > 2010/2/19 di vi : > > botp wrote: > >> On Fri, Feb 19, 2010 at 3:02 PM, di vi wrote: > >>> > >>> 2) to print the global variable (gprint.rb) > >>> > >>> Code: > >>> =EF=BF=BD =EF=BF=BDloop do > >>> =EF=BF=BD =EF=BF=BDputs $foo > >>> =EF=BF=BD =EF=BF=BDend > >> > >> where in this code (gprint.rb) does it say that you are accessing the > >> file var.rb ? > >> read on require and load... > >> > >> best regards -botp > > > > Load and require execute the complete program..I want only the value of > > global variables to change in the respective .rb file. >=20 > Well, there's an obvious solution to that: place the global variable > in a file on its own which you load from all places that need it. > However, I am not sure that this is generally a good idea. What do > you need that for? =2E . . or edit the file you're loading/requiring so that it will only conditionally execute code in the file if the file is executed directly, and not if it is called as a library using load or require. For instance: > cat included_file.rb #!/usr/bin/env ruby $foo =3D 'foo' $bar =3D 'bar' if $0 =3D=3D __FILE__ # code to be executed if file # called directly for execution end > cat including_file.rb #!/usr/bin/env ruby load 'included_file.rb' if $0 =3D __FILE__ # do stuff with $foo # do stuff with $bar end Apologies if I'm off-base -- I haven't read the whole thread in detail, because I just rejoined the list after a hiatus. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --jRHKVT23PllUwdXP Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAkt+1n8ACgkQ9mn/Pj01uKVhKgCgnK35hwe3krWKcvgykGdpKC37 vhgAoLQFHC0PUj28oGFpmD1F+SRs2sB4 =Vr8T -----END PGP SIGNATURE----- --jRHKVT23PllUwdXP--