From: Dave Howell Date: 2006-07-26T15:21:24+09:00 Subject: Re: there has got to be a better way... On Jul 25, 2006, at 8:06, 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? I'm not a big proponent of trying to do everything in one line, because it often results in code that's hard to figure out when you go back to look at it later. So I was a little surprised when this turned into one line of active code. :) class Match def initialize # put whatever's needed here, of course end end class Tournament attr_reader :rounds def initialize(num_of_rounds) @rounds = (1..num_of_rounds).collect do |round_number| Array.new(2**(num_of_rounds-round_number)) {|i| Match.new} end end end @tournament = Tournament.new(4) @tournament.rounds.each_with_index {|round, round_num| puts "Number of matches in round " + (round_num + 1).to_s + " is " + round.size.to_s} Number of matches in round 1 is 8 Number of matches in round 2 is 4 Number of matches in round 3 is 2 Number of matches in round 4 is 1