From: nobu.nokada@... Date: 2002-04-27T00:50:43+09:00 Subject: Re: expand_path and overlap Hi, At Sat, 27 Apr 2002 00:20:39 +0900, Morris, Chris wrote: > > > I'm not sure, is this like what you want? > > > > def collapse_path(path, base) > > head, = path.split("/", 2) > > base.rindex(%r"(\A|.*/)#{Regexp.quote(head)}(?:/|\z)") > > ($1 or base) + path > > end > > Thanks! It seems to be close -- though I haven't groked it myself to see > where it's breaking down. First unit test fails: > > assert_equal("/a/b/c", collapse_path("b/c", "/a/b/c/d/e")) Hmmm, it apparently passes. Only 4th test failed. def collapse_path(path, base) head, = path.split("/", 2) base.rindex(%r"(\A|.*/)#{Regexp.quote(head)}(?:/|\z)") ($1 or base.sub(%r|[^/]\z|, '\&/')) + path end if $0 == __FILE__ require "test/unit" class TC_collapse_path < Test::Unit::TestCase def test_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")) end end end -- Nobu Nakada