how do i loop over a hash of hashes in ruby

前端 未结 5 918
故里飘歌
故里飘歌 2020-12-13 05:32

OK so i have this hash

 h
 => {\"67676.mpa\"=>{:link=>\"pool/sdafdsaff\", :size=>4556}} 

>  h.each do |key, value|
>     puts key
>   p         


        
5条回答
  •  鱼传尺愫
    2020-12-13 06:00

    You'll want to recurse through the hash, here's a recursive method:

    def ihash(h)
      h.each_pair do |k,v|
        if v.is_a?(Hash)
          puts "key: #{k} recursing..."
          ihash(v)
        else
          # MODIFY HERE! Look for what you want to find in the hash here
          puts "key: #{k} value: #{v}"
        end
      end
    end
    

    You can Then take any hash and pass it in:

    h = {
        "x" => "a",
        "y" => {
            "y1" => {
                "y2" => "final"
            },
            "yy1" => "hello"
        }
    }
    ihash(h)
    

提交回复
热议问题