A novice's question about load

D

Donghui Ouyang

Hello All

I have two source files in the same directory: a.rb and b.rb

The content of a.rb is as follow:

puts "Hello form a.rb"

and b.rb is

load 'a'
puts "Hello from b.rb"
load 'a'
puts 'Hello again from b.rb'

When I run the b.rb, It cause a LoadError: no such file to load
But if I replace 'a' with 'a.rb' in b.rb, the program run successfully.
It really confuse me.

My ruby version: ruby 1.8.6

Thanks in advance for your help!
 
A

Ayumu Aizawa

Kernel.load doesn't fill in gap of .rb or .so.
Its spec of ruby.

Thank you
 
J

John W Higgins

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

Evening Donghui,

Hello All

I have two source files in the same directory: a.rb and b.rb

The content of a.rb is as follow:

puts "Hello form a.rb"

and b.rb is

load 'a'
puts "Hello from b.rb"
load 'a'
puts 'Hello again from b.rb'

When I run the b.rb, It cause a LoadError: no such file to load
But if I replace 'a' with 'a.rb' in b.rb, the program run successfully.
It really confuse me.
I assume your confusion is why when you use require the .rb is not needed
while for loaded it is. Require is for pulling in libraries and thus the
parameter to require is the library name which ruby then takes and attempts
to find by adding .rb, or a library extension (.so on linux, .dll on windows
and such). Load on the other hand is specifically for the loading of files
and therefore a full filename is needed to be passed.

You'll also find that while require will only load a library once - the load
command will load a file as many times as it is called.

Hope things help clear things up

John
 
B

Bertram Scharpf

Hi,

Am Montag, 07. Dez 2009, 14:09:53 +0900 schrieb Donghui Ouyang:
I have two source files in the same directory: a.rb and b.rb

---- a.rb ----
puts "Hello form a.rb"

---- b.rb ----
load 'a'
puts "Hello from b.rb"
load 'a'
puts 'Hello again from b.rb'

When I run the b.rb, It cause a LoadError: no such file to load
But if I replace 'a' with 'a.rb' in b.rb, the program run successfully.

The most has been said already.

- "require" loads once, "load" everytime.
- only "require" will try to add ".rb", ".so", etc.
- both "require" and "load" will search the "$:" path.

"$:" is equivalent to "$LOAD_PATH".

Bertram
 
D

Donghui Ouyang

Thanks a lot, your answers are really helpful to me.
And John, I'm most grateful to you for your help.
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top