Hello there, I have the hash:
@hash = {"a"=>"1", "b"=>"2", "c"=>"3"}
And I need to create an array:
@array = [ ['a','1'],['b','2'],['c','3'] ]
How do I do that?
Paco Reyes
Hello there, I have the hash:
@hash = {"a"=>"1", "b"=>"2", "c"=>"3"}
And I need to create an array:
@array = [ ['a','1'],['b','2'],['c','3'] ]
How do I do that?
Paco Reyes
@hash.to_a
Hi --
Either: @array = @hash.to_a
or: @array = @hash.to_a.sort
depending on whether the order matters. A Hash (in Ruby 1.8.6 and earlier) is inherently unordered and the conversion to an array gives elements in the same order that @hash.each would yield them.
-Rob
Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com
Rob, Could I ride this thread with a short variation? I have a zillion dropdowns in my project that ask users what code they should choose. As a ficticious example they'd be asked what day of the week ( 1 - Sunday, 2 - Monday, 3 - Tuesday, etc. ) I'd like to simply create this as an array and pass it to my collection_select helper. This would save me having to create another model and migration. What throws me is that the first member of an array has a zero 0 key and I want it to be one 1. Could someone show me the simple way to load an array with key - value combinations that starts with one 1. Thank you, Kathleen