[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Indeterministic File order using Dir

Joseph Wilk

6/26/2008 4:33:00 PM

You cannot guarantee the order of files in the list returned from:
----
Dir['/tmp/*']
----
The order in which the files are loaded is not deterministic. I had an
example were a 32bit machine and a 64bit machine (both ubuntu-hardy)
where generating the files in a different order.

Some people use Dir as a shortcut to require files.

Example:
----
Dir[File.join(File.dirname(__FILE__), 'example/*.rb')].each{ |f| require
f }
----

I've come across this on a couple of open-source projects. This is
great until you start having dependencies that rely on the order of the
files being required.
----
example/x.rb
example/y.rb

y.rb->
class Y < X

x.rb->
class X
----

Its not safe to assume that just because on your machine it loads x.rb
first then y.rb that it will use the same order on other machines.

I try and ensure that dependencies are required in the files that need
them.
----
y.rb->
require 'x'
class Y < X

x.rb->
class X
----

I'm sure there are lots of other neat idioms that people use to require
files.

Anyone have any good ideas why a 32bit and 64bit machine would generate
a different order on Dir[]?

--
Joseph Wilk
http://www.joes...
--
Posted via http://www.ruby-....

1 Answer

ara.t.howard

6/26/2008 4:53:00 PM

0


On Jun 26, 2008, at 10:33 AM, Joseph Wilk wrote:

> Anyone have any good ideas why a 32bit and 64bit machine would
> generate
> a different order on Dir[]?

it's determined by the underlying call to glob (man 3 glob). in
general (under posix) the glob function sorts in ascending ASCII order
but this is not always going to be the same as alphabetical order so
any ruby code relying on this is bugged. if the underlying glob
function does not return the results in ASCII sorted order in 64 bit
mode then i think it's a non-posix behaviour and possible a bug.

in any case it's entirely a non-ruby issue.

cheers.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama