From: tom_33 Date: 2008-02-21T06:29:58+09:00 Subject: Re: Is there any object-oriented File class in ruby ? On Feb 20, 12:52 am, Christopher Dicely wrote: > > > Essentially, this seems to be not much more than a string parsing > > function with no OO abstraction that represents a file object. > > Yes, File.basename *is* a string parsing function. > > > Compare this with doing the same thing with java: > > File f = new File("/home/gumby/work/ruby.rb"); // or new File("C:\\home > > \\gumby\\work\\ruby.rb"); > > f.getName() #=> "ruby.rb" > > Which also seems to be a string parsing function with no necessary OO > abstraction. Handling a variety of path separators is orthogonal to > whether there is OO abstraction going on. Yes, you are right and internally the implemention of the java method will need to parse the string somehow. However, an important difference with ruby File class and the java File class is that the java class provides a cohesive unit of instance methods that encapsulates a file path, without having to provide a string as parameter to every method. Since the ruby class method "File.basename(filename)" is nothing but a string parsing method it does IMHO not provide much useful value over instead having a class method such as "String.getStringPartAfterLastSlash(anyStringIncludingSomeSlash)" or "String.getStringPartAfterLastOccuranceOfSpecifiedCharacter(anyString, someCharacter)". Anyway, I have found what I was looking for in another reponse in this thread: On Feb 19, 11:40 pm, tom_33 wrote: >> Now my question is what have I been missing here ? >> I mean, is there any other much better File class somewhere in the >> Ruby core that is more pure OO ... On Feb 20, 12:37 am, Gary Wright wrote: > On Feb 19, 2008, at 5:45 PM, tom_33 wrote: > There is also Pathname, a class for manipulating file path strings > and looking up file properties. Yes, that was indeed the kind of class I was looking for. Obviously Ruby supports a procedural invocation style of using class methods on the File class (the same way you would do it with non-oo- language such as C, i.e. providing the same parameters over and over again since there is no object that can encapsulate it in a constructor call). For example, if you would want to implement a methods that renames files that are old and big (for some reason) then you would do this kind of stuff with the File class: if File.size(file_name) > someSizeLimit and File.mtime(file_name) < someTimeLimit then ... do some string manipulation to extract the filename part from the full path ... and also extract the directory part ... new_name = "big_old_file_" + fileNamePart new_nameWithFullPath = directoryPart + "/" + new_name File.rename(file_name, new_nameWithFullPath) end But with the much better Pathname class you can get an object with instance methods: path = Pathname.new(file_name) if path.size() > someSizeLimit and path.mtime() < someTimeLimit then directory = path.parent fileNamePart = path.basename new_name = "big_old_file_" + fileNamePart # below file separator concatenation seems to be automatically included if needed: newPath = directory + new_name path.rename(newPath) end There are two things that are better with the above code that uses Pathname instead of File: (1) You do not have to keep sending the same path string to the methods but in can be encapsulated and memorized by the Pathname instance. (2) You do not need to extract (and concatenate with separators) the directory part and the filename part through low level string parsing. Lastly, I just want to complain a little bit about the documentation of the File class, and hope that someone with write access to the following page: http://www.ruby-doc.org/core/classes/File.html will update it with a reference to the Pathname class, from the the description section in the beginning. Without such a reference, it is likely that other people will use the File class methods, without finding the Pathname class and its nice instance methods. Actually, I do not only think it should be mentioned, but it should be encouraged to use them instead of the File class methods.