From: James Edward Gray II Date: 2005-06-19T05:01:48+09:00 Subject: Re: Newbe questions... On Jun 18, 2005, at 2:50 PM, Chuck Brotman wrote: > In Ruby Is there a prefered (or otherwise elegant) way to do an > inner & > outer loop with two variables? > > eg in psedocode: > > for i=1 to 3 > {for j = 4 to 6 > print i,j, i*j,\nl} I don't personally have a problem with the way you show it right there. I'm not aware of a significantly better way. > would print something like > 1,4,4 > 1,5,5 > 1,6,6 > 2,4,8 > 2,5,10 > 2,6,12 > 3,4,12 > 3,5,15 > 3,6,18 > > Also,how would you generate the same output using the each iterator? irb(main):007:0> (1..3).each do |i| irb(main):008:1* (4..6).each do |j| irb(main):009:2* puts "#{i} * #{j} = #{i * j}" irb(main):010:2> end irb(main):011:1> end 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 => 1..3 Hope that helps. James Edward Gray II