# Description

ar_result_calculations adds methods to Array for when Array is an ActiveRecord result set. For example:

`Product.all.sum(:price)`

Will return the sum of the attribute :price from all rows in the result set. Note this is not the same as Product.sum(:price) which will create an SQL statement that does the summing. When possible the ActiveRecord call which uses SQL will be more efficient if the result set has not already been returned.

```
Product.all.average(:price)
Product.all.regression(:sales_volumn)
```

Will calculate the average and regression of the specified columns.

`Product.all(:select => '(price * 2) as double_price').make_numeric(:double_price).sum(:double_price)`

Will return the sum of the derived column ‘double_price’. Note the call to **make_numeric** which is will coerce the derived column into numbers that can then be summed. ActiveRecord returns derived columns as strings hence the required coercion.

## Methods created on Array

All methods take one parameter (column name).

Method | Description |
---|---|

sum | Sum the given column. Delegates to super() if not an AR result set |

mean | Mean of the given column. Delegates to super() if not an AR result set. Aliases of avg and average |

count | Count the given column. Delegates to super() if not an AR result set |

min | Min the given column. Delegates to super() if not an AR result set |

max | Max the given column. Delegates to super() if not an AR result set |

regression | Ordinary Least Squares regression on the given column. Returns the regression as an array. Argument should be either an ActiveRecord result set or an array of numbers |

slope | Returns the slope of a regression on a given column. Argument should be either an ActiveRecord result set or an array of numbers |

make_numeric | Coerces a column to be numeric. Useful if you have derived columns returned from a query that are not in the model definition and hence are otherwise returned as strings. Returns self so is composable |

sample_variance | Calculates the sample variance of a column (for active record result sets) or self (for an array) |

standard_deviation | Calculates the standard deviation of a column (for active record result sets) or self (for an array) |

median | Calculates the median of a column (for active record result sets) or self (for an array) |

# License

(The MIT License)

Copyright © 2010 Kip Cole

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.