循环通过带有轨道的哈希数组

时间:2012-07-09 19:48:41

标签: ruby-on-rails ruby ruby-on-rails-3 loops

所以我试图从这一系列哈希中抓住所有高中研究生院

"education": [
    {
      "school": {
        "id": "110703012290674", 
        "name": "Kunskapsgymnasiet Malmö"
      }, 
      "year": {
        "id": "136328419721520", 
        "name": "2009"
      }, 
      "type": "High School"
    }, 
    {
      "school": {
        "id": "112812485399398", 
        "name": "Malmö University"
      }, 
      "year": {
        "id": "118118634930920", 
        "name": "2012"
      }, 
      "concentration": [
        {
          "id": "104076956295773", 
          "name": "Computer Science"
        }
      ], 
      "type": "Graduate School", 
      "classes": [
        {
          "id": "165093923542525", 
          "name": "Programmering", 
          "description": "Kursen fokuserar på metoder och tekniker vid utveckling av webbapplikationer med hjälp av HTML5."
        }
      ]
    }
  ],

像这样:

if auth["extra"]["raw_info"]["education"]  

        # hash/array element 0
        if auth["extra"]["raw_info"]["education"][0]["type"] == "High School" 
          user.highschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?)
          user.highschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?)

        elsif auth["extra"]["raw_info"]["education"][0]["type"] == "Graduate School"
          user.graduateschool_name = auth["extra"]["raw_info"]["education"][0]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["school"].blank?)
          user.graduateschool_year = auth["extra"]["raw_info"]["education"][0]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][0]["year"].blank?)
        end

        # hash/array element 1 
        if auth["extra"]["raw_info"]["education"][1]["type"] == "High School"
          user.highschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?)
          user.highschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?)

        elsif auth["extra"]["raw_info"]["education"][1]["type"] == "Graduate School"        
          user.graduateschool_name = auth["extra"]["raw_info"]["education"][1]["school"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["school"].blank?)
          user.graduateschool_year = auth["extra"]["raw_info"]["education"][1]["year"]["name"] if (!auth["extra"].blank? && !auth["extra"]["raw_info"]["education"][1]["year"].blank?)
        end
      end

有没有一种更有效的方法来实现它,比如某种循环?有什么好主意吗?

1 个答案:

答案 0 :(得分:3)

是的,确切地说,你应该使用一个循环。似乎你想要这样的东西:

if edu = auth["extra"]["raw_info"]["education"]  
  edu.each do |e|
    if e["type"] == "High School" 
      user.highschool_name = e["school"]["name"] if (!auth["extra"].blank? && !e["school"].blank?)
      user.highschool_year = e["year"]["name"] if (!auth["extra"].blank? && !e["year"].blank?)
    elsif e["type"] == "Graduate School"
      user.graduateschool_name = e["school"]["name"] if (!auth["extra"].blank? && !e["school"].blank?)
      user.graduateschool_year = e["year"]["name"] if (!auth["extra"].blank? && !e["year"].blank?)
    end
  end
end

更新

好的,这里的版本更简单(基于tokland的建议)。

if (auth["extra"]["raw_info"]["education"] || []).each do |e|

  nameprop, yearprop = if e["type"] == "High School"
    [:highschool_name, :highschool_year]
  elsif e["type"] == "Graduate School"
    [:graduateschool_name, :graduateschool_year]
  end

  user.send("#{nameprop}=", e["school"]["name"]) if !e["school"].blank?
  user.send("#{yearprop}=", e["year"]["name"]) if !e["year"].blank?
end