Indeterministic File order using Dir

J

Joseph Wilk

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[]?
 
A

ara.t.howard

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top