Alex
Alex

Reputation: 6475

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

Upvotes: 1

Views: 184

Answers (2)

bor1s
bor1s

Reputation: 4113

Maybe this:

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

Upvotes: 3

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

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

Upvotes: 2

Related Questions