Addressable::URI - 得到 url 没有任何参数

Addressable::URI - get url without any parameter

url = Addressable::URI.parse("https://my.url?with=a&lot=of&parameters")

如何在不知道有哪些参数或有多少参数的情况下获取没有任何查询参数的基础 url?

我做不到url.query_values = {}因为我有时也需要原版url。

你想要的是使用omit:

Addressable::URI#omit(*components) ⇒ Addressable::URI

Omits components from a URI.

url = Addressable::URI.parse('https://sub.example.com/path/to/foo?x=1&y=2#hash')

url.omit(:query).to_s
# =>
"https://sub.example.com/path/to/foo#hash"

如果您也不想要散列或其他组件,也将它们传递给 omit

url.omit(:query, :hash)