我希望在Rails种子中获得从开始日期到结束日期的日期范围。 当我尝试生成日期范围((Date.today - 10)..Date.today)时,异常发生了。异常信息:bad value for range。但在Rails控制台中一切都正常。我认为ActiveSupport对此是合理的(我的调试器告诉我)。Rails版本为3.1.3。怎么回事?
Date.today.class # => Date
(Date.today - 10).class # => Date
((Date.today - 10)..Date.today).each {|d| puts d.class} # => 10 Date works for me
('a'..10) # => ArgumentError: bad value for range
(Date.today - 10).class => ?
Date.today.class => ?
((Date.today - 10).to_date..Date.today.to_date)
。 - ecoologicbegin
((Date.today - 10)..Date.today).each { |date| puts date }
rescue
$! # => #<NameError: uninitialized constant Date>
end
require 'date'
((Date.today - 10)..Date.today).each { |date| puts date }
# >> 2012-04-06
# >> 2012-04-07
# >> 2012-04-08
# >> 2012-04-09
# >> 2012-04-10
# >> 2012-04-11
# >> 2012-04-12
# >> 2012-04-13
# >> 2012-04-14
# >> 2012-04-15
# >> 2012-04-16