Given an array literal, I would like to create a hash where the keys are the elements from the array and the values are arrays containing the other / remaining elements.
Input:
[1, 2, 3]
Output:
{1=>[2, 3], 2=>[1, 3], 3=>[1, 2]}
It's easy if I introduce a variable:
arr = [1, 2, 3]
arr.map { |i| [i, arr - [i]] }.to_h
But with an array literal, the only solution I could come up with involves instance_exec
or instance_eval
, which seems hackish:
[1, 2, 3].instance_exec { map { |i| [i, self - [i]] } }.to_h
Am I overlooking a built-in method or an obvious solution? group_by
, combination
, permutation
and partition
don't seem to help.