Dir.entries returns [] on the second call

J

Joel VanderWerf

Is this normal?

irb(main):027:0> d = Dir.new '/'
=> #<Dir:0xb7dd5c54>
irb(main):028:0> d.entries
=> [".", "..", "lost+found", "etc", "media", "cdrom", "var", "usr",
"bin", "boot", "dev", "home", "lib", "mnt", "proc", "root", "sbin",
"tmp", "sys", "srv", "opt", "initrd", ".dev", "initrd.img", "vmlinuz",
"win", "ibm", "initrd.img.old", "vmlinuz.old"]
irb(main):029:0> d.entries
=> []
irb(main):030:0> RUBY_VERSION
=> "1.8.2"
irb(main):031:0> RUBY_RELEASE_DATE
=> "2004-12-25"
 
S

Stefan Lang

Is this normal?

irb(main):027:0> d = Dir.new '/'
=> #<Dir:0xb7dd5c54>
irb(main):028:0> d.entries
=> [".", "..", "lost+found", "etc", "media", "cdrom", "var", "usr",
"bin", "boot", "dev", "home", "lib", "mnt", "proc", "root", "sbin",
"tmp", "sys", "srv", "opt", "initrd", ".dev", "initrd.img", "vmlinuz",
"win", "ibm", "initrd.img.old", "vmlinuz.old"]
irb(main):029:0> d.entries
=> []
irb(main):030:0> RUBY_VERSION
=> "1.8.2"
irb(main):031:0> RUBY_RELEASE_DATE
=> "2004-12-25"

Call <tt>dir.rewind</tt> before <tt>dir.entries</tt> and it
works as expected.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Dir.entries returns [] on the second call"

|Stefan Lang wrote:
|> Call <tt>dir.rewind</tt> before <tt>dir.entries</tt> and it
|> works as expected.
|
|Thanks! I knew there was a reasonable explanation.

Maybe reasonable. But still not intuitive.

How about rewinding before "each" method, that make "entries" (and
other iterating methods) require no rewind?

matz.
 
J

Joel VanderWerf

Yukihiro said:
Hi,

In message "Re: Dir.entries returns [] on the second call"

|Stefan Lang wrote:
|> Call <tt>dir.rewind</tt> before <tt>dir.entries</tt> and it
|> works as expected.
|
|Thanks! I knew there was a reasonable explanation.

Maybe reasonable. But still not intuitive.

How about rewinding before "each" method, that make "entries" (and
other iterating methods) require no rewind?

matz.

I dunno. Shouldn't Dir#each behave like File#each? Would it make sense
for File#each to rewind? That seems a pretty radical change.

Maybe I should have been using a higher-level class, like FileList from
rake, anyway.
 
M

Mark Hubbart

Yukihiro said:
Hi,

In message "Re: Dir.entries returns [] on the second call"
=20
I dunno. Shouldn't Dir#each behave like File#each? Would it make sense
for File#each to rewind? That seems a pretty radical change.

I think File#each inherits that behavior from IO, which Dir doesn't
inherit from. IO's might not be rewindable, but their behavior should
be mostly interchangeable.

Dir.each, to me, seems more like a range or array-related method than
an IO related method; if it is, it perhaps should rewind before #each
use.

cheers,
Mark
 
J

Joel VanderWerf

Mark said:
Yukihiro said:
Hi,

In message "Re: Dir.entries returns [] on the second call"

|Stefan Lang wrote:
|> Call <tt>dir.rewind</tt> before <tt>dir.entries</tt> and it
|> works as expected.
|
|Thanks! I knew there was a reasonable explanation.

Maybe reasonable. But still not intuitive.

How about rewinding before "each" method, that make "entries" (and
other iterating methods) require no rewind?

matz.

I dunno. Shouldn't Dir#each behave like File#each? Would it make sense
for File#each to rewind? That seems a pretty radical change.


I think File#each inherits that behavior from IO, which Dir doesn't
inherit from. IO's might not be rewindable, but their behavior should
be mostly interchangeable.

Dir.each, to me, seems more like a range or array-related method than
an IO related method; if it is, it perhaps should rewind before #each
use.

The analogy I think of is: Dir is to opendir(), readdir() as File is to
open(), read().

Dir is like File in other ways. There is a fd involved, there is
Dir#seek, etc.

OTOH, I will probably make this same mistake again (and again...). It
might be better if FileList (or something like it) were part of the core.
 
B

Brian Schröder

Hi,
=20
In message "Re: Dir.entries returns [] on the second call"
=20
|Stefan Lang wrote:
|> Call <tt>dir.rewind</tt> before <tt>dir.entries</tt> and it
|> works as expected.
|
|Thanks! I knew there was a reasonable explanation.
=20
Maybe reasonable. But still not intuitive.
=20
How about rewinding before "each" method, that make "entries" (and
other iterating methods) require no rewind?
=20
matz.
=20
=20

It seems to me that the choice of the method name is the problem here.
It maybe would be better if there was a method
+fetch_remaining_entries+ or simply +fetch_remaining+ that returned
the remaining entries. IMHO the name Dir.entries suggests that it is
an accessor of Dir, and does not imply any change to the state of the
dir object.

best regards,

Brian


--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 

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

No members online now.

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,421
Latest member
DoreenCorn

Latest Threads

Top