Skip to content

Instantly share code, notes, and snippets.

@fushang318
Created December 9, 2010 05:15
Show Gist options
  • Save fushang318/734367 to your computer and use it in GitHub Desktop.
Save fushang318/734367 to your computer and use it in GitHub Desktop.
URL query to Hash:
Ruby代码
irb(main):012:0> url = "http://javaeye.com?a=1&b=2&c=3#123"
=> "http://javaeye.com?a=1&b=2&c=3#123"
irb(main):013:0> url = URI.parse(url)
=> #<URI::HTTP:0x4cca868 URL:http://javaeye.com?a=1&b=2&c=3#123>
irb(main):014:0> url.query
=> "a=1&b=2&c=3"
irb(main):018:0> Hash[* url.query.split(/[&=]/)]
=> {"a"=>"1", "b"=>"2", "c"=>"3"}
Hash to URL Query:
Ruby代码
hash = {"a"=>"1", "b"=>"2", "c"=>"3"}
irb(main):023:0> hash.map{|k_v| k_v.join "="} * "&"
=> "a=1&b=2&c=3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment