Note that although you
calculated the average manually for demonstration purposes, you would normally use
MySQL??™s statistical functions to get this kind of information, as discussed in Chapter 2.
nNote The find method has quite a few options, as you??™ll see in upcoming chapters. For example, the
:conditions parameter specifies conditions for the record retrieval, just like a SQL WHERE clause. The
:limit parameter specifies a maximum number of records to return, just like the SQL LIMIT clause. In fact,
the :conditions parameter and the :limit parameter are directly translated into WHERE and LIMIT
clauses, respectively.
Finally, the code prints out the average salary, which is calculated by dividing the
total salary by the number of players:
puts "\nAverage Cost Per Win : $#{'%0.2f' % (salary_total / win_total )}"
Notice the use of the % operator. This lets you format the output using two decimal
points. It is very similar to the C/C++ sprintf function; in fact, it calls the kernel::spintf
function. You can find out more about the various formatting options at http://www.
Pages:
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45