Best practice: Make an array of arrays

Hello,

I was wondering what was the best practice for making an array of
arrays.
I have an arrays of object order by date, and I want a new array with
object grouped by date.

Greg

hi gregory,

could you provide a brief example of the arrays and the code you’re
working with now? that would make it much easier to give relevant
suggestions…

cheers,

  • j

Gregory Ma wrote in post #1004995:

Hello,

I was wondering what was the best practice for making an array of
arrays.
I have an arrays of object order by date, and I want a new array with
object grouped by date.

Greg

How about something like this:

require ‘date’
require ‘pp’

class Widget
attr_reader :date

def initialize(id, date)
@id = id
@date = date
end
end

data = 20.times.map do |id|
d = Date.new(2011, 6, rand(10) + 1 )
Widget.new(id, d)
end

pp data

results = data.group_by do |widget|
widget.date.to_s
end

pp results

–output:–

[#<Widget:0x10034d658 @date=#<Date: 4911427/2,0,2299161>, @id=0>,
#<Widget:0x10034d360 @date=#<Date: 4911439/2,0,2299161>, @id=1>,
#<Widget:0x10034c5a0 @date=#<Date: 4911439/2,0,2299161>, @id=2>,
#<Widget:0x10034b768 @date=#<Date: 4911433/2,0,2299161>, @id=3>,
#<Widget:0x10034a0e8 @date=#<Date: 4911429/2,0,2299161>, @id=4>,
#<Widget:0x100348c20 @date=#<Date: 4911429/2,0,2299161>, @id=5>,
#<Widget:0x1003472d0 @date=#<Date: 4911439/2,0,2299161>, @id=6>,
#<Widget:0x1003465b0 @date=#<Date: 4911439/2,0,2299161>, @id=7>,
#<Widget:0x100345728 @date=#<Date: 4911427/2,0,2299161>, @id=8>,
#<Widget:0x100344e90 @date=#<Date: 4911433/2,0,2299161>, @id=9>,
#<Widget:0x100342e10 @date=#<Date: 4911443/2,0,2299161>, @id=10>,
#<Widget:0x100340020 @date=#<Date: 4911443/2,0,2299161>, @id=11>,
#<Widget:0x10033d618 @date=#<Date: 4911439/2,0,2299161>, @id=12>,
#<Widget:0x10033b7f0 @date=#<Date: 4911435/2,0,2299161>, @id=13>,
#<Widget:0x10033b340 @date=#<Date: 4911431/2,0,2299161>, @id=14>,
#<Widget:0x10033a120 @date=#<Date: 4911445/2,0,2299161>, @id=15>,
#<Widget:0x1003388c0 @date=#<Date: 4911445/2,0,2299161>, @id=16>,
#<Widget:0x1003373d0 @date=#<Date: 4911431/2,0,2299161>, @id=17>,
#<Widget:0x100335008 @date=#<Date: 4911429/2,0,2299161>, @id=18>,

#<Widget:0x100334838 @date=#<Date: 4911435/2,0,2299161>, @id=19>]
{“2011-06-05”=>
[#<Widget:0x10033b7f0 @date=#<Date: 4911435/2,0,2299161>, @id=13>,
#<Widget:0x100334838 @date=#<Date: 4911435/2,0,2299161>, @id=19>],
“2011-06-07”=>
[#<Widget:0x10034d360 @date=#<Date: 4911439/2,0,2299161>, @id=1>,
#<Widget:0x10034c5a0 @date=#<Date: 4911439/2,0,2299161>, @id=2>,
#<Widget:0x1003472d0 @date=#<Date: 4911439/2,0,2299161>, @id=6>,
#<Widget:0x1003465b0 @date=#<Date: 4911439/2,0,2299161>, @id=7>,
#<Widget:0x10033d618 @date=#<Date: 4911439/2,0,2299161>, @id=12>],
“2011-06-10”=>
[#<Widget:0x10033a120 @date=#<Date: 4911445/2,0,2299161>, @id=15>,
#<Widget:0x1003388c0 @date=#<Date: 4911445/2,0,2299161>, @id=16>],
“2011-06-09”=>
[#<Widget:0x100342e10 @date=#<Date: 4911443/2,0,2299161>, @id=10>,
#<Widget:0x100340020 @date=#<Date: 4911443/2,0,2299161>, @id=11>],
“2011-06-01”=>
[#<Widget:0x10034d658 @date=#<Date: 4911427/2,0,2299161>, @id=0>,
#<Widget:0x100345728 @date=#<Date: 4911427/2,0,2299161>, @id=8>],
“2011-06-02”=>
[#<Widget:0x10034a0e8 @date=#<Date: 4911429/2,0,2299161>, @id=4>,
#<Widget:0x100348c20 @date=#<Date: 4911429/2,0,2299161>, @id=5>,
#<Widget:0x100335008 @date=#<Date: 4911429/2,0,2299161>, @id=18>],
“2011-06-03”=>
[#<Widget:0x10033b340 @date=#<Date: 4911431/2,0,2299161>, @id=14>,
#<Widget:0x1003373d0 @date=#<Date: 4911431/2,0,2299161>, @id=17>],
“2011-06-04”=>
[#<Widget:0x10034b768 @date=#<Date: 4911433/2,0,2299161>, @id=3>,
#<Widget:0x100344e90 @date=#<Date: 4911433/2,0,2299161>, @id=9>]}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs