require question

G

Gaston Garcia

------=_Part_8913_9258161.1124391415461
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hello to the ruby-talkers. It's my first week with ruby, I'm reading Why's=
=20
poingnant guide currently. I'm not a programmer, my only knowledge is a=20
little php I use to do web stuff and some actionScript cus I do some Flash.=
=20
I thought of learning to program and Ruby is what I've chosen (even though=
=20
Eric Raymond says to learn Python is what he thinks is best). Anyways I=20
don't care, Why's poignant guide is funny as hell, and that's it. I wanna b=
e=20
a part of it.

Now my question is (I'm assuming) a very simple one:

I'm trying to use the 'require' Kernel method.

I save a file as 'my_string.rb' and it contains just this:

my_string =3D 'hello world'

On another ruby file saved as 'my_string_reader.rb' I write:

require 'my_string'

puts 'my_string'

I thought that would output my 'hello world', but I get "undefined local=20
variable or method 'my_string' for main:Object (NameError)

I'm using windows xp. But the same problem happened on Ubuntu Linux. Same=
=20
error. It's the only example from Why's poignant guide that gives me errors=
=20
It's as if it's not reading the variables I've got stored in the required=
=20
file.

Thanks for your help.


--=20
-gaston

------=_Part_8913_9258161.1124391415461--
 
B

Brian Schröder

Hello to the ruby-talkers. It's my first week with ruby, I'm reading Why'= s
poingnant guide currently. I'm not a programmer, my only knowledge is a
little php I use to do web stuff and some actionScript cus I do some Flas= h.
I thought of learning to program and Ruby is what I've chosen (even thoug= h
Eric Raymond says to learn Python is what he thinks is best). Anyways I
don't care, Why's poignant guide is funny as hell, and that's it. I wanna= be
a part of it.
=20
Now my question is (I'm assuming) a very simple one:
=20
I'm trying to use the 'require' Kernel method.
=20
I save a file as 'my_string.rb' and it contains just this:
=20
my_string =3D 'hello world'
=20
On another ruby file saved as 'my_string_reader.rb' I write:
=20
require 'my_string'
=20
puts 'my_string'
=20
I thought that would output my 'hello world', but I get "undefined local
variable or method 'my_string' for main:Object (NameError)
=20
I'm using windows xp. But the same problem happened on Ubuntu Linux. Same
error. It's the only example from Why's poignant guide that gives me erro= rs.
It's as if it's not reading the variables I've got stored in the required
file.
=20
Thanks for your help.
=20
=20

Unlike with php ruby does not simple include the sourcefile at the
point of the require, but it takes extra care not to clobber any local
variables. Require calls load and in the description of load this is
mentioned:

# ri Kernel#load
------------------------------------------------------------ Kernel#load
load(filename, wrap=3Dfalse) =3D> true
------------------------------------------------------------------------
Loads and executes the Ruby program in the file _filename_. If the
filename does not resolve to an absolute path, the file is searched
for in the library directories listed in +$:+. If the optional
_wrap_ parameter is +true+, the loaded script will be executed
under an anonymous module, protecting the calling program's global
-> namespace. In no circumstance will any local variables in the <-
-> loaded file be propagated to the loading environment. <-

If you want to include a constant value from a file that is possible:

--- r.rb ---
require 'r1'

puts A
--- ---

--- r2.rb ---
A =3D 12
--- ---

ruby r.rb
=3D> 12

regards,

Brian
--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
W

why the lucky stiff

Gaston said:
On another ruby file saved as 'my_string_reader.rb' I write:

require 'my_string'
puts 'my_string'

I thought that would output my 'hello world', but I get "undefined local
variable or method 'my_string' for main:Object (NameError)
Gaston, welcome to Rubydom. We've proven that we can be funny, but now
you get to see how helpful we are.

This is an error in the book. You might want to read the newer version
at...

http://qa.poignantguide.net/

...for now. I'm working on a new design and the book has a bunch of
fixes on that site.

In the new version, you'll notice I'm storing stuff in the CODE_WORDS
constant rather than a code_words variable. Ordinary variables cannot
be loaded through a `require'. But constants can.

That's all. Nothing more.

_why
 
C

Charles Steinman

Gaston said:
Now my question is (I'm assuming) a very simple one:

I'm trying to use the 'require' Kernel method.

I save a file as 'my_string.rb' and it contains just this:

my_string = 'hello world'

On another ruby file saved as 'my_string_reader.rb' I write:

require 'my_string'

puts 'my_string'

I thought that would output my 'hello world', but I get "undefined local
variable or method 'my_string' for main:Object (NameError)

That's because require doesn't import local variables from a file. They
stay local to the file they are in. If you make the variable a
constant, global or instance variable, then it will work.
 
G

Gaston Garcia

------=_Part_9146_20104899.1124395961706
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thanks Brian and Why. I see why it wasn't working.

And thanks for the new URL of the poignant guide.

-gaston.

=20

Gaston, welcome to Rubydom. We've proven that we can be funny, but now
you get to see how helpful we are.
=20
This is an error in the book. You might want to read the newer version
at...
=20
http://qa.poignantguide.net/
=20
...for now. I'm working on a new design and the book has a bunch of
fixes on that site.
=20
In the new version, you'll notice I'm storing stuff in the CODE_WORDS
constant rather than a code_words variable. Ordinary variables cannot
be loaded through a `require'. But constants can.
=20
That's all. Nothing more.
=20
_why
=20
=20


--=20
-gaston

------=_Part_9146_20104899.1124395961706--
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top