Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 在Rails中获取最近X个月的名称_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 在Rails中获取最近X个月的名称

Ruby on rails 在Rails中获取最近X个月的名称,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试获取过去6个月的月份名称数组。我看到过其他帖子,其中提到了迭代和打印实际日期7/1/16、7/2/16等,但没有一篇文章只涉及月份名称: [“二月”、“三月”、“四月”、“五月”、“六月”、“七月”] 我尝试了以下代码,但出现了以下错误: @array = [] (6.months.ago..Time.now).each do |m| @array.push(Date::MONTHNAMES[m]) end TypeError: can't iterate from Act

我正在尝试获取过去6个月的月份名称数组。我看到过其他帖子,其中提到了迭代和打印实际日期7/1/16、7/2/16等,但没有一篇文章只涉及月份名称:

[“二月”、“三月”、“四月”、“五月”、“六月”、“七月”]

我尝试了以下代码,但出现了以下错误:

@array = []
(6.months.ago..Time.now).each do |m|
  @array.push(Date::MONTHNAMES[m])  
end 

TypeError: can't iterate from ActiveSupport::TimeWithZone

一种简单但有点幼稚的方法是,对整数进行迭代,计算它的月份,然后在数组中查找它

5.downto(0).collect do |n| 
  Date::MONTHNAMES[n.months.ago.month]
end
这会回来的
[“二月”、“三月”、“四月”、“五月”、“六月”、“七月”]
(这是在七月执行的)

一种简单但有点幼稚的方法是对整数进行迭代,计算它属于哪个月,最后在数组中查找它

5.downto(0).collect do |n| 
  Date::MONTHNAMES[n.months.ago.month]
end
这会回来的
[“2月”、“3月”、“4月”、“5月”、“6月”、“7月”]
(这是在7月执行的)

这是一个稍微丑陋的版本,它建立在这个版本的基础上,但不需要每月查找日期,速度大约快31倍:

current_month = Date.today.month

month_names = 6.downto(1).map { |n| DateTime::MONTHNAMES.drop(1)[(current_month - n) % 12] }
当前月<代码>为4时的输出(用于测试12-1月滚动):

基准:

Benchmark.measure do
  10000.times do
    current_month = Date.today.month
    month_names = 6.downto(1).map { |n| DateTime::MONTHNAMES.drop(1)[(current_month - n) % 12] }
  end
end
=> #<Benchmark::Tms:0x007fcfda4830d0 @label="", @real=0.12975036300485954, @cstime=0.0, @cutime=0.0, @stime=0.07000000000000006, @utime=0.06999999999999984, @total=0.1399999999999999> 
Benchmark.measure do
一万倍
当前月份=日期.今天.月份
month_names=6.downto(1).map{n|DateTime::MONTHNAMES.drop(1)[(当前月份-n)%12]]
结束
结束
=> # 
与更干净的版本相比:

Benchmark.measure do
  10000.times do
    5.downto(0).collect do |n| 
        Date::MONTHNAMES[n.months.ago.month]
    end
  end
end
=> #<Benchmark::Tms:0x007fcfdcbde9b8 @label="", @real=3.7730263769917656, @cstime=0.0, @cutime=0.0, @stime=0.04999999999999993, @utime=3.69, @total=3.7399999999999998> 
Benchmark.measure do
一万倍
5.下到(0)。收集do|n|
日期:MONTHNAMES[n.months.ago.month]
结束
结束
结束
=> # 

这是一个稍显丑陋的版本,基于此构建,但不需要每月查找日期,速度大约快31倍:

current_month = Date.today.month

month_names = 6.downto(1).map { |n| DateTime::MONTHNAMES.drop(1)[(current_month - n) % 12] }
当前月<代码>为4时的输出(用于测试12-1月滚动):

基准:

Benchmark.measure do
  10000.times do
    current_month = Date.today.month
    month_names = 6.downto(1).map { |n| DateTime::MONTHNAMES.drop(1)[(current_month - n) % 12] }
  end
end
=> #<Benchmark::Tms:0x007fcfda4830d0 @label="", @real=0.12975036300485954, @cstime=0.0, @cutime=0.0, @stime=0.07000000000000006, @utime=0.06999999999999984, @total=0.1399999999999999> 
Benchmark.measure do
一万倍
当前月份=日期.今天.月份
month_names=6.downto(1).map{n|DateTime::MONTHNAMES.drop(1)[(当前月份-n)%12]]
结束
结束
=> # 
与更干净的版本相比:

Benchmark.measure do
  10000.times do
    5.downto(0).collect do |n| 
        Date::MONTHNAMES[n.months.ago.month]
    end
  end
end
=> #<Benchmark::Tms:0x007fcfdcbde9b8 @label="", @real=3.7730263769917656, @cstime=0.0, @cutime=0.0, @stime=0.04999999999999993, @utime=3.69, @total=3.7399999999999998> 
Benchmark.measure do
一万倍
5.下到(0)。收集do|n|
日期:MONTHNAMES[n.months.ago.month]
结束
结束
结束
=> # 

以下版本将以3个字符的月份格式显示:

5.downto(0).collect do |n| 
  Date.parse(Date::MONTHNAMES[n.months.ago.month]).strftime('%b')
end
输出:(考虑一月为当月)

[“八月”、“九月”、“十月”、“十一月”、“十二月”、“一月”]


以下版本将以3个字符的月份格式显示:

5.downto(0).collect do |n| 
  Date.parse(Date::MONTHNAMES[n.months.ago.month]).strftime('%b')
end
输出:(考虑一月为当月)

[“八月”、“九月”、“十月”、“十一月”、“十二月”、“一月”]