【发布时间】: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 中最干净、最优化的方法是什么?
【问题讨论】:
-
Array#[]不能将字符串作为参数。 -
编辑了问题。将数组更改为哈希。
-
预计您将展示您为解决此问题而编写的代码。请参阅sscce.org 了解更多信息。
标签: ruby-on-rails ruby arrays ruby-on-rails-3