开发者

How do I split a URL into 2 parts in Ruby?

开发者 https://www.devze.com 2022-12-12 10:25 出处:网络
I have a ruby script that downloads URLs from an RSS server and then downloads the files at those URLs.

I have a ruby script that downloads URLs from an RSS server and then downloads the files at those URLs.

I need to split the URL into 2 components like so 开发者_StackOverflow社区-

http://www.website.com/dir1/dir2/file.txt
-->   'www.website.com'    and    'dir1/dir2/file.txt'

I'm struggling to come up with a way to do this. I've been playing with regular expressions but nothing has worked. How would others go about doing this?


Use the URI library.

require 'uri'
u = URI.parse("http://www.website.com/dir1/dir2/file.txt")
u.host
# => "www.website.com"
u.path
# => "/dir1/dir2/file.txt"


In a simple way , you could use split .

split('/')[2]
0

精彩评论

暂无评论...
验证码 换一张
取 消