I am quite new to ruby but enjoying it so far quite immensely. There are some things that have given me some trouble and the following is no exception.
What I am trying to do here is create a sort of ‘super-directory’ by sub-classing ‘Dir’. I’ve added a method called ‘subdirs’ that is designed to list the directory object’s files and push them into an array if the file is a directory itself. The issue is, the results from my test (File.directory?) is strange – here is my method code:
def subdirs
subdirs = Array.new
self.each do |x|
puts "Evaluating file: #{x}"
if File.directory?(x)
puts "This file (#{x}) was considered a directory by File.directory?"
subdirs.push(x)
#yield(x) if block_given?
end
end
return subdirs
end
And strangely, even though there are plenty of directories in the directory I’ve chosen (“/tmp”) – the result of this call only lists “.” and “..”
puts "Testing new Directory custom class: FileOps/DirClass"
nd = Directory.new("/tmp")
subs = nd.subdirs
And the results:
Evaluating file: mapping-root
Evaluating file: orbit-jvxml
Evaluating file: custom-directory
Evaluating file: keyring-9x4JhZ
Evaluating file: orbit-root
Evaluating file: .
This file (.) was considered a directory by File.directory?
Evaluating file: .gdmFDB11U
Evaluating file: .X0-lock
Evaluating file: hsperfdata_mishii
Evaluating file: .X11-unix
Evaluating file: .gdm_socket
Evaluating file: ..
This file (..) was considered a directory by File.directory?
Evaluating file: .font-unix
Evaluating file: .ICE-unix
Evaluating file: ssh-eqOnXK2441
Evaluating file: vesystems-package
Evaluating file: mapping-jvxml
Evaluating file: hsperfdata_tomcat
Are you executing the script from within
/tmp? My guess (I haven’t tried this) is thatFile.directory?(x)is testing to see if there’s a directory named x in the current directory — so, if you’re running this from another directory, you’ll always find.and..but not the other directories.Try changing
File.directory?(x)toFile.directory?("#{path}/#{x}").