[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dir.entries

Brad Tilley

4/7/2006 2:10:00 AM

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

Chris Hulan

4/7/2006 5:12:00 AM

0


Brad Tilley wrote:
> 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)}

Chris Hulan

4/7/2006 5:18:00 AM

0

Brad Tilley wrote:
> 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 '..'

Brad Tilley

4/7/2006 12:23:00 PM

0

ChrisH wrote:
> Brad Tilley wrote:
>
>>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.

baumanj

4/7/2006 3:58:00 PM

0

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

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

Brad Tilley

4/7/2006 5:13:00 PM

0

baumanj@gmail.com wrote:
> 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
>
> Brad Tilley wrote:
>
>>Why does Dir.entries include '.' and '..'? I'm accustomed to Python's
>>os.listdir() which explicitly excludes these. How is this done in Ruby?
>
>

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

Florian Assmann

4/8/2006 12:17:00 AM

0

ChrisH schrieb:
> Brad Tilley wrote:
>> 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 :)

Florian Assmann

4/8/2006 12:21:00 AM

0

ChrisH schrieb:
> Brad Tilley wrote:
>> 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

George Ogata

4/15/2006 5:38:00 AM

0

rtilley <rtilley@vt.edu> writes:

> ChrisH wrote:
>> Brad Tilley wrote:
>>
>>>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...