From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2001-05-11T04:10:09+09:00 Subject: [ruby-talk:14957] load and local variable scope (and constants too) if I have a file that contains: #####start of x.rb ##### suffix = "v" ####end of x.rb ###### and another file loads that file and tries to use the variable suffix: #######start of y.rb ##### load "x.rb" puts "suffix is: #{suffix}" ######end of y.rb ##### and then run y.rb, the output is: y.rb:2: undefined local variable or method `suffix' for # (NameError) So that tells me that local variables have file scope and that doesn't change when the file defining the local is loaded (ie. I can't access a local variable defined in another file even if that file is loaded). Makes sense. But, if I have: #####start of x.rb ##### SUFFIX = "v" ####end of x.rb ###### and y.rb has: #######start of y.rb ##### load "x.rb" puts "suffix is: #{SUFFIX}" ######end of y.rb ##### and then run y.rb, I get: suffix is: v So it seems like locally defined constants have a different scope than locally defined variables - constants can transcend the file scoping if the file they are defined in is load'ed or require'd. In the next case I declare suffix as global with '$': ##### start of x.rb ###### $suffix = "v" ##### end of x.rb ##### #### start of y.rb ##### load "x.rb" puts "suffix is: #{$suffix}" #### end of y.rb ##### and when y.rb is run, as expected it works (because $suffix is global) So, am I right in concluding that in regard to file scoping, constants look more like globals? I suppose this makes sense since if you try to redefine a constant you'll get a warning which will indicate that perhaps you have another constant by the same name in a differnt file. Phil