0

I had to send daily course update notification to the user. Update notification contain four types of [:due,:missed,:over_due,:new]. To make it easy I omitted unnecessary field from data like due and missed coz it the same as overdue and new format. Below-provided data belonged a user. A user can be part of many courses that is why 2 different course_id there in the hash (course_id 20 and 30). The id represents here assignment_id in a particular course. The actual format of data are.

  { 
   1 => { #the 1st user
     :new => {
          1 => {
           :id => 1,
           :course_id =>20,
           :course_name => "B"
          },
          2 => {
           :id => 2,
           :course_id =>30,
           :course_name => "A"
         },
         3 => {
           :id => 3,
           :course_id =>20,
           :course_name => "B"
         }
       }
       :over_due => {}, #This is also having the same format as new
       :missed => {}, #This is also having the same format as new
       :due => {} #This is also having the same format as new
     },
    2 => { #this is 2nd user
       :new => {},
       :over_due => {},
       :missed => {},
       :due => {}
     }
   }

Suppose, This is just a dummy data I had created for a user for more clarity and explanation.

assignments = {
  :new => {
    1 => {
      :id => 1,
      :course_id => 20,
      :name => "A"
    },
    2 => {
      :id => 2,
      :course_id => 20,
      :name=>"A"
    },
    3 => {
      :id => 3,
      :course_id => 30,
      :name=>"B"
    }
  },
  :over_due => {
    4 => {
      :id => 4,
      :course_id => 20,
      :name => "A"
    },
    5 => {
      :id => 5,
      :course_id => 30,
      :name => "B"
    }
  }
}

My requirement to parse data into this format:

{
  20 => {
    :new => {
      1 => {
        :id => 1,
        :course_id => 20,
        :name=>"A"
      },
      2 => {
        :id => 2,
        :course_id => 20,
        :name => "B"
      }
    },
    :over_due => {
      4 => {
        :id => 4,
        :course_id => 20,
        :name => "E"
      }
    }
  },
  30 => {
    :new => {
      3 => {
        :id => 3,
        :course_id => 30,
        :name => "C"
      }
    },
    :over_due => {
      5 => {
        :id => 5,
        :course_id => 30,
        :name=>"F"
      }
    }
  }
}
Mukesh Kumar Gupta
  • 1,567
  • 20
  • 15
  • First, show us what you attempted so far. Then, what's the expected result in clear *words*. Some of your hashes have `:course_id` been transformed to `value` some other haven't... Give us some insight. – Valentin Trinqué Jun 13 '17 at 11:32
  • I had updated the question. look at the following code. hash = {} assignments.each do |type,type_data| type_data.each do |assignment_id,data| course_id = data[:course_id] hash[course_id] = {} hash[course_id][type]= {} hash[course_id][type][assignment_id] = data end end – Mukesh Kumar Gupta Jun 13 '17 at 11:46
  • 1
    @MukeshKumarGupta Don't write code in the comments section, it's very difficult to read. Please [edit your post](https://stackoverflow.com/posts/44520178/edit) and format it nicely. – Tom Lord Jun 13 '17 at 11:51
  • 1
    Also, could you provide a bit more context in terms of what problem you're *actually* trying to solve here? You've got a really ugly mess of hashes-of-hashes-of-hashes; there's probably a much better way to solve the actual problem. – Tom Lord Jun 13 '17 at 11:59
  • This is the first time I am posting a question. I keep in mind your suggestion. – Mukesh Kumar Gupta Jun 13 '17 at 12:00
  • I had to send daily course update notification to the user (valid user this is already taken care correctly). update notification contain four types of [:due,:missed,:over_due,:new]. To make it easy I omitted unnecessary field from data. A user can be part of many courses that is why 2 different course_id there in the hash. The id represents here assignment id in a particular course. 1,2,3,4,5 are assignment id and 20,30 course_id. – Mukesh Kumar Gupta Jun 13 '17 at 12:06

1 Answers1

1

check below code for your solution:

 hash = {} 
 assignments.each do |type,type_data| 
    type_data.each do |assignment_id,data| 
        course_id = data[:course_id]
        hash[course_id] = {} if hash[course_id].nil?
        hash[course_id][type]= {} if hash[course_id][type].nil?
        hash[course_id][type][assignment_id] = data 
    end 
end

output:

{20=>{:new=>{1=>{:id=>1, :course_id=>20, :name=>"A"}, 2=>{:id=>2, :course_id=>20, :name=>"B"}}, :over_due=>{4=>{:id=>4, :course_id=>20, :name=>"E"}}}, 30=>{:new=>{3=>{:id=>3, :course_id=>30, :name=>"C"}}, :over_due=>{5=>{:id=>5, :course_id=>30, :name=>"F"}}}}
puneet18
  • 4,341
  • 2
  • 21
  • 27
  • yeah! It is working. I got what I was missing. I was creating empty hash each time blindly. I think we can optimize. If I find any better way to do it. I will post in the same threads. Thank you. – Mukesh Kumar Gupta Jun 13 '17 at 12:29
  • 1
    @MukeshKumarGupta Great. Accept my answer if it works for you> Thanks – puneet18 Jun 13 '17 at 12:47