如何格式化此哈希?

时间:2011-12-31 06:10:25

标签: ruby-on-rails ruby hash

我有一个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

1 个答案:

答案 0 :(得分:1)

假设我们可以从active_status对象获取name,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