From: Morton Goldberg Date: 2006-07-26T05:49:07+09:00 Subject: Re: there has got to be a better way... A small variant on answers already posted -- use reverse! to avoid index twiddling. Regards, Morton --------------- start of code ! /usr/bin/ruby -w class M attr_accessor :ii # **** support for pretty printing def to_s "M.#@ii" end end class R attr_accessor :mm, :ii # **** support for pretty printing def to_s s = (mm.collect {|m| m.to_s}).join(", ") "[R#@ii: #{s}]" end end class T def initialize # **** start of answer @rr = Array.new(4) {R.new} @rr.each_with_index {|r, i| r.mm = Array.new(1 << i) {M.new}} @rr.reverse! # **** end of answer # **** support for pretty printing tag = 1 @rr.each_with_index do |r, i| r.ii = i + 1 r.mm.each do |m| m.ii = tag tag += 1 end end end # **** support for pretty printing def to_s s = (@rr.collect {|r| r.to_s}).join(", ") "[T: #{s}]" end end t = T.new puts t => [T: [R1: M.1, M.2, M.3, M.4, M.5, M.6, M.7, M.8], \ [R2: M.9, M.10, M.11, M.12], [R3: M.13, M.14], \ [R4: M.15]] --------------- end of code On Jul 25, 2006, at 11:06 AM, Michael Barinek wrote: > i'm looking to simplify the below... > > @tournament.rounds = Array.new(4) { Round.new } > > @tournament.rounds[0].matches = Array.new(8) { Match.new } > @tournament.rounds[1].matches = Array.new(4) { Match.new } > @tournament.rounds[2].matches = Array.new(2) { Match.new } > @tournament.rounds[3].matches = Array.new(1) { Match.new } > > thoughts/suggestions? > > -- > Posted via http://www.ruby-forum.com/. >