我有一个数组:
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
并且需要将其转换为包含密钥p
,a
,c
等的哈希值和值 - 等号右侧的任何内容。在红宝石中这样做的优雅方法是什么?
答案 0 :(得分:7)
Hash[ar.map{|s| s.split("=")}]
答案 1 :(得分:2)
require 'cgi'
ar = ["p=abhZRAh7Un", "a=2", "c=1", "l=3033102", "r=1", "rt=mr", "pid=136330865", "pdid=AiOIhH2vzMPqvhYkxXOxeA%3D%3D", "lks=54.0311", "fks=54.0311"]
CGI.parse(ar.join('&'))
输出:
=> {"rt"=>["mr"], "fks"=>["54.0311"], "pid"=>["136330865"], "lks"=>["54.0311"], "pdid"=>["AiOIhH2vzMPqvhYkxXOxeA=="], "r"=>["1"], "l"=>["3033102"], "c"=>["1"], "a"=>["2"], "p"=>["abhZRAh7Un"]}