I have the following hash:
{
"subtype"=>"example subtype",
"contributors"=> {
"Concept"=>["Example Contributor", "Example Contributor"],
"Editor"=>["Example Contributor", "Example Contributor"],
"Photographer"=>["Example"]
},
"publisher"=>"Example Publisher",
"language"=>"Example Language",
"dimensions"=>"Example Dimensions"
}
Where one can see that it is a hash of hashes, and some have string values and some have array values. I was wondering how I can iterate through this array so that I could have the following output, for example html:
<h3>example subtype</h3>
<h3>Concept</h3>
<p>Example Contributor, Example Contributor</p>
<h3>Editor</h3>
<p>Example Contributor, Example Contributor</p>
<h3>Photographer</h3>
<p>Example</p>
<h3>Publisher</h3>
<p>Example Publisher</p>
<h3>Language</h3>
<p>Example Language</p>
<h3>Dimensions</h3>
<p>Example Dimensions</p>
So far, I am trying to iterate through the array so following this answer (haml):
- object_details.each do |key, value|
%h3= key
- value.each do |v|
%p= v
Which of course fails immediately as the first item, subtype
has no method each
as it is not an array.
I would take each value manually, but the hash may change if values are present or absent (for example, publisher or language may not always be present in the hash)
Is there a smart way to iterate through this hash other than checking for the presence of each hash manually?