How to flatten the nested for loops?
In your example you iterate over the same collection. In that case I'd write:

[1, 2, 3].repeated_permutation(4).each do |n1, n2, n3, n4|
  if n1 == 1 &amp;&amp; n2 == 2 &amp;&amp; n3 == 3 &amp;&amp; n3 == n4
    p [n1, n2, n3, n4]
  end
end


If instead you need to iterate over different collections, check Enumerable#product. 

Of course, any of these alternatives will perform worse than your original code, since you drop whole groups of permutations when some condition is not satisfied. 

Note that you could create a compact abstraction as efficient as your original code (it would take collections to iterate and conditions -with procs?- over them), but this won't pay off if you are going to use it only once.