Averaging

Why can I call this

<%= team.players.sum(:misses) %>
and this
<%= team.players.sum(:misses) %>

but this gives me 0 (some do not have 0 for misses)

<%= team.players.sum(:hit) /
team.players.sum(:misses) unless team.players.sum(:misses) == 0 %></
td>

Say hits = 2 and misses = 4. In Ruby, 2/4 = 0. However, 2.0/4.0 = 0.5.
Try
those expressions in irb.

To solve your problem, you could convert the numbers to floats:
hits.to_f /
misses.to_f = 2.0 / 4.0 = 0.5.

The docs for Number#divmod might be helpful.
http://www.noobkit.com/show/ruby/ruby/ruby-core/numeric/divmod.html

Craig

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs