Phil Mok
Phil Mok

Reputation: 4050

How do I loop numbers into a variable with ruby?

I am building an app for comparing the fantasy football scores of two football players. The NFL season is 17 weeks long therefore each player in the comparison has 17 scores. I have it set up so that each score is a variable.

For example, athlete 1's week 1 score is @a1w1. Athlete 2's is @a2w1.

I have these stats compared in a table and I want the higher score to appear green. The code I originally wrote is not very DRY.

<table>
  <tr>
<% if @a1w1 != nil && @a2w1 != nil && @a1w1 > @a2w1 %>
  <% @a1w1green = "green".html_safe %>
<% else %>
  <% @a1w1green = nil %>
<% end %>

<% if @a1w1 != nil && @a2w1 != nil && @a1w1 < @a2w1 %>
  <% @a2w1green = "green".html_safe %>
<% else %>
  <% @a2w1green = nil %>
<% end %>
    <td class='matchup-stats <%= @a1w1green %>'><%= @a1w1 %></td>
    <td class='stat-week'>1</td>
    <td class='matchup-stats <%= @a2w1green %>'><%= @a2w1 %></td>
  </tr>
</table>

I would have to repeat this 16 times to fill out all 17 weeks. I have an idea for creating a loop that does this for me but am having trouble getting the syntax correct.

<table>
  <% 1.upto(17) do|week| %>
  <% @a1green = @a2green = nil %>
    <% if @a1w[week] > @a2w[week] %>
      <% @a1green = "green".html_safe %>
    <% elsif @a2w[week] > @a1w[week] %>
      <% @a2green = "green".html_safe %>
    <% end %>
    <tr>
      <td class='matchup-stats <%= @a1green %>'><%=  @a1w[week] %></td>
      <td class='stat-week'><%= week %></td>
      <td class='matchup-stats <%= @a2green %>'><%=  @a2w[week] %></td>
    </tr>
    <% end %>
</table>

I know that using the [] are not correct but I can't figure out the correct way to write this. I'm kinda new to Ruby and I've been working at this for hours. Can somebody please lend me some advice?

Upvotes: 0

Views: 59

Answers (2)

Phil Mok
Phil Mok

Reputation: 4050

Inspired by Exupery's answer, I have come up with a solution. I already have the players scores in arrays: @a1vals and @a2vals

<table>
<% 0.upto(16) do|week| %>
<% @a1green = @a2green = nil %>
  <% if @a1vals[week] != nil && @a2vals[week] != nil && @a1vals[week] > @a2vals[week] %>
    <% @a1green = "green".html_safe %>
  <% elsif @a1vals[week] != nil && @a2vals[week] != nil && @a1vals[week] < @a2vals[week] %>
    <% @a2green = "green".html_safe %>
  <% end %>
    <tr>
      <td class='matchup-stats <%= @a1green %>'><%= @a1vals[week] %></td>
      <td class='stat-week'><%= week %></td>
      <td class='matchup-stats <%= @a2green %>'><%= @a2vals[week] %></td>
    </tr>
    <% end %>
</table>

Thanks for your help @Exupery!

Upvotes: 1

frostmatthew
frostmatthew

Reputation: 3298

Instead of storing each score as a variable you can use a hash. You can have a hash for each player and have a key for each week (you could then store the collection of players as an array for easy passing/iterating).

For example:

@a1 = Hash.new
@a1[1] = 42
@a1[2] = 49

...and so forth (this is just a simple example, there are many ways to populate elements of a hash, if you're not familiar I'd suggest reading the ruby doc).

You could then iterate over a player's weeks by something like:

1.upto(17) do |week|
  score = @a1[week]
  ## do whatever with score
end

Upvotes: 0

Related Questions