| ARGH! I just totally contradicted a previous statement (which we've both seemed to ignore). Hashes are not meant to be sorted! The order of keys or values is not guaranteed. That's what arrays are for. So, the simple solution here is to say:
@users.to_csv(: ['username','password']).sort.each do |arr| key = arr[0] value = arr[1] # ... end
-Jordan On Aug 30, 2006, at 12:45 AM, Jordan A. Fowler wrote: Chris,
I've tried to come up with a really pretty way to sort Hashes, and this is the best i've come up with (using your example):
sorted_hash = Hash.new @users.to_csv(: ['username','password']).sort.collect {|arr| {arr[0] => arr[1]}}.each {|hsh| sorted_hash[hsh.keys.first] = hsh[hsh.keys.first]} On Aug 30, 2006, at 12:28 AM, Chris Abad wrote: wait, sorry.... jumped the gun on that one. i just realized why i wanted to jump through the hoops of using attributes.keys and attributes.values rather than class.column_names.
the attributes method supports the :only and :except options, so I could do something like this:
@users.to_csv(: ['username','password'])
On Aug 30, 2006, at 12:23 AM, Chris Abad wrote: perfect... i think this is what i was looking for. thanks kevin.
_______________________________________________ Sdruby mailing list
_______________________________________________ Sdruby mailing list |
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby