From: Steve Wilhelm Date: 2009-12-04T05:06:54+09:00 Subject: Re: have a problem with the loops and variable Marnen Laibow-Koser wrote: > Steve Wilhelm wrote: >> >> Marnen states: >> >> "Note that it would be far more idiomatic to use s.each. Never use += >> to step through subscripts in Ruby." >> >> An example based on Sajjad's original code would be helpful. > > Then write one! :) If you're not sure how, do what you can and post it > here. I'm sure I or someone else will be happy to look over it. > >> Thanks in >> advance. >> >> - Steve W. > > Best, > --  > Marnen Laibow-Koser > http://www.marnen.org > marnen@marnen.org Okay, Sajjad's original code was q = 1 w = 2 while q < w s[q] = 1 puts s[q] q += 1 end It was suggested to use s.each. The problem as I see it is that Sajjad is trying to populate the array s with ones. I think what Sajjad wanted was: q, w = 1, 2 s = Array.new(w - q) { 1 } It doesn't quite do what the original code attempts, because Sajjad's code starts at array index 1 instead of 0. I do know know if that is what he actually intended or if it is another bug. Is there a better way to populate an array with default values using array's each method? -- Posted via http://www.ruby-forum.com/.