【问题标题】:Divide data into multiple hashes on the basis of timestamp [closed]根据时间戳将数据划分为多个哈希[关闭]
【发布时间】:2013-06-30 18:42:06
【问题描述】:

我有一个通知页面。就像社交网站上的任何通用通知页面一样。每个通知都有一个 unix 时间戳。我想根据天将通知划分为多维散列。哈希键可以表示它的日期。例如:

notification['3July'][0] = 'Answered a question'
notification['3July'][1] = 'Answer got 13 upvotes'
notification['3July'][2] = 'Earned a badge'
....

notification['23June'][0]  = 'Set a bounty of 50 credits'
notification['23June'][1]  = 'Followed XYZ'
notification['23June'][2]  = 'Added a question on stack overflow'

然后我将遍历多维哈希并在每天发布通知。目前我从数据库中获取数据,格式如下:

notification[0] = { 'text' => 'Answered a question' , 'timestamp' => 123456 }
notification[1] = { 'text' => 'Answer got 13 upvotes'  , 'timestamp' => 123456 }
notification[2] = { 'text' => 'Earned a badge'  , 'timestamp' => 123456 }
....

notification[3]  = { 'text' =>'Set a bounty of 50 credits'  , 'timestamp' => 123456 }
notification[4]  = { 'text' =>'Followed XYZ'  , 'timestamp' => 123456 }
notification[5]  = { 'text' =>'Added a question on stack overflow'  , 'timestamp' => 123456 }

在 Ruby/Rails 中最干净、最优化的方法是什么?

【问题讨论】:

标签: ruby-on-rails ruby arrays ruby-on-rails-3


【解决方案1】:

按时间戳排序通知,添加一列以选择只有日期的内容,然后在 Ruby 中使用group_by。这应该是最简单的方法。

要选择要使用的其他列,您应该为其添加 attr_accessor

http://www.ruby-doc.org/core-2.0/Enumerable.html#method-i-group_by

【讨论】:

  • 现在就试试这个。如果成功就接受。
  • 稍微更新了描述。
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 2015-02-08
  • 1970-01-01
  • 2017-11-15
  • 2016-03-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多