On Mac: File.file? behaves strangely

R

Ronald Fischer

Or, rather, I probably just made a silly mistake which I can't see
(because I had a long break since I wrote my last Ruby program). Please
have a look at this program:


#!/usr/local/bin/ruby
dir=ARGV[0] or raise "Which directory?";
puts "Opening:"+dir
Dir.foreach(dir) { |entry|
puts entry
if(File.file?(entry))
puts "is a file"
end
}


No matter which directory on Max OSX 10.4 I try this out, it lists all
entries in the directory, but only for the entry ".DS_Store" (which
exists on the Mac automatically in every sub-directory), it prints out
"is a file". It is as if the other directory entries would be no
(regular) files.


What did I do wrong?
 
J

Jesús Gabriel y Galán

Or, rather, I probably just made a silly mistake which I can't see
(because I had a long break since I wrote my last Ruby program). Please
have a look at this program:


#!/usr/local/bin/ruby
dir=3DARGV[0] or raise "Which directory?";
puts "Opening:"+dir
Dir.foreach(dir) { |entry|
=A0puts entry
=A0if(File.file?(entry))
=A0 =A0puts "is a file"
=A0end
}


No matter which directory on Max OSX 10.4 I try this out, it lists all
entries in the directory, but only for the entry ".DS_Store" (which
exists on the Mac automatically in every sub-directory), it prints out
"is a file". It is as if the other directory entries would be no
(regular) files.


What did I do wrong?

entry is relative to the dir, while File.file? works relative to the
working directory. Try this:

irb(main):016:0> Dir.foreach(dir) do |entry|
irb(main):017:1* p entry
irb(main):018:1> p File.file? entry
irb(main):019:1> p File.file?("#{dir}/#{entry}")
irb(main):020:1> end

Jesus.
 
R

Ronald Fischer

entry is relative to the dir, while File.file? works relative to the
working directory.

Of course!!!! I could have seen it myself from the output of my test
program. Stupid me!! Thanks for pointing this out!


Ronald
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top