From: Toi Toi Date: 2009-10-19T06:46:26+09:00 Subject: Re: Dynamic nested each in ruby 1.8.7? That function does not give the correct output for larger arrays. The level functions can be generated with eval, see the code below. However there must be a better Ruby way. def calc level tmp = ["def calcd nums\ntmp=[]\n"] str = "n1" num = 1 level.times { tmp.push "nums.each{ |n#{num}| tmp.push #{str}\n" str = str+" + n#{num+=1}" } level.times { tmp.push "}"} tmp.push "\ntmp\n" tmp.push "end" tmp.to_s end array, times = ('1'..'10').to_a, 3 eval(calc(times)) p calcd(array) Harry Kakueki wrote: > On Sun, Oct 18, 2009 at 11:43 AM, Toi Toi wrote: >> }}} >> tmp >> end >> >> puts calc(["0", "1"], 3) >> -- > > Does this give you the right output? > No recursion, but... > > def calc(n, v) > h,t = Hash[*(0...n.size).zip(n).flatten],[] > (1..v).each{|x| (0...n.size**x).each{|y| t << > y.to_s(n.size).rjust(x,"0")}} > t.map{|a| a.split(//).map{|b| h[b.to_i]}.join}.sort > end > > p calc(["0","1"], 3) > > > Harry -- Posted via http://www.ruby-forum.com/.