mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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
|
||||
|
||||
##
|
||||
|
||||
Reference in New Issue
Block a user