# There has got to be a better way

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?

@tournament.rounds = Array.new(4) { Round.new }

@tournaments.rounds.inject(8) do |size, round|
round.matches = Array.new(size) { Match.new }
size / 2
end

Something like that?

On 7/26/06, Michael B. [email protected] wrote:

thoughts/suggestions?

Posted via http://www.ruby-forum.com/.

a = [8,4,2,1]
@tournament.rounds = 0.upto(3).inject([]) { |round, index|
Round.new.matches=
Array.new( a[index] ) {Match.new} }

I think that should provide the same thing that you have.

@t.rounds = Array.new(4) { Round.new }
@t.rounds.each_index do |i|
@t.rounds[@t.rounds.length - 1 - i] = Array.new(1 << i) {
Match.new }
end
?

On 25/07/06, Michael B. [email protected] wrote:

i’m looking to simplify the below…

@tournament.rounds = Array.new(4) { Round.new }

@tournament.rounds.each_with_index do |round,roundNumber|
round.matches = Array.new(2**(@tournamet.rounds.length - 1 -
roundNumber)) {Match.new}
end

On Wed, 26 Jul 2006, Michael B. wrote:

Posted via http://www.ruby-forum.com/.

this may work for your needs

`````` harp:~ > cat a.rb
class Match
def initialize(idx) @idx = idx end
def inspect() "#{ self.class } <#{ @idx }>" end
end

class Round < ::Array
def self.new(*a,&b) super(*a){|*_| Match.new *_ } end
end

class Tournament < ::Array
end

def Match(*a,&b) Match.new(*a,&b); end
def Round(*a,&b) Round.new(*a,&b); end
def Tournament(*a,&b) Tournament.new(*a,&b); end

tournament = Tournament[ Round(8), Round(4), Round(2), Round(1) ]

p tournament[0]
p tournament[1]
p tournament[2]
p tournament[3]

p tournament[0][7]
p tournament[3][0]

harp:~ > ruby a.rb
[Match <0>, Match <1>, Match <2>, Match <3>, Match <4>, Match <5>,
``````

Match <6>, Match <7>]
[Match <0>, Match <1>, Match <2>, Match <3>]
[Match <0>, Match <1>]
[Match <0>]
Match <7>
Match <0>

regards.

-a

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
@rr = Array.new(4) {R.new}
@rr.each_with_index {|r, i| r.mm = Array.new(1 << i) {M.new}}
@rr.reverse!
# **** 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 8:06, Michael B. 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
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.