繁体   English   中英

Ruby哈希,键中包含字符串,值中包含数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM