Search code examples
rubymultidimensional-arrayhashnestedruby-hash

Transforming deep nested hash into multidimensional array in ruby


I want to transform nested hashes into multidimensional arrays recursivley.

This is my current code:

def deep_to_a(hash)
  return  hash.to_a.each {|k, v|
    if k.is_a?(Hash) then
      hash[k.to_a] = hash.delete(k)
      deep_to_a(k)
    elsif v.is_a?(Hash) then
      hash[k] = hash[k].to_a
      if hash[k].any?{|k, v| k.is_a?(Hash) || v.is_a?(Hash)}
        deep_to_a(v)
      end
    end
  }
end

I want to get:

h = {11=>{12=>13, 14=>15}, 16=>17}
p deep_to_a(h) # => [[11, [[12, 13], [14, 15]]], [16, 17]] 

But I get

[[11, {12=>13, 14=>15}], [16, 17]]

How can I make it work?


Solution

  • A destructive function is hard to debug. In this case, map is better than each + destructive assignment.

    The result of hash.to_a is an array, so your iteration |k, v| is incorrect.

    def deep_to_a(hash)
      hash.map do |v|
        if v.is_a?(Hash) or v.is_a?(Array) then
          deep_to_a(v)
        else
          v
        end
      end
    end
    
    h = {11=>{12=>13, 14=>15}, 16=>17}
    p deep_to_a(h)
    # [[11, [[12, 13], [14, 15]]], [16, 17]]