4236ea6b84f4899b469e8edff4bf6d22

Good afternoon. I have a nested hash (@results) containing data that looks like the following:
{"1",{{"01-04-2008","2.0"}}, "2",{{"01-11-2008","4.0"},{"01-04-2008","10.0"}}}

I also have an object @week_endings which is just an array of unique week_endings found inside my nested hash (so in this case, 01-04-2008 and 01-11-2008).

My code below builds a simple HTML table like so

Name ID 01-04-2008 01-04-208
X 1 2.0 0
Y 2 10.0 4.0

I feel my code could use some refactoring to make my iteration through my nested hash a little more simple.

Any refactorings would be appreciated, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<table class="charge">
  <tr>
    <td colspan="100%" class="mainHeader"><%=@label.label%></td>
  </tr>
  <tr>
    <td class="columnHeader">OBS Name</td>
    <td class="columnHeader">OBS Number</td>
    <% for week in @week_endings%>
      <td class="columnHeader"><%=week.week_ending%></td>
    <%end%>
  </tr>
  <%@results.keys.each do |result| %>
    <tr>
     <td class="columnHeader"><%= Actual.obs_name(result) %></td>
      <td class="chargeBody"><%= result %></td>
         <%for date in @week_endings %>
           <% @value = "0" %>
             <% @results[result].each do |test| %>
               <% if date.week_ending.to_s == test[0]%>
                 <% @value = test[1].to_s %>
                 <%break%>
               <%end%>
             <%end%>
           <td class="chargeBody"><%=@value%></td>
         <%end%>
       </tr>
    <%end%>
</table>

Refactorings

No refactoring yet !

4236ea6b84f4899b469e8edff4bf6d22

mwilliams

February 5, 2008, February 05, 2008 19:35, permalink

No rating. Login to rate!

Refactoring from my co-worker using the fetch method on the hash which makes much more sense.

1
2
3
<%for date in @week_endings %>  
  <td class="chargeBody"><%=@results[result].fetch(date.week_ending.to_s,'0').to_s%></td>
<%end%>
Bbbc9b3dcb7c1553e8e77c32b01d59b8

Don Morrison

February 28, 2008, February 28, 2008 21:22, permalink

No rating. Login to rate!

Late to the party but saw this and had an idea ... I think using each is a bit more rubyish than the for...in

To test this myself I used:
@results = {"1" => {"01-04-2008" => "2.0"}, "2" => {"01-11-2008" => "4.0","01-04-2008" => "10.0"}}
@week_endings = ["01-04-2008","01-11-2008"]

If @week_endings is an array of other kinds of objects you could easily change this.

View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<table class="charge">
  <tr>
    <td colspan="100%" class="mainHeader"><%=@label.label%></td>
  </tr>
  <tr>
    <td class="columnHeader">OBS Name</td>
    <td class="columnHeader">OBS Number</td>
    <% @week_endings.each do |ending|
      <td class="columnHeader"><%=ending%></td>
    <%end%>
  </tr>
  <%@results.each do |key,value| %>
    <tr>
      <td class="columnHeader"><%= Actual.obs_name(key) %></td>
      <td class="chargeBody"><%= value %></td>
      <%@week_endings.each do |ending| %>
        <td class="chargeBody"><%=value.fetch(ending,'0')%></td>
      <%end%>
    </tr>
  <%end%>
</table>

Your refactoring





Format Copy from initial code

or Cancel