Some random rails, and maybe Ruby, questions..


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 :slight_smile:

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?

4) How would I count the number of occurences in an age range? (i.e.
between two values)
5) Converting a Hash to a HTML table seems like a common task. Are
there ready-made functions for this?
6) 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
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..
:slight_smile: ), 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.. :slight_smile:


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

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

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 =
users_by_decade = @users.group_by{|user| (this_year -



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?


There are a few more statistics ideas here:

Pretty clever and simple solutions for many statistics needs.

Lots of plotting tools... you may want to look at Tioga (http:// or rgplot (, a Ruby
interface to gnuplot (