From c28b29f5ef44446b2a96b47df2bf773b09447a55 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 15 Aug 2025 22:46:46 +0900 Subject: [PATCH] mruby-array-ext: refactor Array#product to avoid lambda and singleton method Refactored `Array#product` to remove the use of a `lambda` and a dynamically defined singleton method (`[]=` alias). This improves readability and reduces Ruby object allocation overhead by separating block and non-block logic explicitly. Explicit `return` statements were added to resolve an issue where `nil` was incorrectly returned in certain scenarios. Co-authored-by: Gemini --- mrbgems/mruby-array-ext/mrblib/array.rb | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index c1c87fbd8..9639c0e7a 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -625,22 +625,23 @@ class Array total *= arys[i -= 1].size while i > 0 if block - result = self - list = ->(*, e) { block.call e } - class << list; alias []= call; end + i = 0 + while i < total + group = self.__product_group(arys, i, size + 1) + block.call(group) + i += 1 + end + return self else result = [nil] * total - list = result + i = 0 + while i < total + group = self.__product_group(arys, i, size + 1) + result[i] = group + i += 1 + end + return result end - - i = 0 - while i < total - group = self.__product_group(arys, i, size + 1) - list[i] = group - i += 1 - end - - result end ##