From: Gavin Sinclair Date: 2002-08-06T00:13:22+09:00 Subject: Re: Regex question ----- Original Message ----- From: "Mike Hall" [All sorts of people wrote all sorts of things, and then...] > > > > /[^.]*$/.match(file)[0] > > Now wouldn't all this be much simpler if we had a method > along the lines of 'basename', which would pull off the > extension (or suffix) for us? We already have File::basename > and File::dirname, so why not a File::last-part-of-the-name-name? > > Or what about a 'File::parts' which returns an array of: > directory (or nil), root name (or nil), suffix(es) (or nil). > Would that be more Ruby-esqe? > I agree there's room for some help from the File module here. We already have File.split("/home/fred/aliases.sh") # -> ["/home/fred", "aliases.sh"] which is nice. I think the most sensible think is for this method to take an optional parameter, indicating whether the extension should be a separate element. In fact, it could be an integer specifying how many dots to include in the extension. So: File.split("tuesday.txt") # -> [".", "tuesday.txt"] File.split("/games/doom.tar.gz") # -> ["/games", "doom.tar.gz"] File.split("/games/doom.tar.gz", 1) # -> ["/games", "doom.tar", "gz"] File.split("/games/doom.tar.gz", 2) # -> ["/games", "doom", "tar.gz"] File.split("/games/doom.tar.gz", 3) # -> ["/games", "doom", "tar.gz"] Note that the first two lines represent current File.split behaviour, and the final three are proposed extensions. The method "split" is very appropriate here, as (and I should have thought of this earlier) String.split can be used to chop up a filename into extensions in this manner. Of course, we'd need File.extension(path, n=1) to match File.dirname and File.basename, as well. --Gavin