I would write:
(0..100).flat_map { |x| (0..100-x).map { |y| [x, y, 100-xy] } }
Site Note 1: This is a classic example in which the list shines (and even more if there is some condition). Since Ruby does not have LC, we have to do a typical conversion to OOP: N-1 flat_map + 1 map . It would be great to have LC in Ruby (check this feature request ), Scala proved that even a clean OOP language is very beneficial from this syntactic sugar (although I can understand that the prevention from the developers is due to the implicit iterable protocol / method). On an imaginary Ruby that supported them, you should write:
[[x, y, 100-xy] for x in 0..100 for y in 0..100-x] # imaginary Ruby
Side Note 2: Imagine you prefer a less time-consuming solution (you probably don't need the whole array). For a lazy solution with Ruby 2.0, you just need to add a couple of proxies [lazy][2] :
(0..100).lazy.flat_map { |x| (0..100-x).lazy.map { |y| [x, y, 100-xy] } }
Side Note 3: For completeness only, in @akuhn's answer line, another lazy solution using counters:
Enumerator.new do |e| (0..100).each { |x| (0..100-x).each { |y| e.yield([x, y, 100-xy]) } } end
source share