![](/img/trans.png)
[英]Ruby/Rails: How do I convert a keys-values string(with array value included) to valid hash
[英]Ruby hash with string in Keys and an Array in Value
嗨,我是红宝石的新手,我想获得一个哈希,其中包含键字符串和值数组,如下所示:
Hash = new HashMap
for (issue :is)
Hash.add(is.user_name)
if(hash.contains(is.user_name)) then
hash.value.add(is)
end
end
得到这样的结果:
{"jane"[issue123,issue234,issue345]; "mike" [issue333,issue444,issue555]; "Alain" [issue876,issue987,issue356] }
简(Jane have)[issue123,issue234,issue345]
谢谢
如下所示:
result = Hash.new { |hash, key| hash[key] = [] }
issues.each do |issue|
result[issue.user_name].push issue
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.