尝试查找 Ruby 中每个月最后一天的星期六数量
Trying to find amount of Saturdays that fall on the last day of each month in Ruby
我正在处理一个非常具体的问题。我需要找出五年内每个月最后一天星期六的次数。
对于这个例子,我需要找到从 1/1/1990 到 1/1/1995 的星期六。
我已经尝试过以下代码。
require 'active_support/core_ext'
require 'date'
beginDate = Date.parse("1/1/1990")
endDate = Date.parse("1/1/1995")
saturdays = beginDate.upto(endDate).count(&:saturday?) && beginDate.upto(endDate).count.end_of_month
puts saturdays
有没有人有什么建议或指点?
移动设备,因此无法测试,但类似于 (0..11).map.with_index { |i| date = Date.today + i.months; date.beginning_of_month if date.end_of_month.saturday? }.reject(&:nil?)
你不需要任何 ruby-on-rails crap here in the first place. Plain ruby 就可以了。
require 'date'
beginDate, endDate =
Date.parse("1/1/1990"), Date.parse("1/1/1995")
(beginDate..endDate).count do |d|
d.saturday? && d.month != (d + 1).month
end
#⇒ 9
我正在处理一个非常具体的问题。我需要找出五年内每个月最后一天星期六的次数。
对于这个例子,我需要找到从 1/1/1990 到 1/1/1995 的星期六。
我已经尝试过以下代码。
require 'active_support/core_ext'
require 'date'
beginDate = Date.parse("1/1/1990")
endDate = Date.parse("1/1/1995")
saturdays = beginDate.upto(endDate).count(&:saturday?) && beginDate.upto(endDate).count.end_of_month
puts saturdays
有没有人有什么建议或指点?
移动设备,因此无法测试,但类似于 (0..11).map.with_index { |i| date = Date.today + i.months; date.beginning_of_month if date.end_of_month.saturday? }.reject(&:nil?)
你不需要任何 ruby-on-rails crap here in the first place. Plain ruby 就可以了。
require 'date'
beginDate, endDate =
Date.parse("1/1/1990"), Date.parse("1/1/1995")
(beginDate..endDate).count do |d|
d.saturday? && d.month != (d + 1).month
end
#⇒ 9