From: Robert Klemme Date: 2007-04-07T16:00:04+09:00 Subject: Re: Newbie question - ruby equivalence of for loop? On 07.04.2007 00:21, dwyer@o2.ie wrote: > is there an equivalence in Ruby for the nested "for loop" in java. > i want to complete the following: > i is 0 > j is 2 > j is 4 > j is 6 > j is 8 > i is 1 > j is 2 > j is 4 > j is 6 > j is 8 > i is 2 > j is 2 > j is 4 > j is 6 > j is 8 > but want to use Ruby,(i think Rails has a foreach loop).I can do the above in Java > but Ruby is still foreign to me.Whats the best method to implement the above? irb(main):005:0> 0.upto 2 do |i| irb(main):006:1* 2.step 8, 2 do |j| irb(main):007:2* print i, " ", j, "\n" irb(main):008:2> end irb(main):009:1> end 0 2 0 4 0 6 0 8 1 2 1 4 1 6 1 8 2 2 2 4 2 6 2 8 => 0 You can also use a for loop for the outer loop: irb(main):010:0> for i in 0..2 do irb(main):011:1* puts i irb(main):012:1> end 0 1 2 => 0..2 The scoping is a bit different, i.e. i is known after the loop. Kind regards robert