带有Keys中字符串的Ruby散列和值中的Array

时间:2014-03-19 09:40:09

标签: ruby-on-rails ruby arrays hash

您好我是ruby中的新手,我希望在键中输入带字符串的哈希值和值中的数组,如下所示:

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有[issue123,issue234,issue345]

感谢

1 个答案:

答案 0 :(得分:3)

如下所示:

result = Hash.new { |hash, key| hash[key] = [] }

issues.each do |issue|
  result[issue.user_name].push issue  
end