Forum: Ruby class Dir

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fleck (Guest)
on 2005-11-22 10:55
(Received via mailing list)
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.
nobuyoshi.nakada (Guest)
on 2005-11-22 10:59
(Received via mailing list)
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.
Fleck (Guest)
on 2005-11-22 11:39
(Received via mailing list)
Thanks,
it works. I just offered names to the 'directory?'-method.
m.fellinger (Guest)
on 2005-11-22 11:51
(Received via mailing list)
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.:
This topic is locked and can not be replied to.