From: Aldric Giacomoni Date: 2009-12-04T05:48:01+09:00 Subject: Re: have a problem with the loops and variable Steve Wilhelm wrote: > 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? q, w = 1, 2 s = Array.new(w - q) { |i| i.zero? ? 0 : 1 } How's that? :) Otherwise, brute-forcing like that is really just using permutations.. So how about this? http://trevoke.net/blog/2008/12/20/lexicographic-permutations-in-ruby/ I didn't include explanations for the code, but you can check out the other blog if you'd like. It does the permutation and runs a block for each permutations. -- Posted via http://www.ruby-forum.com/.