Hi, 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] %><br> <% 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? Cheers, Marc -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
something like this. @hash.each do |k, v| # block variables k - key, v - value v.each do |a| end end On Fri, Apr 11, 2008 at 12:55 PM, Marc Hoeppner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > 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] %><br> > <% 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? > > Cheers, > > Marc > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 In HTML: <% 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<% @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 current 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
details, details ... Thx, that worked! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---