Method: Date#step
- Defined in:
- ext/date_ext/date_ext.c
#step(target, step = 1) {|date| ... } ⇒ Date
Yields Date objects between the receiver and the target date (inclusive), with step integer days between each yielded date. step can be negative, in which case the dates are yielded in reverse chronological order. Returns self in all cases.
If target is equal to the receiver, yields self once regardless of step. It target is less than receiver and step is nonnegative, or target is greater than receiver and step is nonpositive, does not yield.
Date.civil(2009, 1, 2).step(Date.civil(2009, 1, 6), 2) do |date|
puts date
end
# Output:
# 2009-01-02
# 2009-01-04
# 2009-01-06
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 |
# File 'ext/date_ext/date_ext.c', line 2500
static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
rhrd_t *d, *n, *newd;
rhrdt_t *ndt;
long step, limit, current;
VALUE rlimit, new, rstep, klass;
Data_Get_Struct(self, rhrd_t, d);
RHR_FILL_JD(d)
switch(argc) {
case 1:
step = 1;
rstep = LONG2NUM(step);
break;
case 2:
rstep = argv[1];
step = NUM2LONG(rstep);
if (step == 0) {
rb_raise(rb_eArgError, "step can't be 0");
}
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments: %d for 2", argc);
break;
}
rlimit = argv[0];
klass = rb_obj_class(self);
#ifdef RUBY19
if (!rb_block_given_p()) {
return rb_funcall(self, rhrd_id_to_enum, 3, rhrd_sym_step, rlimit, rstep);
}
#else
rb_need_block();
#endif
if (RTEST(rb_obj_is_kind_of(rlimit, rb_cNumeric))) {
limit = NUM2LONG(rlimit);
} else if (RTEST((rb_obj_is_kind_of(rlimit, rhrdt_class)))) {
Data_Get_Struct(rlimit, rhrdt_t, ndt);
RHRDT_FILL_JD(ndt)
limit = ndt->jd;
} else if (RTEST((rb_obj_is_kind_of(rlimit, rhrd_class)))) {
Data_Get_Struct(rlimit, rhrd_t, n);
RHR_FILL_JD(n)
limit = n->jd;
} else {
rb_raise(rb_eTypeError, "expected numeric or date");
}
current = d->jd;
if (limit > current) {
if (step > 0) {
while(limit >= current) {
new = Data_Make_Struct(klass, rhrd_t, NULL, -1, newd);
newd->jd = current;
RHR_CHECK_JD(newd)
newd->flags = RHR_HAVE_JD;
current += step;
rb_yield(new);
}
}
} else if (limit < current) {
if (step < 0) {
while(limit <= current) {
new = Data_Make_Struct(klass, rhrd_t, NULL, -1, newd);
newd->jd = current;
RHR_CHECK_JD(newd)
newd->flags = RHR_HAVE_JD;
current += step;
rb_yield(new);
}
}
} else {
rb_yield(self);
}
return self;
}
|