Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#! 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