# Some random rails, and maybe Ruby, questions

**URL:** <https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673>\
**Category:** Rails\
**Created:** [March 12, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673 "2008-03-12T12:00:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 12, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673/1 "2008-03-12T12:00:59Z")

</div>

Hi,

I’m attempting to create a users statistics controller, and have saved  
up some questions regarding this for this post.  
So, please comment on any one of them, if not all 🙂

1. Is there a library somewhere for doing stats? (e.g. mean, median,  
sd, skewness…) on an array in rails?
2. What library would you recommend for “publication ready” (i.e. not  
cheesy) histograms, scatterplots and barplots?
3. Is there some simple way of doing a frequency table of the values  
in an array in Ruby? My solution is

> m1.each {|mem| out[mem.age] = 0 }  
> m1.each {|mem| out[mem.age] += 1 }

where m1 is a collection of members. Are there better solutions?

1. How would I count the number of occurences in an age range? (i.e.  
between two values)
2. Converting a Hash to a HTML table seems like a common task. Are  
there ready-made functions for this?
3. Is there a way of sorting a collection (of members, in my case) by  
one factor, without accessing the database once for each level of the  
factor?  
E.g. “gender” will have either one of two values… So, if i would  
like to get separate my members by gender (Not very PC, I know, but…  
🙂 ), do I have to make two different .find calls?

Sorry for the number of questions… but I am trying to get at grip of  
Rails / Ruby and how to do things The RoR way… 🙂

/Fredrik

–  
“Give up learning, and put an end to your troubles.”

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 12, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673/2 "2008-03-12T18:07:14Z")

</div>

If you are going to do multiple operations on the data (ie., view the  
same data from a number of different perspectives) you might consider  
reading it in once and then doing the operations in Ruby. I think  
that might be what you were hinting at with “not going to the db each  
time.”

With that in mind, 3, 4, and 6 can make use of the “group\_by” method  
mixed into Array. This method iterates over the collection and builds  
a hash in which the keys of the hash are the ‘grouping’ and the values  
are the elements that match that grouping. The ‘grouping’ is the  
result of applying a block. For example:

users\_by\_age = @users.group\_by{|user| user.age}

Now you can iterate over users\_by\_age:

users\_by\_age.each\_pair do |age, age\_collection|  
puts “There are #{age\_collection.size} members who are #{age} years  
old.”  
end

Since you have complete control over the block, you can use the same  
technique to create bigger bins for your histogram and completely  
change the grouping mechanism.

users\_by\_gender = @users.group\_by{|user| user.gender}

this\_year = Date.today.year  
users\_by\_decade = @users.group\_by{|user| (this\_year -  
user.born\_on.year)/10}

HTH,  
AndyV

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 12, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673/3 "2008-03-12T22:13:15Z")

</div>

Fantastic!

Exactly what I was looking for! Just didn’t have the imagination  
enough to find the group\_by method of arrays to be the solution to  
almost all my problems.

So, what do I plot this created data set with? Any ideas?

/Fredrik

On Wed, Mar 12, 2008 at 6:05 PM, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> are the elements that match that grouping. The ‘grouping’ is the
> 
> > So, please comment on any one of them, if not all 🙂
> > 
> > like to get separate my members by gender (Not very PC, I know, but…

–  
“Give up learning, and put an end to your troubles.”

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 13, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/some-random-rails-and-maybe-ruby-questions/131673/4 "2008-03-13T16:19:36Z")

</div>

There are a few more statistics ideas here:  
[http://derrick.pallas.us/ruby-stats/](http://derrick.pallas.us/ruby-stats/)

Pretty clever and simple solutions for many statistics needs.

Lots of plotting tools… you may want to look at Tioga (http://  
[tioga.rubyforge.org/](http://tioga.rubyforge.org/)) or rgplot ([http://rgplot.rubyforge.org/](http://rgplot.rubyforge.org/)), a Ruby  
interface to gnuplot ([http://www.gnuplot.info/](http://www.gnuplot.info/)).
