【问题标题】:how to parse the given hash into desired output in ruby on rails?如何在 ruby​​ on rails 中将给定的哈希解析为所需的输出?
【发布时间】:2026-01-26 10:10:01
【问题描述】:

我必须向用户发送每日课程更新通知。更新通知包含四种类型的[:due,:missed,:over_due,:new]。为了方便起见,我从数据中省略了不必要的字段,例如到期和错过,因为它与过期和新格式相同。下面提供的数据属于用户。用户可以是许多课程的一部分,这就是为什么哈希中有 2 个不同的 course_id(course_id 20 和 30)。 id 在这里代表特定课程中的 assignment_id。 数据的实际格式是。

  { 
   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 => {}
     }
   }

假设,这只是我为用户创建的虚拟数据,以便更清晰和解释。

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"
    }
  }
}

我要求将数据解析成这种格式:

{
  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"
      }
    }
  }
}

【问题讨论】:

  • 首先,向我们展示您到目前为止所做的尝试。那么,明确words的预期结果是什么。您的一些哈希值已将 :course_id 转换为 value,而另一些则没有...请给我们一些见解。
  • 我已经更新了这个问题。看下面的代码。 hash = {} assignments.each 做 |type,type_data| type_data.each 做 |assignment_id,data| course_id = data[:course_id] hash[course_id] = {} hash[course_id][type]= {} hash[course_id][type][assignment_id] = data end end
  • @MukeshKumarGupta 不要在 cmets 部分编写代码,它很难阅读。请edit your post 格式化好。
  • 另外,您能否就您实际上试图解决的问题提供更多背景信息?你有一堆非常丑陋的散列散列;可能有更好的方法来解决实际问题。
  • 这是我第一次发布问题。我记住了你的建议。

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-hash


【解决方案1】:

检查以下代码以获取您的解决方案:

 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

输出:

{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"}}}}

【讨论】:

  • 是的!这是工作。我得到了我所缺少的。我每次都盲目地创建空哈希。我认为我们可以优化。如果我找到更好的方法来做到这一点。我将在相同的线程中发布。谢谢。
  • @MukeshKumarGupta 太好了。如果它适合你,请接受我的回答>谢谢