Created
March 25, 2010 10:01
-
-
Save rklemme/343387 to your computer and use it in GitHub Desktop.
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
#! ruby19 | |
require 'date' | |
class EndlessEnum | |
include Enumerable | |
def initialize(x, method = :succ) | |
@x = x | |
@method = method | |
end | |
# They never come back! | |
def each | |
y = @x | |
loop do | |
yield y | |
y = y.send @method | |
end | |
self | |
end | |
end | |
class Weekday | |
attr_reader :date | |
def initialize(wd, start = Date.today) | |
wd = wd.to_s | |
@wd = /\?\z/ =~ wd ? wd : "#{wd}?" | |
until start.send(@wd) | |
start = start.succ | |
end | |
@date = start | |
end | |
def succ | |
self.class.new(@wd, @date + 1) | |
end | |
end | |
i = 0 | |
EndlessEnum.new(Weekday.new(:monday)).each do |mon| | |
printf "%p %s monday? %p\n", mon, mon.date, mon.date.monday? | |
i += 1 | |
break if i >= 10 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment