Ruby 使用splat运算符分解数组时发生SystemStackError
我有一个应用程序,它将大量数据收集到一个数组中,并将其附加到现有数组中。当我使用splat操作符(用于Ruby 使用splat运算符分解数组时发生SystemStackError,ruby,Ruby,我有一个应用程序,它将大量数据收集到一个数组中,并将其附加到现有数组中。当我使用splat操作符(用于Array.push)时,会收到一条SystemStackError:stack level too deep消息“大”在150k条目的范围内(每个条目包含其他对象) 在Ruby中合并大型数组的首选方法是什么 gathered_info = function_that_returns_a_large_array_of_hashes() dump.push(*gathered_info) 如果要
Array.push
)时,会收到一条SystemStackError:stack level too deep
消息“大”在150k条目的范围内(每个条目包含其他对象)
在Ruby中合并大型数组的首选方法是什么
gathered_info = function_that_returns_a_large_array_of_hashes()
dump.push(*gathered_info)
如果要向数组中添加一组内容,则splat需要将这些内容扩展为单个参数,每个参数占用堆栈空间。因为你已经发现的原因,这对于大列表是不好的 您始终可以直接在阵列上使用:
dump.concat(gathered_info)
那就不那么麻烦了
您通常使用splat,因为没有替代方法可以使用数组,但这里的情况并非如此concat
完全满足您的需要