Prev | Current Page 55 | Next

David Berube

"Practical Reporting with Ruby and Rails"


"#{player.total_wins}\t" <<
"#{player.total_players}\t" <<
"#{'%0.3f' % (player.total_wins.to_f / player.total_players.to_f) }"
end
# Print results per drink and per game:
sql = "SELECT drink,
games.name as game,
COUNT(*) as total_players,
(SELECT SUM(quantity) FROM wins
WHERE wins.player_id=players.id
AND wins.game_id=games.id) as total_wins
FROM players,
games
CHAPTER 2 n CALCULATING STATISTICS WITH ACTIVE RECORD 28
GROUP
BY games.name,
players.drink
; "
current_game=nil
Player.find_by_sql(sql).each do |player|
if current_game != player.game
puts "\n"
puts_underlined player.game
puts "Drink\t\tWins\tPlayers\tWins/Players"
current_game = player.game
end
puts "#{player.drink.ljust(12)}\t" <<
"#{player.total_wins}\t" <<
"#{player.total_players}\t" <<
"#{'%0.3f' % (player.total_wins.to_f /
player.total_players.to_f ) }"
end
Save this script as drink_win_distribution.rb. You can run the script as follows:
ruby drink_win_distribution.rb
You should get the following results:
Overall
=======
Drink Wins Players Wins/Players
Fresca 28 4 7.


Pages:
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67