Input hash:{ 1 => [2 .. 7], 2 => [1], 3 => [1], 4 => [1], 5 => [1], 6 => [1], 7 => [1], 8 => [9], 9 => [8] } Try first then each twice in a row to check that the iteration is restarted ([8, [9]], [5, [1]]) ([8, [9]], [5, [1]]) Starting from 8 -> [ 9 ] Removing 8 -> [ 9 ] Removing 9 -> [ 8 ] Starting from 5 -> [ 1 ] Removing 5 -> [ 1 ] Removing 1 -> [ 2 3 4 5 6 7 ] Removing 2 -> [ 1 ] Removing 3 -> [ 1 ] Removing 4 -> [ 1 ] Removing 6 -> [ 1 ] Removing 7 -> [ 1 ]