From: Josh Cheek Date: 2010-06-01T19:30:03+09:00 Subject: Re: using "require" in irb, where did my variables go!? --0016e6d9a1162131c60487f5743d Content-Type: text/plain; charset=ISO-8859-1 On Mon, May 31, 2010 at 9:23 PM, Tim Apple wrote: > Hello, > > I have a somewhat basic question: lets say that in my working directory > I have a file called my_vars.rb. > > It contains: > x = 5.0 > puts "your variable x is #{x}" > > Now, I fire up irb in my command window, and type in > require "my_vars.rb" > > Which outputs, as expected, > your variable x is 5.0 > => true > > Now, I want to access that variable again, so I enter in: > puts x > > and I get an error saying that x is undefined. It's like it was > temporarily there in memory, but then once it was used, it got thrown > away! Does anyone know how to load in a file and keep using the > variables that were in said file? > > Thanks! > -- > Posted via http://www.ruby-forum.com/. > > Using globals to allow locals to talk across files seems pretty shaky. Why do you need to be able to do this? Probably there is a better way. Perhaps put all that code into one file. Perhaps put it into a method, and have it return the variables of interest (you can conditionally invoke the method if the file is required vs if the file is the primary program by comparing $0 to __FILE__) --0016e6d9a1162131c60487f5743d--