title: Ruby - Hash to Array - Accessing Keys
body: <p>I'm a little uncomfortable using chain of methods in Ruby. Here, I'm using such a chain.</p>
  
  <p>So the question is, Tell me a better way to do the same.</p>
  
  <p>I have this line run,</p>
  
  <pre><code>p["All_Products"]["Product"].map{|h| h["Categories"]["Category"] rescue nil }.compact.flatten.uniq
  </code></pre>
  
  <p><code>p</code> equals this,</p>
  
  <pre><code>{"All_Products"=&gt;
    {"Product"=&gt;
      [{"ProductCode"=&gt;"GFT",
        "Categories"=&gt;
         {"Category"=&gt;
           [{"CategoryID"=&gt;"1513", "CategoryName"=&gt;"Beauty"},
            {"CategoryID"=&gt;"1515", "CategoryName"=&gt;"Specials"},
            {"CategoryID"=&gt;"1553", "CategoryName"=&gt;"Bath and Body"}]}},
       {"ProductCode"=&gt;"GF-honey",
        "Categories"=&gt;
         {"Category"=&gt;
           [{"CategoryID"=&gt;"1514", "CategoryName"=&gt;"Gourmet Food"},
            {"CategoryID"=&gt;"1515", "CategoryName"=&gt;"Specials"},
            {"CategoryID"=&gt;"1632", "CategoryName"=&gt;"Specialty Items"}]}},
       {"ProductCode"=&gt;"GF-malbec",
        "Categories"=&gt;
         {"Category"=&gt;
           [{"CategoryID"=&gt;"1514", "CategoryName"=&gt;"Gourmet Food"},
            {"CategoryID"=&gt;"1632", "CategoryName"=&gt;"Specialty Items"}]}},
       {"ProductCode"=&gt;"GF-olives",
        "Categories"=&gt;
         {"Category"=&gt;
           [{"CategoryID"=&gt;"1514", "CategoryName"=&gt;"Gourmet Food"},
            {"CategoryID"=&gt;"1632", "CategoryName"=&gt;"Specialty Items"}]}},
       {"ProductCode"=&gt;"HC-flatiron",
        "Categories"=&gt;
         {"Category"=&gt;
           [{"CategoryID"=&gt;"1513", "CategoryName"=&gt;"Beauty"},
            {"CategoryID"=&gt;"1515", "CategoryName"=&gt;"Specials"},
            {"CategoryID"=&gt;"1554", "CategoryName"=&gt;"Hair Care"}]}}
      ]
    } 
  }
  </code></pre>
  
  <p>Finally I'm getting this,</p>
  
  <pre><code>[{"CategoryID"=&gt;"1513", "CategoryName"=&gt;"Beauty"},
   {"CategoryID"=&gt;"1515", "CategoryName"=&gt;"Specials"},
   {"CategoryID"=&gt;"1553", "CategoryName"=&gt;"Bath and Body"},
   {"CategoryID"=&gt;"1514", "CategoryName"=&gt;"Gourmet Food"},
   {"CategoryID"=&gt;"1632", "CategoryName"=&gt;"Specialty Items"},
   {"CategoryID"=&gt;"1554", "CategoryName"=&gt;"Hair Care"},
   {"CategoryID"=&gt;"1673", "CategoryName"=&gt;"Sweets"},
   {"CategoryID"=&gt;"1477", "CategoryName"=&gt;"Home Decor"},
   {"CategoryID"=&gt;"1516", "CategoryName"=&gt;"Furniture"},
   {"CategoryID"=&gt;"1517", "CategoryName"=&gt;"Home Accessories"},
   {"CategoryID"=&gt;"1814", "CategoryName"=&gt;"Apparel"},
   {"CategoryID"=&gt;"1815", "CategoryName"=&gt;"Women"},
   {"CategoryID"=&gt;"1816", "CategoryName"=&gt;"Men"}]
  </code></pre>