[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

require v load

Andrew Baine

5/18/2006 1:31:00 PM

Why would require work where load does not? See the output below:

irb(main):001:0> filename = 'lib/sudoku/cell'
=> "lib/sudoku/cell"
irb(main):002:0> load filename
LoadError: no such file to load -- lib/sudoku/cell
from (irb):2:in 'load'
from (irb):2
irb(main):003:0> require filename
=> true
irb(main):004:0> c = Sudoku::Cell.new
=> #<Sudoku::Cell:0x2c20258 @number = nil>

Thanks for any help from an eager Ruby convert


4 Answers

Robert Klemme

5/18/2006 1:33:00 PM

0

ANDREW BAINE wrote:
> Why would require work where load does not? See the output below:
>
> irb(main):001:0> filename = 'lib/sudoku/cell'
> => "lib/sudoku/cell"
> irb(main):002:0> load filename
> LoadError: no such file to load -- lib/sudoku/cell
> from (irb):2:in 'load'
> from (irb):2
> irb(main):003:0> require filename
> => true
> irb(main):004:0> c = Sudoku::Cell.new
> => #<Sudoku::Cell:0x2c20258 @number = nil>
>
> Thanks for any help from an eager Ruby convert

These are two completely different mechanisms: require loads a file only
once and it looks in the search path for libs. load only tries to
resolve the given filename (absolute or relative) and will load the file
as many times as it is invoked.

robert

Andrew Baine

5/18/2006 2:37:00 PM

0

Thanks, Robert.

Let me follow up, though. In my example,
> load filename
failed and then
> require filename
succeeded. I infer from your response that this means that filename is
found in the search path (by require) but not as an absolute or relative
path (by load). Does that sound right?

How can I find out what directories are included in the search path?


ts

5/18/2006 2:56:00 PM

0

>>>>> "A" == ANDREW BAINE <albaine@gmail.com> writes:

A> succeeded. I infer from your response that this means that filename is
A> found in the search path (by require) but not as an absolute or relative
A> path (by load). Does that sound right?

Well not exactly, here an example

moulon% pwd
/tmp
moulon%

moulon% ls net/telnet*
ls: net/telnet*: No such file or directory
moulon%

moulon% ruby -e 'load "net/telnet"'
-e:1:in `load': no such file to load -- net/telnet (LoadError)
from -e:1
moulon%

moulon% ruby -e 'load "net/telnet.rb"'
moulon%

You must give the complete filename (with the extension) with load

With require, you can just write

moulon% ruby -e 'require "net/telnet"'
moulon%

A> How can I find out what directories are included in the search path?

the global variable $LOAD_PATH


--

Guy Decoux

Andrew Baine

5/18/2006 4:18:00 PM

0

Case closed: I wasn't adding ".rb" to the end of the filename. Require
could find it but load couldn't. Thanks Guy and Robert.