Skip to content

Commit 4229164

Browse files
authored
Fixed Addressable::URI#inspect to use self.class. (#544)
* 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") # => #<CustomURL:0x1824 URI:https://example.com>
1 parent 96c9da5 commit 4229164

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/addressable/uri.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ def to_hash
23822382
#
23832383
# @return [String] The URI object's state, as a <code>String</code>.
23842384
def inspect
2385-
sprintf("#<%s:%#0x URI:%s>", URI.to_s, self.object_id, self.to_s)
2385+
sprintf("#<%s:%#0x URI:%s>", self.class.to_s, self.object_id, self.to_s)
23862386
end
23872387

23882388
##

spec/addressable/uri_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,19 @@ def to_s
17571757
expect(@uri.inspect).to include("%#0x" % @uri.object_id)
17581758
end
17591759

1760+
context "when Addressable::URI has been sub-classed" do
1761+
class CustomURIClass < Addressable::URI
1762+
end
1763+
1764+
before do
1765+
@uri = CustomURIClass.parse("http://example.com")
1766+
end
1767+
1768+
it "when inspected, should have the sub-classes name" do
1769+
expect(@uri.inspect).to include("CustomURIClass")
1770+
end
1771+
end
1772+
17601773
it "should use the 'http' scheme" do
17611774
expect(@uri.scheme).to eq("http")
17621775
end

0 commit comments

Comments
 (0)