I want to embed some ruby code to adjust the width of a on my html page.
Basically I have a loop in the Html page which looks like this
<% pass_fail.each do |row| %>
<div class="bar bar-success" style="width: 70%;"></div>
<div class="bar bar-warning" style="width: 20%;"></div>
<div class="bar bar-danger" style="width: 10%;"></div>
<% end %>
I want the width of the divs to be something like
((row[1]/(row[1]+row[2]+row[3]))*100)%
((row[2]/(row[1]+row[2]+row[3]))*100)%
((row[3]/(row[1]+row[2]+row[3]))*100)%
Can someone please help me with the exact syntax to embed the code to set the width of the div.
Asuming
rowis a Array of three ints, use<%= %>to output:to_fmust be used to capture the results since it is between 0 and 1. Can be simplified by summing the array usinginject:Not really see what you will use this for, but I understand it is spectacular.
Unless the sum of
rowis changing between each iteration, it is recommended to cache the sum where possible to speed up execution.