开发者

How to create an infinite enumerable of Times?

I want to be able to have an object extend Enumer开发者_JAVA百科able 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?


In 1.9 (and probably previous versions using backports), you can easily create enumerator:

require 'date'

def ndays_from(from, step=7)
  Enumerator.new {|y|
    loop {
      y.yield from
      from += step
    }
  }
end

e = ndays_from(Date.today)
p e.take(5)
#=> [#<Date: 2010-03-25 (4910561/2,0,2299161)>, #<Date: 2010-04-01 (4910575/2,0,2299161)>, #<Date: 2010-04-08 (4910589/2,0,2299161)>, #<Date: 2010-04-15 (4910603/2,0,2299161)>, #<Date: 2010-04-22 (4910617/2,0,2299161)>]


Store a Date as instance variable, initialized to a Monday. You would implement an each method which increments the stored date by 7 days using date += 7.


You could do something by extending Date...


#!/usr/bin/ruby

require 'date'

class Date
  def current_monday
    self - self.wday + 1
  end

  def next_monday
    self.current_monday + 7
  end
end

todays_date = Date.today
current_monday = todays_date.current_monday
3.times do |i|
  puts current_monday.to_s
  current_monday = current_monday.next_monday
end

2010-03-22
2010-03-29
2010-04-05
2010-04-12

...with the usual warnings about extending base classes of course.


You can extend Date class with nw method mondays

class Date
  def self.mondays(start_date=Date.today, count=10)
    monday = start_date.wday > 1 ? start_date - start_date.wday + 8 : start_date - start_date.wday + 1
    mondays = []
    count.times { |i| mondays << monday + i*7}
    mondays
  end
end

Date.mondays will return by default Array of mondays with 10 elements from closest monday to Date.today. You can pass parameters:

Date.mondays(start_date:Date, count:Integer)

start_date - start point to find closest monday count - number of mondays you are looking

IE:

Date.mondays(Date.parse('11.3.2002'))
Date.mondays(Date.parse('11.3.2002'), 30)


module LazyEnumerable
  extend Enumerable

  def select(&block)
    lazily_enumerate { |enum, value| enum.yield(value) if 
block.call(value) }
  end

  def map(&block)
    lazily_enumerate {|enum, value| enum.yield(block.call(value))}
  end

  def collect(&block)
    map(&block)
  end

  private

  def lazily_enumerate(&block)
    Enumerator.new do |enum|
      self.each do |value|
        block.call(enum, value)
      end
    end
  end
end

...........

class LazyInfiniteDays
  include LazyEnumerable

  attr_reader :day

  def self.day_of_week
    dow = { :sundays => 0, :mondays => 1, :tuesdays => 2, :wednesdays => 
3, :thursdays => 4, :fridays => 5, :saturdays => 6, :sundays => 7 }
    dow.default = -10
    dow
  end

  DAY_OF_WEEK = day_of_week()

  def advance_to_midnight_of_next_specified_day(day_sym)
    year = DateTime.now.year
    month = DateTime.now.month
    day_of_month = DateTime.now.day
    output_day = DateTime.civil(year, month, day_of_month)
    output_day += 1 until output_day.wday == DAY_OF_WEEK[day_sym]
    output_day
  end

  def initialize(day_sym)
    @day = advance_to_midnight_of_next_specified_day(day_sym)
  end

  def each
    day = @day.dup
    loop {
      yield day
      day += 7
    }
  end

  def ==(other)
    return false unless other.kind_of? LazyInfiniteDays
    @day.wday == other.day.wday
  end
end


Ruby 2.7 introduced Enumerator#produce for creating an infinite enumerator from any block, which results in a very elegant, very functional way of implementing the original problem:

irb(main):001:0> require 'date'
=> true
irb(main):002:0> puts Date.today
2022-09-23
=> nil
irb(main):003:0> Date.today.friday?
=> true
irb(main):004:0> future_mondays = Enumerator.produce { |date|
  date = (date || Date.today).succ
  date = date.succ until date.monday?
  date
}
=> #<Enumerator: #<Enumerator::Producer:0x00007fa4300b3070>:each>
irb(main):005:0> puts future_mondays.first(5)
2022-09-26
2022-10-03
2022-10-10
2022-10-17
2022-10-24
=> nil
irb(main):006:0> _
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜