Note that there are some explanatory texts on larger screens.

plurals
  1. POaccess variables in array?
    text
    copied!<p>I have a Savon SOAP session where I get a response that is converted to an array. Part of this response is then stored in an variable package containing a hash like this;</p> <pre><code>{ :cancellation_policies=&gt;nil, :contract_id=&gt;"834", :hotel_id=&gt;"41298", :package_id=&gt;"6b0938e-bf45ed4facda", :package_price=&gt;{ :currency=&gt;"NOK", :final_price=&gt;"1400.05", :final_price_in_supplier_currency=&gt;"169", :original_price=&gt;"1400.05", :original_price_in_supplier_currency=&gt;"169", :supplier_currency=&gt;"GBP" }, :rooms=&gt;{ :room=&gt;{ :adults_count=&gt;"2", :availability=&gt;"Available", :description=&gt;{ :"@xmlns:a"=&gt;"http://schemas.microsoft.com/2003/10/Serialization/Arrays" }, :id=&gt;"6de98b4b-256e-47de-bbd3-700602a16e8e", :kids_ages=&gt;{ :"@xmlns:a"=&gt;"http://schemas.microsoft.com/2003/10/Serialization/Arrays" }, :price=&gt;{ :currency=&gt;"NOK", :final_price=&gt;"1419.05", :final_price_in_supplier_currency=&gt;"165", :original_price=&gt;"1419.05", :original_price_in_supplier_currency=&gt;"165", :supplier_currency=&gt;"GBP" }, :room_basis=&gt;"Bed and Breakfast BB", :room_basis_original=&gt;"Room and Breakfast (American Buffet Breakfast)", :room_class=&gt;"Standard", :room_class_original=&gt;"Double Room", :room_type=&gt;"Double", :room_type_original=&gt;"Double Room" } }, :supplier_id=&gt;"21", :supplier_name=&gt;"HTP" } </code></pre> <p>The strange thing happens when i try to access package[:cancellation_policies] i get an error saying; can't convert Symbol into Integer. </p> <p>How do I access for instance :cancellation_policies ? </p> <p>package.inspect gives;</p> <pre><code>{ :cancellation_policies=&gt;nil, :contract_id=&gt;"834", :hotel_id=&gt;"41298", :package_id=&gt;"4e80e642-13eb-48d6-9f1e-8b35f1c406ca", :package_price=&gt;{:currency=&gt;"NOK", :final_price=&gt;"1677.06", :final_price_in_supplier_currency=&gt;"195", :original_price=&gt;"1677.06", :original_price_in_supplier_currency=&gt;"195", :supplier_currency=&gt;"GBP"}, :rooms=&gt;{:room=&gt;{:adults_count=&gt;"2", :availability=&gt;"Available", :description=&gt;{:"@xmlns:a"=&gt;"http://schemas.microsoft.com/2003/10/Serialization/Arrays"}, :id=&gt;"dce10420-bbc5-471e-b388-03d6bb132e27", :kids_ages=&gt;{:"@xmlns:a"=&gt;"http://schemas.microsoft.com/2003/10/Serialization/Arrays"}, :price=&gt;{:currency=&gt;"NOK", :final_price=&gt;"1677.06", :final_price_in_supplier_currency=&gt;"195", :original_price=&gt;"1677.06", :original_price_in_supplier_currency=&gt;"195", :supplier_currency=&gt;"GBP"}, :room_basis=&gt;"Bed and Breakfast BB", :room_basis_original=&gt;"Breakfast (Full Breakfast)", :room_class=&gt;"Standard", :room_class_original=&gt;"Standard Double - Double", :room_type=&gt;"Double", :room_type_original=&gt;"Standard Double - Double"}}, :supplier_id=&gt;"21", :supplier_name=&gt;"HTP"} </code></pre> <p>(As far as I can tell the same output as above?)</p> <p>So I tried with; </p> <pre><code>&lt;% package.each do |key, value| %&gt; &lt;%= key %&gt; is &lt;%= value %&gt; &lt;% end %&gt; </code></pre> <p>which nicely prints contract_id is 834 hotel_id is 41298</p> <p>...and so on, if that clarifies anything?</p> <p>So temporary thing that solves it for me;</p> <pre><code>package_as_hash = Hash.new package.each do |key, value| package_as_hash[key] = value end </code></pre> <p>Highly doubt its the most efficient way but at least it works... </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload