2011-06-03 62 views
1

我下面紅寶石 - 寫一個數組哈希而不覆蓋

my_hash = Hash.new 
my_hash[:children] = Array.new 

然後,我有一個自稱一些時間,每次寫入孩子的函數

my_hash[:children] = my_replicating_function(some_values) 

怎麼辦我寫入時不覆蓋已寫入的數據?

這是整個功能是什麼樣子

def self.build_structure(candidates, reports_id) 
structure = Array.new 
candidates.each do |candidate, index| 
    if candidate.reports_to == reports_id 
    structure = candidate 
    structure[:children] = Array.new 
    structure[:children] = build_structure(candidates, candidate.candidate_id) 
    end 
end 
structure 
end 

回答

3

也許這:

structure[:children] << build_structure(candidates, candidate.candidate_id) 
2

structure[:children] << build_structure(candidates, candidate.candidate_id)