Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use defined?(yield) and SIZED_ENUMERATOR #10007

Merged
merged 1 commit into from Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion array.rb
Expand Up @@ -44,7 +44,7 @@ class Array
def each
Primitive.attr! :inline_block
unless defined?(yield)
return to_enum(:each) { self.length }
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
end
_i = 0
value = nil
Expand Down
8 changes: 4 additions & 4 deletions kernel.rb
Expand Up @@ -129,7 +129,7 @@ def tap
#
def then
Primitive.attr! :inline_block
unless block_given?
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end
yield(self)
Expand All @@ -145,7 +145,7 @@ def then
#
def yield_self
Primitive.attr! :inline_block
unless block_given?
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end
yield(self)
Expand Down Expand Up @@ -182,8 +182,8 @@ def yield_self
# } #=> :ok
def loop
Primitive.attr! :inline_block
unless block_given?
return enum_for(:loop) { Float::INFINITY }
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size)'
end

begin
Expand Down
6 changes: 6 additions & 0 deletions numeric.c
Expand Up @@ -5664,6 +5664,12 @@ int_downto(VALUE from, VALUE to)
return from;
}

static VALUE
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
{
return int_neg_p(num) ? INT2FIX(0) : num;
}

/*
* call-seq:
* round(ndigits= 0, half: :up) -> integer
Expand Down
4 changes: 2 additions & 2 deletions numeric.rb
Expand Up @@ -230,8 +230,8 @@ def size
# With no block given, returns an Enumerator.
def times
Primitive.attr! :inline_block
unless block_given?
return to_enum(:times) { self < 0 ? 0 : self }
unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, int_dotimes_size)'
end
i = 0
while i < self
Expand Down
6 changes: 6 additions & 0 deletions object.c
Expand Up @@ -3988,6 +3988,12 @@ f_sprintf(int c, const VALUE *v, VALUE _)
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
*
Expand Down