From: Renaud HEBERT Date: 2001-08-31T00:30:34+09:00 Subject: [ruby-talk:20583] Re: Relative path So far we have: def rel_path(a, b) a = a.split('/') b = b.split('/') raise "windows sucks" if a[0] =~ /^\w\:$/ and a[0] != b[0] while a[0] == b[0] a.shift b.shift end '../'*a.size + b.join('/') end I like it but 1) rel_path(a, a) loops indefinitely. 2) the shifting of two array maybe not the "quickest way" So here is my version: def rel_path(a, b) a = a.split('/') b = b.split('/') raise "windows sucks" if a[0] =~ /^\w\:$/ and a[0] != b[0] i = 0 while ((a[i] == b[i]) && (i < a.size)) i += 1 end '../'*(a.size - i) + b[i..-1].join('/') end 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.. > -----Original Message----- > From: Gardian, Milan [mailto:Milan.Gardian@LEIBINGER.com] > Sent: Thursday, August 30, 2001 9:41 AM > To: ruby-talk@ruby-lang.org > Subject: [ruby-talk:20580] Relative path > > Hello, > > what is the shortest & quickest way of finding a relative path between path1 > and path2 in Ruby? (either universal or Win32 platform-specific)... > > E.g.: > ----- > path1 = "c:/temp/foo" > path2 = "c:/temp/foo/dir/another_dir" > path3 = "c:/special_dir" > path4 = "d:/dir" > > rel_path(path1, path2) -> "dir/another_dir" > > rel_path(path2, path1) -> "../.." > > rel_path(path2, path3) -> "../../../special_dir" > > rel_path(path1, path4) -> Exception - not possible > ----- > > Thank you for your help! > > Cheers, > > Milan -- Renaud Hebert phone: (33) 01 30 77 59 92 mailto:renaud.hebert@alcatel.fr