Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created March 25, 2010 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rklemme/343387 to your computer and use it in GitHub Desktop.
Save rklemme/343387 to your computer and use it in GitHub Desktop.
#! 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