Ruby URI module creates illegal URI file

This is the next question for Converting the path to URI files .

Consider:

require 'uri'

uri = URI.join('file:///', '/home/user/dir1/dir2/dir3/name.ext')
 => #<URI::Generic:0x0000000263fcc0 URL:file:/home/user/dir1/dir2/dir3/name.ext>

uri.to_s
 => "file:/home/user/dir1/dir2/dir3/name.ext"

Is the result illegal? Shoudln't it be "file://home/...", with a double slash?

+3
source share
1 answer

No. file://home/...refers to a file on the host named "home". Full file:///home/...three-slash syntax where an empty host component indicates the local host. However, most URI parsers that recognize a file scheme also accept file:/pathnameonly with one slash; the absence of double slashes means that the host component is skipped.

+4
source

All Articles