How do I safely join relative url segments?

前端 未结 11 1918
南方客
南方客 2020-12-30 19:27

I\'m trying to find a robust method of joining partial url path segments together. Is there a quick way to do this?

I tried the following:

puts URI::         


        
11条回答
  •  甜味超标
    2020-12-30 20:23

    You can use this:

    URI.join('http://exemple.com', '/a/', 'b/', 'c/', 'd')
    => #
    URI.join('http://exemple.com', '/a/', 'b/', 'c/', 'd').to_s
    => "http://exemple.com/a/b/c/d"
    

    See: http://ruby-doc.org/stdlib-2.4.1/libdoc/uri/rdoc/URI.html#method-c-join-label-Synopsis

提交回复
热议问题