Ruby return top level hash key if value recursivel

2019-09-16 16:46发布

问题:

I have the data structure below and I am trying to return the top level key (lo, eth0 or eth1) if anywhere recursively and arbitrarily deep within it's value is a given string. Then terminate the search after the first instance of the string is found.

Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays This is sort of similar to what I'm trying to do but I haven't been able to map it to my own problem

h.find{ |k,v| break k if v.include? "number" }
 => "eth0"

h.find{ |k,v| break k if v.include? "10.0.128.26" }
 => nil
#Should return eth0

I'd like to know how to generally work with nested data structures like this, but I'd settle for being able to specifically search within a specific sub hash, addresses in my case.

    h = \
    {"lo"=>
      {"mtu"=>"65536",
       "flags"=>["LOOPBACK", "UP", "LOWER_UP"],
       "encapsulation"=>"Loopback",
       "addresses"=>
        {"127.0.0.1"=>
          {"family"=>"inet",
           "prefixlen"=>"8",
           "netmask"=>"255.0.0.0",
           "scope"=>"Node"}},
       "state"=>"unknown"},
     "eth0"=>
      {"type"=>"eth",
       "number"=>"0",
       "mtu"=>"1500",
       "flags"=>["BROADCAST", "MULTICAST", "UP", "LOWER_UP"],
       "encapsulation"=>"Ethernet",
       "addresses"=>
        {"00:0C:29:1A:64:6A"=>{"family"=>"lladdr"},
         "10.0.128.26"=>
          {"family"=>"inet",
           "prefixlen"=>"24",
           "netmask"=>"255.255.255.0",
           "broadcast"=>"10.0.128.255",
           "scope"=>"Global"}},
       "state"=>"up",
       "arp"=>
        {"10.0.128.31"=>"00:0c:29:04:12:9a",
         "10.0.128.100"=>"00:0c:29:5b:b4:46",
         "10.0.128.30"=>"00:0c:29:05:a4:c7",
         "10.0.128.18"=>"00:0c:29:6a:3f:75",
         "10.0.128.3"=>"0c:c4:7a:c0:31:d1",
         "10.0.128.43"=>"00:0c:29:01:eb:6b",
         "10.0.128.44"=>"00:09:0f:09:00:03",
         "10.0.128.14"=>"00:0c:29:d2:15:80",
         "10.0.128.22"=>"00:0c:29:18:99:30"},
       "routes"=>
        [{"destination"=>"10.0.128.0/24",
          "family"=>"inet",
          "scope"=>"link",
          "proto"=>"kernel",
          "src"=>"10.0.128.26"}],
       "link_speed"=>10000,
       "duplex"=>"Full",
       "port"=>"Twisted Pair",
       "transceiver"=>"internal",
       "auto_negotiation"=>"off",
       "mdi_x"=>"Unknown",
       "ring_params"=>
        {"max_rx"=>4096,
         "max_rx_mini"=>0,
         "max_rx_jumbo"=>2048,
         "max_tx"=>4096,
         "current_rx"=>256,
         "current_rx_mini"=>0,
         "current_rx_jumbo"=>128,
         "current_tx"=>512}},
     "eth1"=>
      {"type"=>"eth",
       "number"=>"1",
       "mtu"=>"1500",
       "flags"=>["BROADCAST", "MULTICAST", "UP", "LOWER_UP"],
       "encapsulation"=>"Ethernet",
       "addresses"=>
        {"00:0C:29:1A:64:74"=>{"family"=>"lladdr"},
         "11.11.11.1"=>
          {"family"=>"inet",
           "prefixlen"=>"24",
           "netmask"=>"255.255.255.0",
           "broadcast"=>"11.11.11.1",
           "scope"=>"Global"}},
       "state"=>"up",
       "routes"=>
        [{"destination"=>"default", "family"=>"inet", "via"=>"11.11.11.1"},
         {"destination"=>"11.11.11.1/24",
          "family"=>"inet",
          "scope"=>"link",
          "proto"=>"kernel",
          "src"=>"11.11.11.1"}],
       "link_speed"=>10000,
       "duplex"=>"Full",
       "port"=>"Twisted Pair",
       "transceiver"=>"internal",
       "auto_negotiation"=>"off",
       "mdi_x"=>"Unknown",
       "ring_params"=>
        {"max_rx"=>4096,
         "max_rx_mini"=>0,
         "max_rx_jumbo"=>2048,
         "max_tx"=>4096,
         "current_rx"=>256,
         "current_rx_mini"=>0,
         "current_rx_jumbo"=>128,
         "current_tx"=>512}}}

回答1:

As you want to return top parent key you can do it in a simple way using mentioned answer and find of top hash keys

#return true if find or nil
def deep_key?(obj, key)
  if obj.respond_to?(:key?) && obj.key?(key)
    true
  elsif obj.respond_to?(:each)
    r = nil
    obj.find{ |*a| r = deep_key?(a.last, key) }
    r
  end
end

key = '00:0C:29:1A:64:74'

#now you check if the provided key is a top level key or run search
h.key?(key) ? key : h.find { |k, v| deep_key?(v, key) }.first

The deep_key? it's a bit modified search function from the mentioned answer that returns true if find a key (or nil if don't find). You can use this function inside Hash#find block - top-level key and his value will be the result if found (added first to return just a key).



回答2:

Since you are interested in top-level key, you could do something like this:

hash.find{ |k,v| break k if v.to_s.include? "10.0.128.26" }
#=> eth0

By using v.to_s, we are able to search in the string representation of the hash, and also are able to avoid recursion.