instance method
Date#step
(::Date limit, ?::Integer step) { (::Date) -> untyped } -> ::Date
(::Date limit, ?::Integer step) -> ::Enumerator[::Date, ::Date]
step(limit, step = 1){|date| ... } → self
ext/date/date_core.c
View on GitHub ↗
static VALUE
d_lite_step(int argc, VALUE *argv, VALUE self)
{
VALUE limit, step, date;
int c;
rb_scan_args(argc, argv, "11", &limit, &step);
if (argc < 2)
step = INT2FIX(1);
#if 0
if (f_zero_p(step))
rb_raise(rb_eArgError, "step can't be 0");
#endif
RETURN_ENUMERATOR(self, argc, argv);
date = self;
c = f_cmp(step, INT2FIX(0));
if (c < 0) {
while (FIX2INT(d_lite_cmp(date, limit)) >= 0) {
rb_yield(date);
date = d_lite_plus(date, step);
}
}
else if (c == 0) {
while (1)
rb_yield(date);
}
else /* if (c > 0) */ {
while (FIX2INT(d_lite_cmp(date, limit)) <= 0) {
rb_yield(date);
date = d_lite_plus(date, step);
}
}
return self;
}
Calls the block with specified dates; returns self.
-
The first
dateisself. -
Each successive
dateisdate + step, wherestepis the numeric step size in days. -
The last date is the last one that is before or equal to
limit, which should be a Date object.
Example:
Example
limit = Date.new(2001, 12, 31) Date.new(2001).step(limit){|date| p date.to_s if date.mday == 31 }
Output:
Example
"2001-01-31" "2001-03-31" "2001-05-31" "2001-07-31" "2001-08-31" "2001-10-31" "2001-12-31"
Returns an Enumerator if no block is given.