Array as Hash value

I have the hardest time getting my head around the following problem,
maybe someone here can help:

I have a Hash that contains Arrays as values - but how do I access the
arrays in html?

This is what I tried:

<% for entry in @hash %>
key: <%= entry[0] %>

<% for element in entry[1] %>
<%= element %>
<% end %>
<% end %>

Now that doesnt work (since entry[1] doesnt seem to be recognized as
Array) and the code looks a bit un-elegant too, if I may say so… how
can I access the elements of the array and is there a way to write this
a bit prettier?



something like this.

@hash.each do |k, v| # block variables k - key, v - value

v.each do |a|



On Fri, Apr 11, 2008 at 12:55 PM, Marc H.

I dont know, I removed all the bells and whistles from the method, used
a static array as input and it simply wont accept that this thing is an
array when I try to output it in HTML… Drives me insane…

Could someone please write out the HTML code for accessing a value-array
in a Hash?

This is what I tried:

The Hash:

@some_hash[“key”] = @some_array


<% for key,values in @some_hash %>
<%= key %>
<% for value in value %>
<%= value %>
<% end %>
<% end %>

But again, “values” is not a array but a string… is it me or is this a
rails-specific thing?

<% @hash.each do |k, v| -%>
<%= k %>
<% v.each do |a| -%>
<%= a %>
<% end -%>
<% end -%>

@hash.each associated with the block will go through each member
of your Hash instance, @hash. the block variable k will hold the
key, and the block variable v will hold the value.

as you initialized the value to be an array, you can then call the Array
method each to go through each member of your array. associated with
a block, the variable a will hold the value of the member.

details, details …
Thx, that worked!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs