当数组中存在空条目时,如何添加值?
我想创建一个实时序列数组。目前,我正在使用统计 gem 提取每个“天”的值:
define_statistic :sent_count, :count
=> :all, :group => 'DATE(date_sent)',
:filter_on => {:email_id => 'email_id
> = ?'}, :order => 'DATE(date_sent) ASC'
它的作用是创建一个数组,其中有日期的值,例如
[["12-20-2010",1], ["12-24-2010",3]]
但我需要它来填充空值,所以它看起来更例如:
[["12-20-2010",1], ["12-21-2010",0], ["12-22-2010",0], ["12-23-2010",0], ["12-24-2010",3]]
请注意第二个示例如何为第一个数组中缺少的天数提供“0”值。
I want to create a real time-series array. Currently, I am using the statistics gem to pull out values for each 'day':
define_statistic :sent_count, :count
=> :all, :group => 'DATE(date_sent)',
:filter_on => {:email_id => 'email_id
> = ?'}, :order => 'DATE(date_sent) ASC'
What this does is create an array where there are values for a date, for example
[["12-20-2010",1], ["12-24-2010",3]]
But I need it to fill in the null values, so it looks more like:
[["12-20-2010",1], ["12-21-2010",0], ["12-22-2010",0], ["12-23-2010",0], ["12-24-2010",3]]
Notice how the second example has "0" values for the days that were missing from the first array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要对基类进行猴子修补以包含此方法:它并不是通用的;它是一种通用的方法。即使有,它也不需要在那里。我会将它放在一个模块中,您可以将其混合到任何需要它的代码中:
但是,如果您真的想要:
I would recommend against monkey-patching the base classes to include this method: It's not all that general purpose; even if it were, it just doesn't need to be there. I'd stick it in a module that you can mix in to whatever code needs it:
However, if you really want to:
这是有效的:
大多数代码都在准备事情,要么构建一个新数组,要么将日期对象返回到字符串。
This works:
Most of the code is preparing things, either to build a new array, or return the date objects back to strings.