From: Pit Capitain Date: 2002-04-27T01:11:09+09:00 Subject: RE: expand_path and overlap On 27 Apr 2002, at 0:20, Morris, Chris gave some unit tests for collapse_path > assert_equal("/a/b/c", collapse_path("b/c", "/a/b/c/d/e")) > assert_equal("/a/b", collapse_path("b", "/a/b/c/d/e")) > assert_equal("/a/b/c/f", collapse_path("b/c/f", "/a/b/c/d/e")) > assert_equal("/a/b/c/d/e/m/n/o", collapse_path("m/n/o", "/a/b/c/d/e")) > assert_equal("/a/b/c/a/b/d", collapse_path("a/b/d", "/a/b/c/a/b/c")) Here are the modified implementations according to the unit tests. The result of collapse_path( "b/c/f", "/a/b/c/d/e/b/x/g/h" ) should be specified, too. def collapse_path1( path, base ) abs_path = '/' + path pattern = Regexp.quote( abs_path.sub( /(.)\/.*/, '\1' ) ) base.sub( /(.*)#{pattern}(\/|$).*/, '\1' ) + abs_path end def collapse_path2( path, base ) abs_path = '/' + path pattern = Regexp.quote( abs_path ) while pattern return $1 + abs_path if base =~ /(.*)#{pattern}(\/|$)/ pattern.sub!( /(.*)\/.*/, '\1' ) end return base + abs_path end Regards, Pit