I have a store which holds objects like [obj1, obj2, obj3] with attributes date, task, staff, etc. The objects contains data like date "18.07.2019", staff 1001, task 1
My initial idea was to group them somehow, but I miserably failed. I'd like build a structure as shown at the bottom of the post, either from the objects itselves or from an contructed array holding only three (or maybe more) attributes.
Challenge:
As I am using an very old ruby 1.6 I can't use all the modern easy methods.
The store consists of objects like [obj1, obj2, obj3] with attributes date, task, staff.
all_items = []
store.each do |obj|
all_items << [obj.date, obj.task, obj.staff]
end
My attempt:
I tried a lot like
h = {}
h[date] = []
h[date] << [obj.date, obj.task, obj.staff]
h[date].to_h
Current state:
h = {}
all_items = []
store.each do |obj|
h[obj.date.to_s] = []
h[obj.date.to_s] << { obj.date.to_s => [obj.staff, obj.task] }
all_items << Hash[*h[obj.date.to_s].flatten]
end
puts all_items.inspect
looks like this (still nothing I can work with):
[
{"18.07.2019"=>[1001, 2]},
{"18.07.2019"=>[1001, 2]},
{"18.07.2019"=>[1001, 3]},
{"19.07.2019"=>[1001, 1]},
{"20.07.2019"=>[1002, 3]},
...
]
Expected format:
I would love to come out with something like this, so that I know who has which tasks on which date in order to work with the data later in my program.
{
"18.07.2019" => [
{ staff: 1001, tasks: [1, 2, 3]},
{ staff: 1002, tasks: [1, 2, 4]}
],
"19.07.2019" => [
{ staff: 1001, tasks: [2, 3]},
{ staff: 1002, tasks: [1, 2]}
],
...
}
"18.07.2019",1001,[1,2,3], ... in somewhere.