working with file types and directories

J

John Maclean

This line shows all the files in the current dir.

This one -should- show all of thier filetypes...

Dir.foreach(".") {|x| File.ftype x }
=> nil
# what gives?

irb(main):023:0> Dir.foreach(".") {|x| File.ftype(x) }
=> nil

Dir.foreach(".") {|x| File.ftype(#{x})}
# I knew that this wouldn't work, but I tried it anyway
 
J

John W. Kennedy

John said:
This line shows all the files in the current dir.

This one -should- show all of thier filetypes...

Dir.foreach(".") {|x| File.ftype x }
=> nil
# what gives?

What do you mean by "show"? This doesn't "show" anything.

Perhaps you mean

Dir.foreach(".") {|x| puts File.ftype(x)}

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
 
J

Joel VanderWerf

John said:
This line shows all the files in the current dir.

This one -should- show all of thier filetypes...

Dir.foreach(".") {|x| File.ftype x }
=> nil
# what gives?

irb(main):023:0> Dir.foreach(".") {|x| File.ftype(x) }
=> nil

Dir.foreach(".") {|x| File.ftype(#{x})}
# I knew that this wouldn't work, but I tried it anyway

Try this:

Dir.foreach(".") {|x| puts File.ftype x }

Note that Dir.foreach doesn't return the results of the block. If you
want that behavior:

require 'enumerator'
Dir.enum_for:)foreach, ".").map {|x| File.ftype x }
 
J

John Maclean

Thanks for that reply. I guess that the only way to check that files
are non-empty is to open them, read them and cound the lines, huh?

On
Sat, 28 Jan 2006 14:56:21 +0900 Joel VanderWerf
 
A

Alexis Reigel

Thanks for that reply. I guess that the only way to check that files
are non-empty is to open them, read them and cound the lines, huh?

--> FileTest.zero?("file")
Returns true if file is of zero length.
 
J

John Maclean

Yesss!!! Thanks :)
Dir.foreach(".") {|x| puts FileTest.size(x) }
Dir.foreach(".") {|x| puts FileTest.zero?(x) } # checks for empty
files.

also I'm (finally) getting to grips with using ri.
 
J

John Maclean

OK, next step...
I can now run tests on files within directories. How can I achieve
something like the following?

puts "files have been checked" if Dir.foreach(".") {|x| puts
FileTest.writable?(x) } == "true"

#at the moment I get this:-
irb(main):022:0> puts "files have been checked" if Dir.foreach(".")
{|x| puts FileTest.writable?(x) } == "true" true
true
true
true
true
true

#but *not* the desired string "files have been checked"
 
J

Joel VanderWerf

John said:
OK, next step...
I can now run tests on files within directories. How can I achieve
something like the following?

puts "files have been checked" if Dir.foreach(".") {|x| puts
FileTest.writable?(x) } == "true"

#at the moment I get this:-
irb(main):022:0> puts "files have been checked" if Dir.foreach(".")
{|x| puts FileTest.writable?(x) } == "true" true
true
true
true
true
true

#but *not* the desired string "files have been checked"

Comparing the return value with the string "true" won't work because you
are outputting each writeable? result to stdout, and anyway that output
needs to be and-ed to test if all of the files are writable.

You do want the "files have been checked" output if all files are
writable, right?

Here's one way:

puts "files have been checked" if Dir.new(".").all? {|x|
FileTest.writable?(x) }
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
John Maclean said:
puts "files have been checked" if Dir.foreach(".") {|x| puts
FileTest.writable?(x) } == "true"

Dir.foreach returns nil, nil != "true" so the condition can not be
met. I think you want to do the following, right?

Dir.foreach(".") {|x| puts "file have been checked" if FileTest.writable?(x)}

where print out the message for each x which is a name of writable
file in the current directory.
 
J

John Maclean

Thanks for that reply...

I've gone through the great guide here http://pine.fm/LearnToProgram/ in
order to get me up to speed with itteration.

Essentialy what I'd like to do is to check some data within a
directory. Those files are essential to the rest of the program as this
is where the data "lives".

The following line check all files within the dir and prints a message
only if all of the files are writable. It's rather useful to see this
message once as there are loads of files within that dir.

puts "all files have been checked and are writable" if
Dir.new(".").all? {|x| FileTest.writable?(x) }
# this message is only printed once.

Your one-liner prints that message for every time it detects a
writable file.
 

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
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top