From: Massimiliano Mirra Date: 2002-01-13T10:00:07+09:00 Subject: Re: Dir.entries have no home On Sun, Jan 13, 2002 at 02:43:33AM +0900, Massimiliano Mirra wrote: > > I'd like to be able to ask questions of directory entries, like are > > you a directory, what is your last-written date, and so on. > Does this help you? Should be pretty portable, too. Some afterthoughts... > class XDir > def XDir.entries(dir) > dir = File.expand_path(dir) > Dir.entries(dir).collect {|name| FileDesc.new(dir, name)} > end > end This could more simply be: class Dir def Dir.desc_entries(dir) dir = File.expand_path(dir) Dir.entries(dir).collect {|entry| FileDesc.new(dir, name)} end end > class FileDesc > def initialize(location, name) > @location, @name, @path = location, name, File.join(location, name) > @stat = File.stat(@path) > end > attr_reader :location, :name, :path, :stat > > def method_missing(method_id, *args, &block) > if @stat.respond_to? method_id > @stat.send(method_id, *args, &block) > else > super > end > end I wonder (and would like opinions on) whether there is any hidden trouble, beyond synchronization issues, in the otherwise useful idea to go from a FileDescription object to a FileHandle object. With a FileHandle, things like the following could be possible: log = FileHandle.new('/home/joe', 'program.log') log.location => "/home/joe" log.name => "program.log" log.path => "/home/joe/program.log" log.location = '/home/joe/subdir' log.location => "/home/joe" log.commit => true log.location => "/home/joe/subdir" log.path = '/home/joe/otherdir/othername' log.commit => true log.location => "/home/joe/otherdir" log.name => "othername" log.location = '/file_in_root_dir' log.commit => false log.location => "/home/joe/otherdir" Massimiliano