开发者

ruby - writing an array to a hash without overwriting

I do the following

my_hash = Hash.new
my_hash开发者_运维技巧[:children] = Array.new

I then have a function that calls itself a number of time each time writing to children

my_hash[:children] = my_replicating_function(some_values)

How do I write without overwriting data that is already written ?

This is what the entire function looks like

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


Maybe this:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜