Project

General

Profile

Feature #14550 » date-step-2.patch

ksss (Yuki Kurihara), 02/25/2018 08:35 AM

View differences:

ext/date/date_core.c
#define USE_PACK
static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p;
static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p, id_to, id_by;
static VALUE cDate, cDateTime;
static VALUE half_days_in_day, day_in_nanoseconds;
static double positive_inf, negative_inf;
......
/*
* call-seq:
* d.step(limit[, step=1]) -> enumerator
* d.step(limit[, step=1]){|date| ...} -> self
* d.step(limit[, step=1]) -> enumerator
* d.step(limit[, step=1]){|date| ...} -> self
* d.step(to: limit, by: step) -> enumerator
* d.step(to: limit, by: step) {|date| ...} -> self
*
* Iterates evaluation of the given block, which takes a date object.
* The limit should be a date object.
*
* Date.new(2001).step(Date.new(2001,-1,-1)).select{|d| d.sunday?}.size
* #=> 52
* Date.new(2001, 1, 1).step(to: Date.new(2001,-1,-1), by: 5).count
* #=> 73
*/
static VALUE
d_lite_step(int argc, VALUE *argv, VALUE self)
{
VALUE limit, step, date;
VALUE limit, step, hash, date;
rb_scan_args(argc, argv, "11", &limit, &step);
RETURN_ENUMERATOR(self, argc, argv);
if (argc < 2)
argc = rb_scan_args(argc, argv, "02:", &limit, &step, &hash);
if (!NIL_P(hash)) {
ID keys[2];
VALUE values[2];
keys[0] = id_to;
keys[1] = id_by;
rb_get_kwargs(hash, keys, 0, 2, values);
if (values[0] != Qundef) {
if (argc > 0) rb_raise(rb_eArgError, "to is given twice");
limit = values[0];
}
if (values[1] != Qundef) {
if (argc > 1) rb_raise(rb_eArgError, "step is given twice");
step = values[1];
}
}
if (NIL_P(step)) {
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;
switch (FIX2INT(f_cmp(step, INT2FIX(0)))) {
case -1:
......
date_s_test_unit_conv, 0);
de_define_singleton_method(cDate, "test_all", date_s_test_all, 0);
#endif
id_to = rb_intern("to");
id_by = rb_intern("by");
}
/*
test/date/test_date_arith.rb
end
assert_equal(4, i)
i = 0
p.step(q, by: 2) do
i += 1
end
assert_equal(4, i)
i = 0
p.step(to: q, by: 2) do
i += 1
end
assert_equal(4, i)
i = 0
p.step(q) do
i += 1
end
assert_equal(8, i)
i = 0
p.step(to: q) do
i += 1
end
assert_equal(8, i)
assert_raise(ArgumentError) { p.step(q, to: q) { } }
assert_raise(ArgumentError) { p.step(q, 1, by: 1) { } }
assert_raise(ArgumentError) { p.step(q, foo: nil) { } }
end
def test_step__noblock
p = Date.new(2001,1,14)
q = Date.new(2001,1,21)
e = p.step(q, 2)
assert_equal(4, e.to_a.size)
e = p.step(q, by: 2)
assert_equal(4, e.to_a.size)
e = p.step(to: q, by: 2)
assert_equal(4, e.to_a.size)
e = p.step(q)
assert_equal(8, e.to_a.size)
e = p.step(to: q)
assert_equal(8, e.to_a.size)
end
end
(2-2/2)