How distinguish between a file and a directory?

B

Bazsl

A newbie question. The code

Dir.foreach(Dir.getwd) do |f|
if ((f != '.') && (f != '..'))
puts f
end
end

lists the files and directories in the current working directory but
does not identify which is a file and which is a directory. How can I
add that information to the listing? Thanks.
 
F

Farrel Lifson

File::directory?(path)
will return true of name is a directory, false if it is a file

Farrel
 
M

Michael Fellinger

A newbie question. The code

Dir.foreach(Dir.getwd) do |f|
if ((f != '.') && (f != '..'))
puts f
end
end

lists the files and directories in the current working directory but
does not identify which is a file and which is a directory. How can I
add that information to the listing? Thanks.

Dir['*'].each do |f|
print "#{f} is a: "
if File.directory?(f)
puts "directory"
else
puts "file"
end
end

Please note that Dir::[] is an alias to Dir::glob and it will not show
you files starting with a dot unless you use Dir['{,.}*']

^ manveru
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top