title: Transpose, Slice and Inject From Multipe Arrays
body: <p>I don't know how appropriate title for this question.</p>
  
  <p>Well, This is my problem</p>
  
  <p>I have <code>index</code> action on <code>homes_controller.rb</code> looks like :</p>
  
  <pre><code>def index
    @r = 2
    @datas = [[1, 10], [2, 10], [3, 15], [4, 20], [5, 70], [6, 180], [7, 250], [8, 270], [9, 230], [10, 40], [11, 0], [12, 10]]
  end
  </code></pre>
  
  <p>I want to result from above like this:</p>
  
  <p><img src="https://i.stack.imgur.com/T1VSh.jpg" alt="i want"></p>
  
  <p>And on view looks like :</p>
  
  <pre><code>&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
       &lt;th&gt;Month&lt;/th&gt;
       &lt;th&gt;Forecast&lt;/th&gt;
       &lt;th&gt;Order&lt;/th&gt;
       &lt;th&gt;Begining Inventory&lt;/th&gt;
       &lt;th&gt;Ending Inventory&lt;/th&gt;
     &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
     &lt;% ei = 0 %&gt;
     &lt;% @datas.each_with_index do |d, index| %&gt;
      &lt;tr&gt;
       &lt;td&gt;&lt;%= d[0] %&gt;&lt;/td&gt;
       &lt;td&gt;&lt;%= d[1] %&gt;&lt;/td&gt;
       &lt;td&gt;&lt;% if d[1] &gt; bi %&gt;
        &lt;% @datas.transpose.at(1).slice(index, @r).inject do |sum, s| -%&gt;
         &lt;%= @biu = sum + s %&gt;
        &lt;% end %&gt;
       &lt;% end %&gt;
       &lt;/td&gt;
       &lt;td&gt;&lt;%= @bi = ei + @biu %&gt;&lt;/td&gt;
       &lt;td&gt;&lt;%= @ei = @bi - d[1]  %&gt;&lt;/td&gt;
      &lt;/tr&gt;
     &lt;% @biu = 0 %&gt;
     &lt;% ei = @ei %&gt;
     &lt;% end %&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>State : </p>
  
  <ol>
  <li>When the forecast <code>d[1]</code> is bigger than <code>ei</code> the calculation order <code>@biu</code> is 2 (<code>@r</code>) next of forecast. </li>
  <li>Begining Inventory <code>@bi</code> is calculation from <code>ei</code> plus order <code>@biu</code></li>
  <li>Ending Inventory <code>@ei</code> is calculation from <code>@bi</code>subtracted with forecast <code>d[1]</code></li>
  <li>And then <code>@ei</code> put to <code>ei</code></li>
  </ol>
  
  <p>But the result like this :</p>
  
  <p><img src="https://i.stack.imgur.com/vDAhn.jpg" alt="result"></p>
  
  <p>I want 12th months must have : Order = 10, Begining invetory = 10, and Ending Inventory = 0</p>