Ruby 4.0 リファレンスマニュアル

instance method URI::HTTP#authority

authority -> StringRuby 3.1 から[permalink][rdoc][edit]

[RFC3986] の Section 3.2 で定義されている authority を文字列で返します。

port が URI::Generic.default_port と同じ場合は host だけを返します。そうでない場合は "host:port" という形式の文字列を返します。

require 'uri'
p URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority
# => "www.example.com"
p URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority
# => "www.example.com:8000"
p URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority
# => "www.example.com"

[SEE_ALSO] URI::HTTP#origin, URI::Generic.default_port