>>>>> In [ruby-dev : No.23631] >>>>> Masahiro Sakai (酒井政裕) <sakai / tom.sfc.keio.ac.jp> wrote: > 単なる文字列の連結ではなく、上に書いたように相対URIの解決として > 動作して欲しいと思っています。 なるほど。誤解してしまっていました。すいません。 > P.S. > この件とは関係ないのですが、最近URIをキーとするHashを使うことがあり、 > URI::Genericに#hashと#eql?が定義されていて欲しいと思いました。 この二件について、こういう感じでどうでしょうか: diff -ruN ruby-1.8.1.orig/lib/uri/generic.rb ruby-1.8.1/lib/uri/generic.rb --- ruby-1.8.1.orig/lib/uri/generic.rb 2004-03-24 21:20:32.000000000 +0900 +++ ruby-1.8.1/lib/uri/generic.rb 2004-06-06 14:17:46.000000000 +0900 @@ -726,7 +726,12 @@ # # => #<URI::HTTP:0x2021f3b0 URL:http://my.rubysite.com/main.rbx?page=1> # def merge(oth) - base, rel = merge0(oth) + begin + base, rel = merge0(oth) + rescue + raise $!.class, $!.message + end + if base == rel return base end @@ -734,7 +739,7 @@ authority = rel.userinfo || rel.host || rel.port # RFC2396, Section 5.2, 2) - if rel.path.empty? && !authority && !rel.query + if (rel.path.nil? || rel.path.empty?) && !authority && !rel.query base.set_fragment(rel.fragment) if rel.fragment return base end @@ -744,10 +749,10 @@ # RFC2396, Section 5.2, 4) if !authority - base.set_path(merge_path(base.path, rel.path)) + base.set_path(merge_path(base.path, rel.path)) if base.path && rel.path else # RFC2396, Section 5.2, 4) - base.set_path(rel.path) + base.set_path(rel.path) if rel.path end # RFC2396, Section 5.2, 7) @@ -785,14 +790,6 @@ return oth, oth end - if !self.hierarchical? - raise BadURIError, - "not hierarchical URI: #{self}" - elsif !oth.hierarchical? - raise BadURIError, - "not hierarchical URI: #{oth}" - end - if self.absolute? return self.dup, oth else @@ -913,7 +910,11 @@ # def route_from(oth) # you can modify `rel', but can not `oth'. - oth, rel = route_from0(oth) + begin + oth, rel = route_from0(oth) + rescue + raise $!.class, $!.message + end if oth == rel return rel end @@ -1045,6 +1046,14 @@ end end + def hash + self.component_ary.hash + end + + def eql?(oth) + self.hash == oth.hash + end + =begin --- URI::Generic#===(oth) diff -ruN ruby-1.8.1.orig/test/uri/test_mailto.rb ruby-1.8.1/test/uri/test_mailto.rb --- ruby-1.8.1.orig/test/uri/test_mailto.rb 2003-10-05 15:09:26.000000000 +0900 +++ ruby-1.8.1/test/uri/test_mailto.rb 2004-06-06 14:20:30.000000000 +0900 @@ -116,6 +116,13 @@ u.select(:scheme, :host, :not_exist, :port) end end + + def test_merge + # [ruby-dev:23628] + u0 = URI.parse('mailto:foo / example.com') + u1 = URI.parse('mailto:foo / example.com#bar') + assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1)) + end end これまで通りのテストが通ることは確認していますが、 non-hierarchical URIの場合に本当にうまく動いてくれるか分かりません。 もしよろしければテストを作っていただけると助かります。 # 互換性についてはテストが通ることで確認できたと考えています。 -- やまだあきら <URL:http://arika.org> Vine Caves, Ltd. <URL:http://vinecaves.com>