Forum: Ruby Indeterministic File order using Dir

F68f69615423aa3851bd445409754dbf?d=identicon&s=25 Joseph Wilk (joesniff)
on 2008-06-26 18:35
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.joesniff.co.uk
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2008-06-26 18:56
(Received via mailing list)
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://codeforpeople.com/
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.