Using hash.keys - why doesn't it display in order created

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


<[email protected] do |mod|%>
<%= check_box_tag(“mods[]”, mod ,false, {:class =>
“class”}) %>
<%= mod%>

<%end%>

Hi~

On Jan 30, 2007, at 4:45 PM, Kim wrote:

Please help - thanks, K
Assignment’ => AssignResource, ‘Style Guides’ => StyleResource,
end
end

View


<[email protected] do |mod|%>
<%= check_box_tag(“mods[]”, mod ,false, {:class =>
“class”}) %>
<%= mod%>

<%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 Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

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

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?

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.

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.

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 F.s
(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).