From: Michael Witrant Date: 2001-10-28T02:10:31+09:00 Subject: [ruby-talk:23640] Re: Path class (was: Re: FileSpec vs Dir/File class methods) On Sat, 27 Oct 2001 19:46:56 +0900, Niklas Frykholm wrote: > A Path class might make it easier to deal with some things, such as > different path separators on different systems. On the other hand it > would complicate the language (more to learn). The File methods would > have to be changed to take both a string or a path. > > Personally, I'm quite happy with having paths as Strings. If you really > need Path objects, you can write a class for them (and post it to RAA > ;). > I've been thinking about a Path class for some weeks now, and you motivated me to finally write it :) You can find a first try there: http://www.lepton.fr/ruby/path.rb I just wrote it today and didn't really test it yet. The Path class inherits from String and implements: * most File::Stat instance methods (size, readable?, mtime, ...) * some File class methods (open, rename, dirname, chmod, ...) * some Dir class methods (entries, foreach, mkdir, chdir, ...) * Some other methods: - #read() if a block is given: like #open(filename) else: return the content of the file - #write(data = nil) if data is not nil: write data to file else: like #open(filename, 'w') (and a block must be given) - #/(other) like #join(other) Another class called Symlink behaves like Path, except #lstat is always used in place of #stat. A new method String#path converts a String to a Path. I would like to know some thoughts on these interrogations: - Should String#path be renamed String#to_path? - Should Path#delete call Dir.rmdir if the path is a directory and File.delete otherwise? For now it just calls File.delete. You must call Path#rmdir to remove a directory. - Is Dir.open of any use there? I never used any Dir instance in Ruby. - Using this class instead of builtins adds at least one extra call per method. Maybe should it be converted to a faster C module? - Path#entries and Path#foreach never return '.' or '..'. I don't think they have any use there. I've always exluded these two entries in my Ruby scripts. Is this a Good Thing? - Should #join (and #/) remove duplicate slashes? ie: '/bar/'.path.join('foo') => '/bar//foo' or '/bar/foo'? File::join does not. - Would a #rmtree method be nice? It would act like #delete, but remove the entire directory tree if it exists. Like 'rm -fr'. - Should it provide a way make all strings behave like a Path? And how? I would removes the need to call #path on each external string, and allow faster scripting ;-) - Any other remark or suggestion? Any contribution is welcome.. for example to build the test unit (if it needs one). Mike.