From: Ryan Tarpine Date: 2001-08-31T01:44:46+09:00 Subject: [ruby-talk:20586] Re: Relative path Renaud HEBERT wrote: > > ... > But: > 1) it is still very fragile: 'dir//dir2' and 'dir/./dir2' will give > wrong results when compared to 'dir/dir2' > 2) unix is case sensitive, windows is not; '/' vs '\'.. > 3) it should be possible to do it only with strings manipulation to be > more efficient.. def rel_path(a, b) # Should work in in win and *nix sep = File::Separator # Get rid of /path//to/./here problems a = (File::expand_path a).split(sep) b = (File::expand_path b).split(sep) # Windows? raise "can't switch drives" if a[0] =~ /^\w\:$/ and a[0] != b[0] # If one of the paths starts with /, the split array # will have an empty first element (if a path is solely # /, it will be empty) a.shift unless a.empty? or not a[0].empty? b.shift unless b.empty? or not b[0].empty? # If a & b are empty, a[0] == b[0] == nil while a[0] == b[0] and not a.empty? and not b.empty? a.shift b.shift end parent = ".." + sep (parent * a.size) + b.join(sep) end I'm not sure about your last one, but I do have a problem of my own. What about network drives? e.g. //server/myservice File::expand_path on a string beginning with multiple "/"'s will not squeeze them into one "/". I assume this is not to mangle network paths. I have abolutely no experience with them, so I don't know if you just raise an exception. Someone else should have no problem answering this. Also: "/".split("/").length == 0, but "/a".split("/a").length == 2. In the second one, there is an empty string for the first element. Should the first one act the same (return an array with a single element, an empty string)? -- Ryan Tarpine, rtarpine@hotmail.com "Technology is a queer thing. It brings you great gifts in one hand, and it stabs you in the back with the other." -- C.P. Snow