如何在 Ruby 中将 schema 设置为 URI 对象
我正在尝试从用户输入中解析 URI。我假设有些用户不会将该方案放入他们的 URI 中,并且我想默认为“http”。
以下代码不起作用:
require 'uri'
uri_to_check = URI::parse("www.google.com")
uri_to_check.scheme = "http" unless uri_to_check.scheme
puts uri_to_check.to_s
我希望看到“http://www.google.com”,但我获取“http:www.google.com”。甚至可以这样做吗?
如果是这样,我错过了什么?
有更好的方法吗?
I'm trying to parse a URI from user input. I'm assuming some users won't put the scheme in their URI's and I want to default to "http".
The following code doesn't work:
require 'uri'
uri_to_check = URI::parse("www.google.com")
uri_to_check.scheme = "http" unless uri_to_check.scheme
puts uri_to_check.to_s
I expect to see "http://www.google.com" but I get "http:www.google.com". Is it even possible to do it this way?
If so, what am I missing?
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
前导斜杠 (
//
) 表示 URL 是基于 IP 的地址,需要标记主机名,以便 URI 可以正确解析它们。维基百科有一些很好的概述和使用示例:
http://en.wikipedia.org/wiki/Url,
http://en.wikipedia.org/wiki/URI_scheme ,
http://en.wikipedia.org/wiki/URL_normalization
最好的信息就在规范本身中:http://www.ietf.org/rfc/rfc1738.txt 特别是在部分3.1“3.1.通用互联网方案语法”。
您可能需要考虑使用可寻址 gem。它更智能,当我需要进行大量 URI 解析或操作时,我会使用它。
http://addressable.rubyforge.org/ 和
http://addressable.rubyforge.org/api/Addressable/URI.html
The leading slashes (
//
) indicate that the URL is an IP-based address, and are needed to flag the hostname so URI can parse them correctly.Wikipedia has some good overviews and examples of use:
http://en.wikipedia.org/wiki/Url ,
http://en.wikipedia.org/wiki/URI_scheme ,
http://en.wikipedia.org/wiki/URL_normalization
The best information is in the spec itself: http://www.ietf.org/rfc/rfc1738.txt particularly in section 3.1 "3.1. Common Internet Scheme Syntax".
You might want to consider using the Addressable gem. It's smarter and is what I use when I need to do a lot of URI parsing or manipulation.
http://addressable.rubyforge.org/ and
http://addressable.rubyforge.org/api/Addressable/URI.html
当要解析的字符串不包含模式时,
URI
无法识别它作为主机名:
当您按照示例中的方式设置方案,然后调用
to_s
时,将在没有主机的情况下构建 URI...您可以尝试如下操作:(这是一个快速技巧,我不知道
URI
详细信息...)When the string you want to be parsed doesn't conatin a scheme,
URI
doesn't recognize itas a hostname:
When you set the scheme as you do in your example and then call
to_s
the URI is build without the host...You can try something like the following: (That's a quick hack, I don't know the
URI
details...)