Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Last active August 29, 2015 13:59
Show Gist options
  • Save cheeyeo/10591415 to your computer and use it in GitHub Desktop.
Save cheeyeo/10591415 to your computer and use it in GitHub Desktop.
Date range enumerator
# 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