Hi --

On Sun, 28 Jun 2009, Catsquotl wrote:

> Hi
>
> I have an array full_array[] of an arbitrary number of transaction objects.
> within the transaction is a Date member..
>
> From the array I have created a new array years[].uniq! based on the 
> Date.year members
>
> what id like to do is take the new arrays members and use them as names for 
> variables..
>
> Say the array holds members for 2007, 2008, 2009
> i`d like to iterate through the full_array and map all members from 2008 in 
> an array.
>
> my options are creating an array based on years.length where each member 
> holds all transactions from a specific year.
>
> or iterate through the members of years and create an array based on the 
> value or index of years..
>
> I can`t seem to think of the logic to accomplish this..

You could use group_by (if you've got Ruby 1.9 or a 1.9-flavored 1.8):

   full_array.group_by {|transaction| transaction.date.year }

That will give you a hash whose keys are the years and whose values
are arrays of transaction objects, one such array per year-key.


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com