A novice's question about load

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!

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

Thank you


Ayumu AIZAWA
twitter.com/ayumin

2009/12/7 Donghui O. [email protected]:

Evening Donghui,

On Sun, Dec 6, 2009 at 9:09 PM, Donghui O.
[email protected]wrote:

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

Hi,

Am Montag, 07. Dez 2009, 14:09:53 +0900 schrieb Donghui O.:

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

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