ruby URI类
Posted 难&道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby URI类相关的知识,希望对你有一定的参考价值。
一. URI
require ‘uri‘ uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413") #=> #<URI::HTTP:0x00000000b14880 URL:http://foo.com/posts?id=30&limit=5#time=1305298413> uri.scheme #=> "http" uri.host #=> "foo.com" uri.path #=> "/posts" uri.query #=> "id=30&limit=5" uri.fragment #=> "time=1305298413" uri.to_s #=> "http://foo.com/posts?id=30&limit=5#time=1305298413"
二. 常用方法
-
encode_www_form(enum, enc=nil)
URI.encode_www_form([["q", "ruby"], ["lang", "en"]]) #=> "q=ruby&lang=en" URI.encode_www_form("q" => "ruby", "lang" => "en") #=> "q=ruby&lang=en" URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en") #=> "q=ruby&q=perl&lang=en" URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]]) #=> "q=ruby&q=perl&lang=en"
-
decode_www_form(str, enc=Encoding::UTF_8, separator: ‘&‘, use__charset_: false, isindex: false)
ary = URI.decode_www_form("a=1&a=2&b=3") p ary #=> [[‘a‘, ‘1‘], [‘a‘, ‘2‘], [‘b‘, ‘3‘]] p ary.assoc(‘a‘).last #=> ‘1‘ p ary.assoc(‘b‘).last #=> ‘3‘ p ary.rassoc(‘a‘).last #=> ‘2‘ p Hash[ary] # => {"a"=>"2", "b"=>"3"}
相当于对请求字符串进行编解码
以上是关于ruby URI类的主要内容,如果未能解决你的问题,请参考以下文章