timpone
timpone

Reputation: 19969

iterate through array of hashes and output key, value

I'd like to do the following:

<% tags=[{"2":"birds"},{"3":"cars"}] %>

<% tags.each do |key, value| %>
   <%=key %> <%=value %>
<% end %>

but this doesn't work. How would I do this (if possible)?

Upvotes: 1

Views: 169

Answers (1)

james00794
james00794

Reputation: 1147

Since each hash can have any number of key/value pairs:

<% tags=[{"2":"birds"},{"3":"cars"}] %>

<% tags.each do |t| %>
   <% t.each do |key,value| %>
      <%=key %> <%=value %>
   <% end %>
<% end %>

Upvotes: 4

Related Questions