require problem

E

eoghan

Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'

rabbit.rb
---------
require 'foo'

print my_string

eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)

Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies?
http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
 
J

Justin Collins

If you mean your are requiring 'mouse' in rabbit, then my_string would
be considered a variable local to 'mouse'

-Justin
 
B

Brian Schröder

Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string =3D 'blah'

rabbit.rb
---------
require 'foo'

print my_string

eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)

Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies?
http://www.rubycentral.com/book/irb.html
Thanks
Eoghan

Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.

mouse.rb
---
module AnimalConstants
MOUSE_NAME =3D "Mickey Mouse"
end

rabbit.rb
---s
require 'mouse'

p AnimalConstants::MOUSE_NAME

Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)

regards,

Brian
 
E

eoghan

Brian said:
=20
Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.
=20
mouse.rb
---
module AnimalConstants
MOUSE_NAME =3D "Mickey Mouse"
end
=20
rabbit.rb
---s
require 'mouse'
=20
p AnimalConstants::MOUSE_NAME
=20
Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)
=20
regards,
=20
Brian

Thanks I understand what you are saying... however consider the=20
following example from whys guide (and maybe im missing something here=20
again...)
http://www.poignantguide.net/ruby/chapter-4.html#section3

wordlist.rb
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
code_words =3D {
'starmonkeys' =3D> 'Phil and Pete, those prickly chancellors of the=20
New Reich',
'catapult' =3D> 'chucky go-go', 'firebomb' =3D> 'Heat-Assisted Living=
',
'Nigeria' =3D> "Ny and Jerry's Dry Cleaning (with Donuts)",
'Put the kabosh on' =3D> 'Put the cable box on'
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D

i assume this should be saved to a file too...?
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
require 'wordlist'

# Get evil idea and swap in code words
print "Enter your new idea: "
idea =3D gets
code_words.each do |real, code|
idea.gsub!( real, code )
end

# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name =3D gets.strip
File::eek:pen( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end

This doesnt work either. For me at least. Any ideas?
Eoghan
 
B

Brian Schröder

Thanks I understand what you are saying... however consider the
following example from whys guide (and maybe im missing something here
again...)
http://www.poignantguide.net/ruby/chapter-4.html#section3

wordlist.rb
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
code_words =3D {
'starmonkeys' =3D> 'Phil and Pete, those prickly chancellors of the
New Reich',
'catapult' =3D> 'chucky go-go', 'firebomb' =3D> 'Heat-Assisted Living= ',
'Nigeria' =3D> "Ny and Jerry's Dry Cleaning (with Donuts)",
'Put the kabosh on' =3D> 'Put the cable box on'
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

i assume this should be saved to a file too...?
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
require 'wordlist'

# Get evil idea and swap in code words
print "Enter your new idea: "
idea =3D gets
code_words.each do |real, code|
idea.gsub!( real, code )
end

# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name =3D gets.strip
File::eek:pen( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end

This doesnt work either. For me at least. Any ideas?
Eoghan

Hello Eoghan,

this is a known bug in _why's tutorial. It would be a good idea if it
were fixed in the tutorial, as this question comes up from time to
time on this mailing list. Change code_words to CODE_WORDS and it
works.

regards,

Brian
 
E

eoghan

Brian said:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
Hello Eoghan,
=20
this is a known bug in _why's tutorial. It would be a good idea if it
were fixed in the tutorial, as this question comes up from time to
time on this mailing list. Change code_words to CODE_WORDS and it
works.
=20
regards,
=20
Brian
=20

Thanks Brian.
 
J

Justin Collins

It might be instructive to point out that the reason changing code_words=20
to CODE_WORDS works is because variables beginning with a capital letter=20
are constants and constants are accessible when you do a require.

-Justin Collins
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top