From: Robert Klemme Date: 2010-02-02T01:40:41+09:00 Subject: Re: Creating Arrays Through a Loop 2010/2/1 Aldric Giacomoni : > Squawk Boxed wrote: >> I was wondering if it was possible to create a series of arrays through >> a loop.  This doesn't work (I think): >> >> for num in 1..10 >>   array_num = Array.new >> end > > It works fine, but it will always assign the new array to the same > variable. > > One idea is to create an array, and then add arrays as elements. > > main_array = Array.new > (1..10).times { main_array << Array.new } Either use (1..10).each or use 10.times - but (1..10).times won't work: irb(main):002:0> (1..10).times {|*a| p a} NoMethodError: undefined method `times' for 1..10:Range from (irb):2 from /opt/bin/irb19:12:in `
' Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/