Skip to content

Commit

Permalink
Fixed Addressable::URI#inspect to use self.class. (#544)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
postmodern authored Aug 22, 2024
1 parent 96c9da5 commit 4229164
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,7 @@ def to_hash
#
# @return [String] The URI object's state, as a <code>String</code>.
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

##
Expand Down
13 changes: 13 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4229164

Please sign in to comment.