3

私のルビーコードでは、過去1年間の月名をすべて表示したいと考えています。たとえば、現在の日付はからまで"April 2013"のすべての月の名前を表示したいとします。これに最適なアイデアは何ですか?"May 2012""April 2013"["May", "June", .., "April"]

私は試した:

<%= (1.year.ago.to_date.strftime("%B")..Date.today.strftime("%B")).map %> { |a| a } %>

しかし、次のようになります["April"]

4

5 に答える 5

1

これを試して:

(0..11).each { |i|
  month = (Date.today.month + i) % 12 + 1;
  puts Date.new(Date.today.year - 1 + (month < Date.today.month ? 0 : 1), month).strftime("%B %Y")
}
于 2013-04-07T09:32:16.613 に答える