如何在Ruby 1.8中解析URI的查询部分?

如何在Ruby 1.8中解析URI的查询部分?,ruby,parsing,uri,ruby-1.8,Ruby,Parsing,Uri,Ruby 1.8,在Ruby1.8中,使用URI标准库,我可以解析 http://au.easyroommate.com/content/common/listing_detail.aspx?code=H123456789012&from=L123456789012345 使用URI.split获取 ["http", nil, "au.easyroommate.com", nil, nil, "/content/common/listing_detail.aspx", nil, "code=H1234

在Ruby1.8中,使用URI标准库,我可以解析

http://au.easyroommate.com/content/common/listing_detail.aspx?code=H123456789012&from=L123456789012345
使用
URI.split
获取

["http", nil, "au.easyroommate.com", nil, nil,
"/content/common/listing_detail.aspx", nil, 
"code=H123456789012&from=L123456789012345", nil]

但是,有没有可能不使用我自己的黑客从查询部分获取
H123456789012
位(例如通过
&
进行拆分,然后获取与
/code.(.*)/
匹配的位?

您正在寻找方法

params = CGI::parse("query_string")
  # {"name1" => ["value1", "value2", ...],
  #  "name2" => ["value1", "value2", ...], ... }

您可以使用
Rack::Utils
,它有一个名为
parse\u nested\u query
的方法,您可以从URL传入查询字符串:

Rack::Utils.parse_nested_query(uri.query_string)

然后,这将返回一个表示查询字符串的
Hash
对象,该查询字符串允许您访问
code

结果表明,Rack是标准库的一部分,而不是gem。这让我感到惊讶。Rack是gem,它不是Ruby标准库的一部分(这是一件好事)@Mauricio:你说得对。我忘了我已经安装好了。