title: Rails, how to loop either array of hashes or only a hash
body: <p>I am connecting to an API and I get array of hashes or only 1 hash for the data. So when the data comes as array of hashes;</p>
  
  <pre><code>"extras"=&gt;{"extra"=&gt;[{"id"=&gt;"529216700000100800", "name"=&gt;"Transfer Trogir - Dubrovnik (8 persons max)", "price"=&gt;"290.0", "currency"=&gt;"EUR", "timeunit"=&gt;"0", "customquantity"=&gt;"0", "validdatefrom"=&gt;"1970-01-01", "validdateto"=&gt;"2119-07-20", "sailingdatefrom"=&gt;"1970-01-01", "sailingdateto"=&gt;"2119-07-20", "obligatory"=&gt;"0", "perperson"=&gt;"0", "includedinbaseprice"=&gt;"0", "payableoninvoice"=&gt;"1", "availableinbase"=&gt;"-1", "includesdepositwaiver"=&gt;"0", "includedoptions"=&gt;""}, {"id"=&gt;"528978430000100800", "name"=&gt;"Gennaker + extra deposit (HR)", "price"=&gt;"150.0", "currency"=&gt;"EUR", "timeunit"=&gt;"604800000", "customquantity"=&gt;"0", "validdatefrom"=&gt;"1970-01-01", "validdateto"=&gt;"2119-07-19", "sailingdatefrom"=&gt;"1970-01-01", "sailingdateto"=&gt;"2119-07-19", "obligatory"=&gt;"0", "perperson"=&gt;"0", "includedinbaseprice"=&gt;"0", "payableoninvoice"=&gt;"1", "availableinbase"=&gt;"-1", "includesdepositwaiver"=&gt;"0", "includedoptions"=&gt;""}]
  </code></pre>
  
  <p>I'am looping through the array to get the values as;</p>
  
  <pre><code>b["extras"]["extra"].each do |extra|
    puts extra["id"]
    puts extra["name"]
  end
  </code></pre>
  
  <p>But when this is not array; only 1 hash, then this is not working, adding each loop makes it array but not array of hashes;</p>
  
  <pre><code>"extras"=&gt;{"extra"=&gt;{"id"=&gt;"640079840000100800", "name"=&gt;"Comfort package (GRE)", "price"=&gt;"235.0", "currency"=&gt;"EUR", "timeunit"=&gt;"0", "customquantity"=&gt;"0", "validdatefrom"=&gt;"1970-01-01", "validdateto"=&gt;"2120-03-25", "sailingdatefrom"=&gt;"2015-01-01", "sailingdateto"=&gt;"2120-03-25", "obligatory"=&gt;"1", "perperson"=&gt;"0", "includedinbaseprice"=&gt;"0", "payableoninvoice"=&gt;"1", "availableinbase"=&gt;"-1", "includesdepositwaiver"=&gt;"0", "includedoptions"=&gt;""}}
  
  
  b["extras"]["extra"].each do |extra|
    puts extra["id"]
    puts extra["name"]
  end
  </code></pre>
  
  <p>This time, that gives error TypeError (no implicit conversion of String into Integer);</p>
  
  <p>When I type puts <code>extra.inspect</code>; I get <code>["id", "640079840000100800"]</code>. So to make it work I should pass <code>extra[1]</code> to get the id number. </p>
  
  <p>But I can not predict either array of hashes or only hash. Is there any easy way to solve this issue that works either array of hashes or just a hash?</p>