[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

require bug?? (1.8.0

Andrew Walrond

12/2/2003 3:48:00 PM

Require is supposed to include each file only once, but if the same physical
file is accessed from different paths, it can get included multiple times. Is
this the correct behaviour?

Example 1

File a.rb
puts "a includes b"
require "b.rb"
puts "a includes c"
require "c.rb"

File b.rb
puts "b includes c"
require "c.rb"

File c.rb
puts "C INCLUDED"

$ ruby a.rb
a includes b
b includes c
C INCLUDED
a includes c

C included once.

Example 2:

Change file b.rb
puts "b includes c"
require "./c.rb"

$ ruby a.rb
a includes b
b includes c
C INCLUDED
a includes c
C INCLUDED

File c.rb is included twice.


6 Answers

ts

12/2/2003 3:56:00 PM

0

>>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:

A> Require is supposed to include each file only once, but if the same
A> physical file is accessed from different paths, it can get included
A> multiple times.

If you don't want this behaviour, how do you test that this is the same
physical file ?


Guy Decoux

Ceri Storey

12/2/2003 4:02:00 PM

0

On Wed, Dec 03, 2003 at 12:55:47AM +0900, ts wrote:
> If you don't want this behaviour, how do you test that this is the same
> physical file ?

If it's on a filesystem which will tell you the inode no. of a file,
(ie: mot unix systems) then you can compare that and the device number.
Beyond that, it'd be a case of finding the physical path to the file and
comparing that. Although it falls down when you have say, loopback
filesystems and SUBST'd drives.
--
Ceri Storey <cez@necrofish.org.uk>


ts

12/2/2003 4:09:00 PM

0

>>>>> "C" == Ceri Storey <cez@necrofish.org.uk> writes:

C> If it's on a filesystem which will tell you the inode no. of a file,
C> (ie: mot unix systems) then you can compare that and the device number.

Well, the problem here is that ruby run also on another systems (not only
un*x) and I'm not sure if it exist a portable way to retrieve inode and
device number.

C> Beyond that, it'd be a case of finding the physical path to the file and
C> comparing that. Although it falls down when you have say, loopback
C> filesystems and SUBST'd drives.

and you can have problems with links


Guy Decoux




Carl Youngblood

12/2/2003 4:10:00 PM

0

Ceri Storey wrote:
> On Wed, Dec 03, 2003 at 12:55:47AM +0900, ts wrote:
>
>> If you don't want this behaviour, how do you test that this is the same
>> physical file ?
>
>
> If it's on a filesystem which will tell you the inode no. of a file,
> (ie: mot unix systems) then you can compare that and the device number.
> Beyond that, it'd be a case of finding the physical path to the file and
> comparing that. Although it falls down when you have say, loopback
> filesystems and SUBST'd drives.

Getting the inode will work on most unix systems. My guess is that
getting the physical path will work on Windows (for now anyway. I've
heard that they are thinking of adding symbolic links in longhorn).

Carl

Ara.T.Howard

12/2/2003 5:16:00 PM

0

Andrew Walrond

12/2/2003 8:26:00 PM

0

On Tuesday 02 Dec 2003 7:47 pm, Ara.T.Howard wrote:
> c.rb
> $lib_c = true
> require 'a' unless defined? $lib_a
> require 'b' unless defined? $lib_b
> ...
>

Yes; similar to the usual c/c++ construct everybody uses..

#ifndef _A_H_
#define _A_H_
...
#endif

I assume that require's "only loading once" capability was designed to remove
the necessity for this clunky stuff, but the simple 'compare the filename'
implementation is such that it must be used with care.

Andrew Walrond