Forum: Ruby Indeterministic File order using Dir

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
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:
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.

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.

class Y < X

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
require 'x'
class Y < X

class X

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

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

Joseph Wilk
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.


a @
This topic is locked and can not be replied to.