From: Robert Klemme on 25 Mar 2010 06:02
2010/3/25 Rob Biedenharn <Rob(a)agileconsultingllc.com>:
> On Mar 24, 2010, at 11:35 PM, Alex Baranosky wrote:
>> I want to be able to have an object extend Enumerable in Ruby to be an
>> infinite list of Mondays (for example).
>> So it would yield: March 29, April 5, April 12...... etc
>> How can I implement this in Ruby?
> require 'date'
> class Mondays
> include Enumerable
> def initialize(starting=Date.today)
> @monday = starting
> @monday += 1 until @monday.wday == 1
> def succ
> @monday += 7
IMHO you are abusing #succ here since it is intended to return the
next element _of the same class_. Either you make it return self
which I would also consider not too safe because the instance does not
change or you change the design and move it to another class.
Here's my generic solution.
remember.guy do |as, often| as.you_can - without end
First | Prev |
Pages: 1 2
Prev: class instance variable visibility to nested modules...