Dir-s seem awkward as compared to File-s. Many of the methods are similar to IO methods, but a Dir doesn’t inherit from IO. For example, tell in the IO docs reads:
Returns the current offset (in bytes) of ios.
When read-ing and tell-ing through a normal Dir, I get large numbers like 346723732 and 422823816. I was originally expecting these integers to be more “array-like” and just be a simple range.
- Are these the bytes of the files contained in the
Dir? - If not, is there any meaning to the numbers returned like
IO#tell? - Also why do
Dir-s have anopenandclosefunction if they are not Streams? - Is it still just as important to close a
Diras a normalIO?
Any general explanation of how a Ruby Dir works would be appreciated.
update Another confusing part: if Dirs are not IOs, why does close raise an IOerror?
Closes the directory stream. Any further attempts to access dir will raise an IOError.
Also notice that in the documentation it considers it a “directory stream”. So this brings up the question again of are they streams or not and if not, why the naming convention?
So after some IRC chat here’s the conclusion I’ve come to:
The
Dirobject is NOT anIODirDoes not inherit from the IO class and is only readable. Still not sure why anIOErroris raised on#close.An opened
DirIS a stream howeverAlso if you check the source for
Dir#closeYou will see that it calls the C functiondirclose.man dircloseprints:…with
dirpbeing a param.So yes, instantiated
Dirs will open a stream and yes,Dirs will use a file descriptor and need to be closed if you do not want to rely on garbage collection.Big thanks to injekt and others on #ruby-lang irc!