# Class Dir

**URL:** <https://www.ruby-forum.com/t/class-dir/95857>\
**Category:** Ruby\
**Created:** [November 22, 2005, 9:55am UTC](https://www.ruby-forum.com/t/class-dir/95857 "2005-11-22T09:55:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 22, 2005, 9:55am UTC](https://www.ruby-forum.com/t/class-dir/95857/1 "2005-11-22T09:55:20Z")

</div>

Hi,  
what’ s the best way to detect if a Dir entry is a directory or file? I’  
m doing a Dir.foreach blabla |x|.  
I’ m trying to use ‘directory?’, but it doesn’ t work the way I use it.

Thanks,  
Alex.

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 22, 2005, 9:59am UTC](https://www.ruby-forum.com/t/class-dir/95857/2 "2005-11-22T09:59:22Z")

</div>

Hi,

At Tue, 22 Nov 2005 17:52:23 +0900,  
Alexander F. wrote in [ruby-talk:166951]:

> what’ s the best way to detect if a Dir entry is a directory or file? I’ m doing a Dir.foreach blabla |x|.  
> I’ m trying to use ‘directory?’, but it doesn’ t work the way I use it.

Dir.foreach yields just basenames. You’ll need to prefix the  
directory name to them.

Dir.foreach(blabla) {|x| x = File.join(blabla, x); p x if  
File.directory?(x)}

Or, Dir.glob(File.join(blabla, “\*/”)) returns an array of the  
directories.

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 22, 2005, 10:39am UTC](https://www.ruby-forum.com/t/class-dir/95857/3 "2005-11-22T10:39:37Z")

</div>

Thanks,  
it works. I just offered names to the ‘directory?’-method.

---

<div class="post-metadata">

**Author:** ![Fleck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@Fleck](https://www.ruby-forum.com/u/Fleck)\
**Post date:** [November 22, 2005, 10:51am UTC](https://www.ruby-forum.com/t/class-dir/95857/4 "2005-11-22T10:51:41Z")

</div>

Dir.new(’./’).entries.partition {|i| File.directory?(i))}  
=\> [  
[".", “…”, “log”, “src”, “conf”, “\_darcs”, “public”, “script”,  
“templates”],  
[“README”, “run.rb.save”, “run.rb”]  
]

Am Dienstag 22 November 2005 09:52 schrieb Alexander F.:
