user6127511 user6127511 - 2 years ago 83
Ruby Question

ruby - iterate through a hash containing key/array value pairs, and iterate through each value

I have an hash that looks like this:

"key1": [
"key2": [
"key3": [
"key4": [

How do I iterate through every keyN, while also looping through all the values in that key?

I have an array with all the keys if that helps.


Answer Source

Pretty simple, really:

hash.each do |name, values|
  values.each do |value|
    # ...

You can do whatever you want with name and value at the lowest level.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download