You’re passing tens of thousands of arguments to a method and that’s just too many for Ruby to handle, causing a stack error.
Instead just pass the un-flattened, un-splatted mapped array to Hash.[]
, since it accepts that just fine and gives the same (correct) result (without a SystemStackError):
Hash[array.map { |s| [s.id, s] }]
As an aside, we can see the argument count is in fact the issue (rather than specifically Hash.[]
) with a simple test:
def f(*args); end
f(*(1..1000000).to_a) #<SystemStackError: stack level too deep>