Ruby: How to turn a hash into HTTP parameters?

后端 未结 14 1611
故里飘歌
故里飘歌 2020-12-07 06:46

That is pretty easy with a plain hash like

{:a => \"a\", :b => \"b\"} 

which would translate into

\"a=a&b=b\"
<         


        
14条回答
  •  长情又很酷
    2020-12-07 07:26

    {:a=>"a", :b=>"b", :c=>"c"}.map{ |x,v| "#{x}=#{v}" }.reduce{|x,v| "#{x}&#{v}" }
    
    "a=a&b=b&c=c"
    

    Here's another way. For simple queries.

提交回复
热议问题