Last active
August 29, 2015 13:59
-
-
Save cheeyeo/10591415 to your computer and use it in GitHub Desktop.
Date range enumerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# dr = DateRange.new(Date.today) | |
# dr.each{|d| puts d} | |
# enum = dr.lazy | |
# enum.next | |
# enum.find_all{|d| d.cwday < 5 } # returns all | |
# enum.detect{|d| d.cwday < 3 } # only returns the first instance | |
require 'date' | |
class DateRange | |
include Enumerable | |
def initialize(from, step=7) | |
end_date = from + step | |
@range = (from...end_date) | |
end | |
# The class must provide a method each, which yields successive members of the collection | |
def each | |
@range.each{|e| yield(e)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment