mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Rewrite Integer#times in Ruby (#8388)
This commit is contained in:
parent
4efcaf956e
commit
5b5ae3d9e0
Notes:
git
2023-09-07 17:58:12 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>
6 changed files with 53 additions and 72 deletions
53
numeric.c
53
numeric.c
|
@ -5652,58 +5652,6 @@ int_downto(VALUE from, VALUE to)
|
|||
return from;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
|
||||
{
|
||||
if (FIXNUM_P(num)) {
|
||||
if (NUM2LONG(num) <= 0) return INT2FIX(0);
|
||||
}
|
||||
else {
|
||||
if (RTEST(rb_funcall(num, '<', 1, INT2FIX(0)))) return INT2FIX(0);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* times {|i| ... } -> self
|
||||
* times -> enumerator
|
||||
*
|
||||
* Calls the given block +self+ times with each integer in <tt>(0..self-1)</tt>:
|
||||
*
|
||||
* a = []
|
||||
* 5.times {|i| a.push(i) } # => 5
|
||||
* a # => [0, 1, 2, 3, 4]
|
||||
*
|
||||
* With no block given, returns an Enumerator.
|
||||
*
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
int_dotimes(VALUE num)
|
||||
{
|
||||
RETURN_SIZED_ENUMERATOR(num, 0, 0, int_dotimes_size);
|
||||
|
||||
if (FIXNUM_P(num)) {
|
||||
long i, end;
|
||||
|
||||
end = FIX2LONG(num);
|
||||
for (i=0; i<end; i++) {
|
||||
rb_yield_1(LONG2FIX(i));
|
||||
}
|
||||
}
|
||||
else {
|
||||
VALUE i = INT2FIX(0);
|
||||
|
||||
for (;;) {
|
||||
if (!RTEST(int_le(i, num))) break;
|
||||
rb_yield(i);
|
||||
i = rb_int_plus(i, INT2FIX(1));
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* round(ndigits= 0, half: :up) -> integer
|
||||
|
@ -6243,7 +6191,6 @@ Init_Numeric(void)
|
|||
rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
|
||||
rb_define_method(rb_cInteger, "upto", int_upto, 1);
|
||||
rb_define_method(rb_cInteger, "downto", int_downto, 1);
|
||||
rb_define_method(rb_cInteger, "times", int_dotimes, 0);
|
||||
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
||||
rb_define_method(rb_cInteger, "next", int_succ, 0);
|
||||
rb_define_method(rb_cInteger, "pred", int_pred, 0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue