From 3e0e96f8f51af6891c8082f0e9d6e3444edcbfa8 Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 12 Aug 2024 15:53:32 -0700 Subject: [PATCH] Fixed `Addressable::URI#inspect` to use `self.class`. * This will allow sub-classing `Addressable::URI` and have the sub-classes name show up in the `inspect` output. class CustomURL < Addressable::URI def custom_method ... end end uri = CustomURL.parse("https://example.com") # => # --- lib/addressable/uri.rb | 2 +- spec/addressable/uri_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/addressable/uri.rb b/lib/addressable/uri.rb index 40b80cf0..07c9aeb5 100644 --- a/lib/addressable/uri.rb +++ b/lib/addressable/uri.rb @@ -2382,7 +2382,7 @@ def to_hash # # @return [String] The URI object's state, as a String. def inspect - sprintf("#<%s:%#0x URI:%s>", URI.to_s, self.object_id, self.to_s) + sprintf("#<%s:%#0x URI:%s>", self.class.to_s, self.object_id, self.to_s) end ## diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index 26ee923d..07818490 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -1757,6 +1757,19 @@ def to_s expect(@uri.inspect).to include("%#0x" % @uri.object_id) end + context "when Addressable::URI has been sub-classed" do + class CustomURIClass < Addressable::URI + end + + before do + @uri = CustomURIClass.parse("http://example.com") + end + + it "when inspected, should have the sub-classes name" do + expect(@uri.inspect).to include("CustomURIClass") + end + end + it "should use the 'http' scheme" do expect(@uri.scheme).to eq("http") end