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 <gemini@google.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-15 22:46:46 +09:00
parent ad2757d6b5
commit c28b29f5ef
+14 -13
View File
@@ -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
##