[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

there has got to be a better way...

Michael Barinek

7/25/2006 3:07:00 PM

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-....

6 Answers

Brad Phelan

7/25/2006 3:14:00 PM

0

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?
>

for i in 0..3
@tournament.rounds[i] = Array.new( 1 << ( 3 - i ) ) { Match. new }
end

perhapps.

--
Brad Phelan
http://xt...

Matthew Harris

7/25/2006 3:20:00 PM

0

@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 Barinek <barinek@gmail.com> 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-....
>
>


--
Matt

Farrel Lifson

7/25/2006 3:34:00 PM

0

On 25/07/06, Michael Barinek <barinek@gmail.com> 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

Ara.T.Howard

7/25/2006 3:41:00 PM

0

Morton Goldberg

7/25/2006 8:49:00 PM

0

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-....
>


Dave Howell

7/26/2006 6:21:00 AM

0


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