Use defined?(yield) and SIZED_ENUMERATOR

Prefer built-in features over method calls that may be overridden.
This commit is contained in:
Nobuyoshi Nakada 2024-02-17 23:28:00 +09:00
parent df63e5bef6
commit 3dccb716da
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
5 changed files with 19 additions and 7 deletions

View File

@ -44,7 +44,7 @@ class Array
def each def each
Primitive.attr! :inline_block Primitive.attr! :inline_block
unless defined?(yield) unless defined?(yield)
return to_enum(:each) { self.length } return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end end
_i = 0 _i = 0
value = nil value = nil

View File

@ -129,7 +129,7 @@ module Kernel
# #
def then def then
Primitive.attr! :inline_block Primitive.attr! :inline_block
unless block_given? unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)' return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end end
yield(self) yield(self)
@ -145,7 +145,7 @@ module Kernel
# #
def yield_self def yield_self
Primitive.attr! :inline_block Primitive.attr! :inline_block
unless block_given? unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)' return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end end
yield(self) yield(self)
@ -182,8 +182,8 @@ module Kernel
# } #=> :ok # } #=> :ok
def loop def loop
Primitive.attr! :inline_block Primitive.attr! :inline_block
unless block_given? unless defined?(yield)
return enum_for(:loop) { Float::INFINITY } return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size)'
end end
begin begin

View File

@ -5664,6 +5664,12 @@ int_downto(VALUE from, VALUE to)
return from; return from;
} }
static VALUE
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
{
return int_neg_p(num) ? INT2FIX(0) : num;
}
/* /*
* call-seq: * call-seq:
* round(ndigits= 0, half: :up) -> integer * round(ndigits= 0, half: :up) -> integer

View File

@ -230,8 +230,8 @@ class Integer
# With no block given, returns an Enumerator. # With no block given, returns an Enumerator.
def times def times
Primitive.attr! :inline_block Primitive.attr! :inline_block
unless block_given? unless defined?(yield)
return to_enum(:times) { self < 0 ? 0 : self } return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, int_dotimes_size)'
end end
i = 0 i = 0
while i < self while i < self

View File

@ -3988,6 +3988,12 @@ f_sprintf(int c, const VALUE *v, VALUE _)
return rb_f_sprintf(c, v); return rb_f_sprintf(c, v);
} }
static VALUE
rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
{
return DBL2NUM(HUGE_VAL);
}
/* /*
* Document-class: Class * Document-class: Class
* *