Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: URI concatenate behavior

Tags:

uri

ruby

I am currently developing an HTML parser and I came to one specific website that is not properly coded in terms of urls. On the main page ("http://example.com/a/b"), I have some links starting with "a/b" without the initial "/". Ruby URI works correctly as per my understanding of relative paths:

uri = URI("http://example.com/a/b")
(uri + "a/b").to_s
==> http://example.com/a/a/b

However, the behavior seems to be different in a browser such as Firefox or Chrome, as the page is directed to http://example.com/a/b (which works by the way).

Have you seen this before and what would you suggest to have the same behavior in Ruby than in Web browsers?

like image 347
user1694048 Avatar asked Sep 03 '26 08:09

user1694048


1 Answers

I think uri + 'a/b' is simply doing a string concatenation.

What I would suggest is to try this:

File.join('http://www.example.com', 'a/b')

File.join supports this kind of operation :)

like image 59
Tigraine Avatar answered Sep 06 '26 20:09

Tigraine