The simple
ActiveSupport::OrderedHash.new [[1,2]]
does no longer work in Rails 2.3, so I built a small patch for array that make OrderedHash creation simple again.
def to_ordered_hash
oh = ActiveSupport::OrderedHash.new
each {|k,v| oh[k]=v }
oh
end
#Usage
hash = [[:key, 'value'], [:key2, :value2], [1, 2]].to_ordered_hash
value == hash[:key]

1 comment
Comments feed for this article
October 7, 2009 at 7:46
Tyler Rick
Thanks for this tip! I was very confused when I tried to instantiate a new hash with ActiveSupport::OrderedHash.new the same way I had done before only to get back an empty hash! Adding Array#to_ordered_hash makes instantiation so much nicer. I wish they would add that to core Ruby or Rails.