Class: Date
- Defined in:
- lib/epitools/core_ext/date.rb,
lib/epitools/core_ext/misc.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#find(direction = :backward, &block) ⇒ Object
(also: #search)
Returns an enumerator which searches through the calendar a day at a time, returning every date that match the block.
- #find_backwards(&block) ⇒ Object (also: #find_backward)
- #find_bidirectional(&block) ⇒ Object (also: #find_both, #search_both, #search_bidirectional)
- #find_forwards(&block) ⇒ Object (also: #find_forward)
- #inspect ⇒ Object
Class Method Details
Instance Method Details
#find(direction = :backward, &block) ⇒ Object Also known as: search
Returns an enumerator which searches through the calendar a day at a time, returning every date that match the block.
Examples:
>> Date.find(:forward) { year % 42 == 0 }
>> Date.find(:backward) { month == 1 }
>> Date.parse("1900-01-01").find_bidirectional { wednesday? }
>> Date.today.find_forward { wednesday? and day == 4 }.take(10)
(Note: the block doesn't need a parameter; every method you call is sent directly to the date object that's being evaluated)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/epitools/core_ext/date.rb', line 34 def find(direction=:backward, &block) raise ArgumentError.new("This method requires a block") unless block_given? Enumerator.new do |y| p = self loop do y << p if p.instance_eval(&block) case direction when :forward, :forwards p = p.next_day when :backward, :backwards p = p.prev_day else raise "Error: Unknown date search direction #{direction.inspect}" end end end end |
#find_backwards(&block) ⇒ Object Also known as: find_backward
61 62 63 |
# File 'lib/epitools/core_ext/date.rb', line 61 def find_backwards(&block) find(:backwards, &block) end |
#find_bidirectional(&block) ⇒ Object Also known as: find_both, search_both, search_bidirectional
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/epitools/core_ext/date.rb', line 65 def find_bidirectional(&block) f = find(:forward, &block) b = find(:backward, &block) Enumerator.new do |y| loop do y << b.next y << f.next end end end |
#find_forwards(&block) ⇒ Object Also known as: find_forward
57 58 59 |
# File 'lib/epitools/core_ext/date.rb', line 57 def find_forwards(&block) find(:forwards, &block) end |
#inspect ⇒ Object
176 177 178 |
# File 'lib/epitools/core_ext/misc.rb', line 176 def inspect strftime("#<Date: %Y-%m-%d (%a %b %d, %Y)>") end |