# Extending models with statistic capabilities

**URL:** <https://www.ruby-forum.com/t/extending-models-with-statistic-capabilities/174021>\
**Category:** Rails\
**Created:** [August 19, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/extending-models-with-statistic-capabilities/174021 "2009-08-19T16:04:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph\_j](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@christoph\_j](https://www.ruby-forum.com/u/christoph_j)\
**Post date:** [August 19, 2009, 4:04pm UTC](https://www.ruby-forum.com/t/extending-models-with-statistic-capabilities/174021/1 "2009-08-19T16:04:12Z")

</div>

Hi,  
I have several models in my app which contain several numerical  
attribute.  
Ex.: stock model; attributes: bid, ask, value,…  
todays\_values\_of\_Google = [ [#Stock: id=1, bid = 1.5, ask = 1.5, value =  
1.5, time=…, …], [#Stock: id=2, bid = 1.45, ask = 1.5, value =  
1.45,…],… ]

Now I would like to have some statistics. I saw the possibility of open  
the  
array class and add some functions like:

Class Array  
def mean; blabla ; end;  
…  
end

But I would like to have the option of telling the _attribute_ on which  
to  
calculate the mean.

So what would be the best practice for this as I need this on several  
model?  
1.) A module? How?  
Maybe this  
def mean(_array,attr_); #calculate over all items of _array_s  
attribute  
_attr_… ; end  
2.) Modify Classes? Where to do it so all model can access the “new”  
methods  
of Array (for example)  
3.) Somethink else?

Thanks for any help

Chris
