How do I iterate a HashWithIndifferentAccess? I need to set the order using a sortable_element. -- 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-/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.
first check out the class of the object, if its a hash do HashWithIndifferentAccess.each do |key, value| puts key puts value end -- 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 21 September 2010 21:45, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > first check out the class of the object, if its a hash do > > HashWithIndifferentAccess.each do |key, value| > puts key > puts value > endif it''s a hash, its order is not guaranteed... next time you iterate it, you might find it''s in a different order -- 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.
Pål Bergström wrote:> How do I iterate a HashWithIndifferentAccess? I need to set the order > using a sortable_element.As Michael said, order is not guaranteed -- even if you include an element for that. When I need to count on the order I use an array of pairs. I use this for value list data. It''s not as handy as Lasso''s array of pairs which are sortable and searchable, but it''s a close as we get. list = [ ["Small", "S"], ["Medium","M"], ["Large","L"] ] If you have to start with the Hash, and it includes a sort number of some type, then you can modify the conversion to specifically retrieve the by the sort number. If you need help with that, give an exampe of the exact data structure you''d be starting with. -- gw -- 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-/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.
Greg Willits wrote:> If you need help with that, give an exampe of the exact data structure > you''d be starting with. >With the help of sortable_element and :tree set as true (and moving up item 3 before item 1, just as a test) I get a Hash from this list: <ul> <li id="3"></li> <li id="1"></li> <li id="5"><ul><li id="6"></li></ul></li> <li id="7"></li> </ul> The hash: {"0"=>{"id"=>"3"}, "1"=>{"id"=>"1"}, "2"=>{"0"=>{"id"=>"6"}, "id"=>"5", "1"=>{"id"=>"7"}}} I can sort out the key and value. But with key nr 2 I have problem (id 5 with sub list containing id 6). If I use params[:mylist].each do |k,v| I only get the key of 2 and value 5. How do I deal with a nested hash like that? I hope I make sense. -- 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-/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.
i hash inside a hash i read it list[:outer_value][:deeper_value] and you can do list.each do |key,value| if value.class == ''Hash'' puts "values for key #{ key}" value.each do | key2,value2| puts "key: #{key2} and value: #{value2}" end else puts "key: #{key} and value: #{value}" end end 2010/9/22 Pål Bergström <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Greg Willits wrote: > > > If you need help with that, give an exampe of the exact data structure > > you''d be starting with. > > > > With the help of sortable_element and :tree set as true (and moving up > item 3 before item 1, just as a test) I get a Hash from this list: > > > <ul> > <li id="3"></li> > <li id="1"></li> > <li id="5"><ul><li id="6"></li></ul></li> > <li id="7"></li> > </ul> > > The hash: > > {"0"=>{"id"=>"3"}, "1"=>{"id"=>"1"}, "2"=>{"0"=>{"id"=>"6"}, "id"=>"5", > "1"=>{"id"=>"7"}}} > > I can sort out the key and value. But with key nr 2 I have problem (id 5 > with sub list containing id 6). If I use params[:mylist].each do |k,v| I > only get the key of 2 and value 5. How do I deal with a nested hash like > that? > > I hope I make sense. > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.
radhames brito wrote:> i hash inside a hash i read it list[:outer_value][:deeper_value] and > you > can do > > list.each do |key,value| > if value.class == ''Hash'' > puts "values for key #{ key}" > value.each do | key2,value2| > puts "key: #{key2} and value: #{value2}" > end > else > puts "key: #{key} and value: #{value}" > end > end >Perfect. Thanks. -- 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-/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.
Possibly Parallel Threads
- Parsing Asterisk's .conf files from Perl, Java or PHP file
- using "aggregate" when variable names contain spaces
- [PATCH: host-browser replacement 0/3] replacement of host-browser on ovirt-server
- reading in a (very simple) list from a file
- Serialized attribute saved as HashWithIndifferentAccess in database