Dir.entries

B

Brad Tilley

Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?
 
C

ChrisH

Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?

Dir.entries(Dir.pwd).delete_if{|f| ['.','..'].include?(f)}
 
C

ChrisH

Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?

Further reading shows that Dir::[] (aka Dir::glob) will list
files/directories and not include ',' or '..'
 
R

rtilley

ChrisH said:
Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?


Further reading shows that Dir::[] (aka Dir::glob) will list
files/directories and not include ',' or '..'


Thank you... I found that Dir.glob('*') works exactly like I want.
 
B

baumanj

If you're working on unix-based systems*, I'd definitely recommend
using the Pathname class. It provides the children method that does
what you're looking for more cleanly than Dir.glob('*') and provides
lots of other conveniences.

* Basically anything but Windows, Pathname should have Windows support
in Ruby 2.0
 
R

rtilley

If you're working on unix-based systems*, I'd definitely recommend
using the Pathname class. It provides the children method that does
what you're looking for more cleanly than Dir.glob('*') and provides
lots of other conveniences.

* Basically anything but Windows, Pathname should have Windows support
in Ruby 2.0

Thanks... I'm using Windows... glob seems to do this OK.
 
F

Florian Assmann

ChrisH said:
Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?

Dir.entries(Dir.pwd).delete_if{|f| ['.','..'].include?(f)}

Dir.entries( Dir.pwd ) - [ '.', '..' ] looks fine too :)
 
F

Florian Assmann

ChrisH said:
Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?

Dir.entries(Dir.pwd).delete_if{|f| ['.','..'].include?(f)}

And to have this python feeling once and forever you could for example
do this:

irb(main):002:0> def Dir.listdir( dir )
irb(main):003:1> Dir.entries( dir ) - [ '.', '..' ]
irb(main):004:1> end

HeFun
Florian
 
G

George Ogata

rtilley said:
ChrisH said:
Brad said:
Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
os.listdir() which explicitly excludes these. How is this done in Ruby?
Further reading shows that Dir::[] (aka Dir::glob) will list
files/directories and not include ',' or '..'


Thank you... I found that Dir.glob('*') works exactly like I want.

Note that Dir.glob('*') and Dir.['*'] omit '.' and '..' by virtue of
the fact that they omit ALL dot-files, à la shell. That may or may
not be what you want...
 

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

Staff online

Members online

Forum statistics

Threads
474,206
Messages
2,571,068
Members
47,674
Latest member
scazeho

Latest Threads

Top