Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如,我需要从 2013 年 1 月 1 日开始,并为每个日期“做一些事情”,从而为每个日期生成一个 JSON 文件。
我已经为一个日期制定了“做一些事情”部分,但我很难从一个日期开始并循环到另一个结束日期。
您可以使用范围:
(Date.new(2012, 01, 01)..Date.new(2012, 01, 30)).each do |date| # Do stuff with date end
或(见@awendt 答案)
Date.new(2012, 01, 01).upto(Date.new(2012, 01, 30)) do |date| # Do stuff with date end
你可以使用:
first.upto(last) do |date|
wherefirst和last是 Date 对象。
first
last
例如,看看我在我的一个项目中做了什么。