Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp me refactor my World Cup Challenge Script
    text
    copied!<p>I am setting up a World Cup Challenge between some friends, and decided to practice my Ruby and write a small script to automate the process. </p> <p><strong>The Problem:</strong> </p> <ul> <li>32 World Cup qualifiers split into 4 tiers by their Fifa ranking</li> <li>8 entries</li> <li>Each entry is assigned 1 random team per tier</li> <li>Winner takes all :-)</li> </ul> <p>I wrote something that suffices yet is admittedly brute force. But, in my attempt to improve my Ruby, I acknowlege that this code isn't the most elegant solution around - So I turn to you, the experts, to show me the way. </p> <p>It may be more clear to check out this gist - <a href="https://gist.github.com/91e1f1c392bed8074531" rel="nofollow noreferrer">https://gist.github.com/91e1f1c392bed8074531</a></p> <p><strong>My Current (poor) solution</strong>:</p> <pre><code>require 'yaml' @teams = YAML::load(File.open('teams.yaml')) @players = %w[Player1 Player2 Player3 Player4 Player5 Player6 Player7 Player8] results = Hash.new players = @players.sort_by{rand} players.each_with_index do |p, i| results[p] = Array[@teams['teir_one'][i]] end second = @players.sort_by{rand} second.each_with_index do |p, i| results[p] &lt;&lt; @teams['teir_two'][i] end third = @players.sort_by{rand} third.each_with_index do |p, i| results[p] &lt;&lt; @teams['teir_three'][i] end fourth = @players.sort_by{rand} fourth.each_with_index do |p, i| results[p] &lt;&lt; @teams['teir_four'][i] end p results </code></pre> <p>I am sure there is a better way to iterate through the tiers, and duplicating the @players object ( <code>dup()</code> or <code>clone()</code> maybe?)</p> <p>So from one Cup Fan to another, help me out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload