Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中循环使用几个月_Ruby On Rails_Ruby_Ruby On Rails 3_Loops - Fatal编程技术网

Ruby on rails 如何在RubyonRails中循环使用几个月

Ruby on rails 如何在RubyonRails中循环使用几个月,ruby-on-rails,ruby,ruby-on-rails-3,loops,Ruby On Rails,Ruby,Ruby On Rails 3,Loops,我需要在RubyonRails应用程序中循环几个月。对于每个月,我都需要找到给定月份的第一天和最后一天。然后在单独的查询中使用这些日期来查找这些日期期间发生的事件并对其进行计算 最初,我尝试了以下方法: (11.months.ago.to_date.month..Date.today.month).each do |m| start_date = '01-#{m}-#{Date.today.year}'.to_date.beginning_of_month end_date = '01-

我需要在RubyonRails应用程序中循环几个月。对于每个月,我都需要找到给定月份的第一天和最后一天。然后在单独的查询中使用这些日期来查找这些日期期间发生的事件并对其进行计算

最初,我尝试了以下方法:

(11.months.ago.to_date.month..Date.today.month).each do |m|
  start_date = '01-#{m}-#{Date.today.year}'.to_date.beginning_of_month
  end_date = '01-#{m}-#{Date.today.year}'.to_date.end_of_month
end
当然,在这种情况下,如果11个月前涉及到上一年,年份不会更新。而且,我认为这种类型的for/each循环不起作用。我还尝试将数字映射到一个数组并使用相同的方法,但收到了一个错误


完成类似任务的最佳方法是什么?

这里有一种方法:

number_of_months = 0..11
number_of_months.to_a.reverse.each do |month_offset|
  start_date = month_offset.months.ago.beginning_of_month
  end_date = month_offset.months.ago.end_of_month
  puts "Start date : #{start_date} End date : #{end_date}"
end
=>


首先,计算两个日期之间的月数(由以下人员提供):

然后,从开始月份开始,然后每月计数,找到第一个/最后一个日期并附加到累加器数组中

dates = number_of_months.times.each_with_object([]) do |count, array|
  array << [start_date.beginning_of_month + count.months,
            start_date.end_of_month + count.months]
end
# => ...
这是在Rails 3.2.5/Ruby 1.9.3p194中测试和使用的

time\u start=13.6个月前
time_start = 13.months.ago
time_end = 0.seconds.ago
time = time_start
while time < time_end
  puts("m:#{time.month} y:#{time.year}")
  time = time.advance(months: 1)
end
时间=0.5秒前 时间=开始时间 当时间
从(包括)某些日期起的未来12个月的开始和结束:

(0..11).map do |m| 
  start_date = some_date.at_beginning_of_month + m.month
  end_date = start_date.at_end_of_month
end

您是否期望任意的开始/结束日期?是;这是ruby 1.8.7上的rails 3.0.10版本,谢谢-我认为这是最好的解释,也是最干净的实现方式(也调整了很多年)。
2011年12月30日,星期五
不是12月的最后一天。advance方法似乎已经不存在了。您可以使用
Date.next\u month
mydate>>1
提前一个月。请参阅或
time+=1.月
dates
# => [[Tue, 01 Nov 2011, Wed, 30 Nov 2011], [Thu, 01 Dec 2011, Fri, 30 Dec 2011], ...

dates[0].first
# => Tue, 01 Nov 2011

dates[0].last
# => Wed, 30 Nov 2011

dates[0].last.class
# => Date
time_start = 13.months.ago
time_end = 0.seconds.ago
time = time_start
while time < time_end
  puts("m:#{time.month} y:#{time.year}")
  time = time.advance(months: 1)
end
(start_date..end_date).select{|date| date.day == 1}.map{|date| [date.beginning_of_month, date.end_of_month]}
(0..11).map do |m| 
  start_date = some_date.at_beginning_of_month + m.month
  end_date = start_date.at_end_of_month
end