using "require" in irb, where did my variables go!?

T

Tim Apple

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!
 
M

Marcin Wolski

Tim said:
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!

You could define x as a global variable $x e.g.:
#in my_vars.rb
$x = 5
puts "your variable x is #{$x}"
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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!
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__)
 
F

Florian Gilcher

Hello,
=20
I have a somewhat basic question: lets say that in my working = directory
I have a file called my_vars.rb.
=20
It contains:
x =3D 5.0
puts "your variable x is #{x}"
=20
Now, I fire up irb in my command window, and type in
require "my_vars.rb"
=20
Which outputs, as expected,
your variable x is 5.0
=3D> true
=20
Now, I want to access that variable again, so I enter in:
puts x
=20
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?
=20
Thanks!

There is a way around this, although I think that the "usual" solutions =
(wrapping this data into a proper object or method) are much better.

All code in Ruby is evaluated inside the context of an object. In other =
terms: self is always defined. Code outside of class statements, module =
statements and some special statements that change the binding is =
evaluated inside the toplevel object (called "main"). "main" is just a =
normal object and we can thus set instance variables:

File: test.rb
@b =3D 42
=20
File: inc.rb
require "test"
puts @b #=3D> prints 42

Those instance variables can only be accessed in the toplevel context =
and not in any other object or class.

I don't think this is a good way to do it, but it is a nice way of =
illustrating how far "everything is an object" is rooted into ruby.

Gru=DF,
Skade

=20=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top