2011-12-31 72 views
0

我有一個UUID,每個UUID有三個獨特的屬性。我想存儲所有這些。我知道我需要一個散列裏面的散列,但是我在這樣做時遇到了麻煩。如何格式化此散列?

它在一個循環內創建它們,並且每次迭代我需要將它添加到散列中,所以我不知道如何去做。

19ee480015a2012f0aeb64ce8f2f69f4: 
status: complete 
name: SaveComment 
pct_complete: 100 

083732301597012f0aea64ce8f2f69f4: 
status: working 
name: SaveComment 
pct_complete: 35 

bf40ca301596012f0ae864ce8f2f69f4: 
status: complete 
name: SaveComment 
pct_complete: 100 

這是代碼它進入:

get '/percentcomplete' do 
    progress = {} 
    Resque::Status.status_ids.each do |uuid| 
    active_status = Resque::Status.get(uuid) 

    #update hash each loop here with name, status, pct_complete, and uuid 
    end 
end 
+2

那麼,有什麼問題? – 2011-12-31 06:12:45

+0

我如何格式化哈希,就像我不知道里面的東西是什麼。所有我能想到的是散列是像字典的東西..在同一級別的層次結構。我也不知道如何將每個新組添加到循環中的散列,以便它保留{{uuid,this,that} {uuid,this,}} – Tallboy 2011-12-31 06:17:28

+0

仍然不清楚。 – 2011-12-31 06:19:26

回答

1

假設我們能得到的名稱,狀態,從active_status對象pct_complete,

get '/percentcomplete' do 
    progress = {} 
    Resque::Status.status_ids.each do |uuid| 
    active_status = Resque::Status.get(uuid) 

    #update hash each loop here with name, status, pct_complete, and uuid 
    progress[uuid.to_s] = {:name => active_status.name, 
      :status => active_status.status, 
      :ptc_complete => active_status.ptc_complete} 
    end 
end 
+0

非常感謝你 – Tallboy 2011-12-31 06:48:52

+0

比我想象的容易..所以真的沒有添加運算符?你只需用新的密​​鑰覆蓋變量,然後添加它? – Tallboy 2011-12-31 06:52:47

+0

有一個存儲方法,我們可以將key和value作爲參數傳遞給它。有關更多詳細信息,請參閱http://www.ruby-doc.org/core-1.9.3/Hash.html。 – nkm 2011-12-31 07:02:03