Hello, i have problem iterating through hashes, for example I have two hashes: 1. {:item => {:subitem => {data => "yes", otherdata => "yes"}} 2. {:item => {:subitem => [{data => "yes", otherdata => "yes"},{data => "no", otherdata => "yes"}]} I wish with in view iterate through hash and draw table, but using hash.each function, in first example rails iterate through data and other data, but in second case through {},{} not data and other data. Is there any solution how simply solve this problem? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 7 January 2011 22:20, Raitis <raitis.kronbergs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > i have problem iterating through hashes, for example I have two > hashes: > 1. > {:item => {:subitem => {data => "yes", otherdata => "yes"}} > 2. > {:item => {:subitem => [{data => "yes", otherdata => "yes"},{data => > "no", otherdata => "yes"}]} > > I wish with in view iterate through hash and draw table, but using > hash.each function, in first example rails iterate through data and > other data, but in second case through {},{} not data and other data.In the second case, since subitem is an array, you will have to iterate through the array and for each element iterate through the hash. If you are doing that already show us the code (strip out everything that is not required first so that we can concentrate on the important stuff). Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin, that''s means I always need to check hash value data and transform it or use another each cycle? Currently in view I have simple .each iteration: http://pastie.org/1439787 This will work great for second hash, when drawing table. Raitis On Jan 8, 12:07 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 7 January 2011 22:20, Raitis <raitis.kronbe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, > > i have problem iterating through hashes, for example I have two > > hashes: > > 1. > > {:item => {:subitem => {data => "yes", otherdata => "yes"}} > > 2. > > {:item => {:subitem => [{data => "yes", otherdata => "yes"},{data => > > "no", otherdata => "yes"}]} > > > I wish with in view iterate through hash and draw table, but using > > hash.each function, in first example rails iterate through data and > > other data, but in second case through {},{} not data and other data. > > In the second case, since subitem is an array, you will have to > iterate through the array and for each element iterate through the > hash. If you are doing that already show us the code (strip out > everything that is not required first so that we can concentrate on > the important stuff). > > Colin-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 January 2011 10:49, Raitis <raitis.kronbergs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Colin, that''s means I always need to check hash value data and > transform it or use another each cycle?Please don''t top post, it makes it difficult to follow the thread. Insert your reply at the appropriate points in the previous message. Thanks.> > Currently in view I have simple .each iteration: > http://pastie.org/1439787Since that is only six lines you could easily have inserted it inline: <% @search_results[:item][:subitem].each do |key, value| %> <tr> <td><%= key %></td> <td><%= value %></td> </tr> <% end %> Think about the code. If @search_results[:item][:subitem] is a hash as in your first example then this is using Hash.each do |key, value| which is what you want. If it is in fact an array, as in your second example this is going to use Array.each do |key, value| which is meaningless I think. Hopefully you know whether it is an array or not and can use a nested each block to iterate through the array. Perhaps you can make it always an array which sometimes only has one entry to simplify the code. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.