I am using .keys on a hash to create an array that is than displayed in a view with .each - why does it not display the keys in the order added to the hash? I would like to controll how the array is displayed - I want the checkboxes to display like I added them to the hash, but it doesn''t - I can''t figure out how it is displayed, when I change the order of the hash it does little to the view. Please help - thanks, K Code Controller ----------------- # creates my hash def mod_types {''Learning Objects'' => LearnObjResource, ''RSS Feed'' => RssResource, ''Blank Module'' => BlankResource,''Course Librarian'' => LibResource, ''Professor Information'' => InstResource, ''Course Assignment'' => AssignResource, ''Style Guides'' => StyleResource, ''Plagiarism Information'' => PlagResource, ''Instructor Recommends'' => RecomResource, ''Course Reserves'' => ReserveResorce } end #action for view def list_modules @mods = @user.modules @list = mod_types.keys # here I am making an array out of the hash if request.post? redirect_to :action => "manage_modules" end end View ----------- ... <%@list.each do |mod|%> <%= check_box_tag("mods[]", mod ,false, {:class => "class"}) %> <%= mod%><br /> <%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-/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 -~----------~----~----~----~------~----~------~--~---
Ezra Zygmuntowicz
2007-Jan-31 00:53 UTC
Re: Using hash.keys - why doesn''t it display in order created
Hi~ On Jan 30, 2007, at 4:45 PM, Kim wrote:> > I am using .keys on a hash to create an array that is than displayed > in a view with .each - why does it not display the keys in the order > added to the hash? > > I would like to controll how the array is displayed - I want the > checkboxes to display like I added them to the hash, but it doesn''t - > I can''t figure out how it is displayed, when I change the order of the > hash it does little to the view. > > Please help - thanks, K > > Code > > Controller > ----------------- > # creates my hash > def mod_types > {''Learning Objects'' => LearnObjResource, ''RSS Feed'' => > RssResource, ''Blank Module'' => BlankResource,''Course Librarian'' => > LibResource, ''Professor Information'' => InstResource, ''Course > Assignment'' => AssignResource, ''Style Guides'' => StyleResource, > ''Plagiarism Information'' => PlagResource, ''Instructor Recommends'' => > RecomResource, ''Course Reserves'' => ReserveResorce } > end > > #action for view > def list_modules > @mods = @user.modules > @list = mod_types.keys # here I am making an array out of the hash > if request.post? > redirect_to :action => "manage_modules" > end > end > > View > ----------- > ... > <%@list.each do |mod|%> > <%= check_box_tag("mods[]", mod ,false, {:class => > "class"}) %> > <%= mod%><br /> > <%end%>Hashes are unordered data structures. You can''t get them in order inserted with a normal hash. You will have to use an array or google for an ordered hash class for ruby. Cheers- -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez-NLltGlunAUd/unjJdyJNww@public.gmane.org -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Jan-31 00:57 UTC
Re: Using hash.keys - why doesn''t it display in order created
Hi Kim, Hashes are, by definition, unordered. Ruby does provide the sort method for Hash, though. See pg. 499 in Pickaxe for more info. hth Bill --~--~---------~--~----~------------~-------~--~----~ 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 don''t want to sort the hash - I want to determine the order the values from the hash are displayed. I can''t use an array because of what I need to be able to do after users select values. The view uses the keys form the hash - is there any way that I can controll the order they are displayed? On Jan 30, 4:57 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Kim, > > Hashes are, by definition, unordered. Ruby does provide the sort method for > Hash, though. See pg. 499 in Pickaxe for more info. > > hth > Bill--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris Hall
2007-Jan-31 18:57 UTC
Re: Using hash.keys - why doesn''t it display in order created
as stated previously, Hash.sort, which you don''t want to do, so then the answer is no, that''s not to say you can''t roll your own solution. off the top of my head... def mod_types [ { key1 => val1 }, {key2 => val2}, ...] end def list_modules ... @list = mod_types.map { |mod_type| mod_type.keys.first } ... end your view shouldn''t have to change. i would imagine it wouldn''t take much to modify whatever it is you are doing with users, but since you didn''t provide any details on that, I can''t provide any possible solutions. On 1/31/07, Kim <Kim.Griggs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I don''t want to sort the hash - I want to determine the order the > values from the hash are displayed. I can''t use an array because of > what I need to be able to do after users select values. > > The view uses the keys form the hash - is there any way that I can > controll the order they are displayed? > > On Jan 30, 4:57 pm, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: > > Hi Kim, > > > > Hashes are, by definition, unordered. Ruby does provide the sort method for > > Hash, though. See pg. 499 in Pickaxe for more info. > > > > hth > > Bill > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Benjamin Ritcey
2007-Jan-31 18:57 UTC
Re: Using hash.keys - why doesn''t it display in order created
Kim wrote:> I don''t want to sort the hash - I want to determine the order the > values from the hash are displayed. I can''t use an array because of > what I need to be able to do after users select values. > > The view uses the keys form the hash - is there any way that I can > controll the order they are displayed? >AFAIK, not out of the box, but there are ordered hash implementations available; check out the Dictionary class from Ruby Facets (http://facets.rubyforge.org/api/more/classes/Dictionary.html). Grab the code from http://facets.rubyforge.org/repo/lib/facets/more/dictionary.rb and you can use it in your app (drop it in ''lib'' and require it as needed). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dictionary looks good except it does not have some of the methods that hash has that I need - thanks I went with hash.keys.sort - not really what I want but at least it is in alphabetically order. On Jan 31, 10:57 am, Benjamin Ritcey <brit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Kim wrote: > > I don''t want to sort the hash - I want to determine the order the > > values from the hash are displayed. I can''t use an array because of > > what I need to be able to do after users select values. > > > The view uses the keys form the hash - is there any way that I can > > controll the order they are displayed? > > AFAIK, not out of the box, but there are ordered hash implementations > available; check out the Dictionary class from Ruby Facets > (http://facets.rubyforge.org/api/more/classes/Dictionary.html). > > Grab the code from > > http://facets.rubyforge.org/repo/lib/facets/more/dictionary.rb > > and you can use it in your app (drop it in ''lib'' and require it as needed).--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---