gpt4 book ai didi

ruby-on-rails - 在 ActiveSupport 日期范围内迭代的 rake 任务中的无限循环

转载 作者:行者123 更新时间:2023-12-04 18:16:22 26 4
gpt4 key购买 nike

我的 rake 任务卡在无限循环中时遇到问题。到目前为止,我已经能够将问题隔离为一段将日期范围转换为数组的代码。即使我直接调用它:

(1.month.ago.beginning_of_month..1.month.ago.end_of_month).to_a.each {|d| p d}

该任务永远挂起迭代相同的元素:
"2012-06-01 00:40:41 UTC"
"2012-06-01 00:40:41 UTC"
"2012-06-01 00:40:41 UTC"
"2012-06-01 00:40:41 UTC"
"2012-05-31 20:40:41 UTC"
"2012-05-31 20:40:41 UTC"
"2012-06-01 00:40:42 UTC"
"2012-06-01 00:40:42 UTC"
"2012-06-01 00:40:42 UTC"
"2012-06-01 00:40:42 UTC"
"2012-05-31 20:40:42 UTC"
"2012-05-31 20:40:42 UTC"
...

如果相反我使用这个
(Date.new(2012,6,1)..Date.new(2012,6,30)).to_a.each {|d| p d}

一切正常,因此必须专门针对日期/时间使用 ActiveSupport 扩展。有没有其他人看过这个?

这发生在带有 Rails 3.1 和 Ruby 1.9.3 的 Windows 机器上的开发环境中,如果这很重要的话。

最佳答案

您正在创建 Time一个月的范围,增量为 1 秒,约 250 万个条目:

(1.month.ago.beginning_of_month..1.month.ago.end_of_month)
# => Fri, 01 Jun 2012 00:00:00 CEST +02:00..Sat, 30 Jun 2012 23:59:59 CEST +02:00
1.month.ago.beginning_of_month.succ
# => Fri, 01 Jun 2012 00:00:01 CEST +02:00

创建 Date范围使用:
(1.month.ago.beginning_of_month.to_date..1.month.ago.end_of_month.to_date)
# => Fri, 01 Jun 2012..Sat, 30 Jun 2012
1.month.ago.beginning_of_month.to_date.succ
# => Sat, 02 Jun 2012

关于ruby-on-rails - 在 ActiveSupport 日期范围内迭代的 rake 任务中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com