Class: ActiveSupport::OrderedHash

Overview

The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the order in which keys will return keys, or each yield pairs. ActiveSupport::OrderedHash implements a hash that preserves insertion order, as in Ruby 1.9:

oh=ActiveSupport::OrderedHash.newoh[:a]=1oh[:b]=2oh.keys# => [:a, :b], this order is guaranteed

ActiveSupport::OrderedHash is namespaced to prevent conflicts with other implementations.

Class Method Details

# File 'activesupport/lib/active_support/ordered_hash.rb', line 73defself.[](*args)ordered_hash=newif(args.length==1&&args.first.is_a?(Array))args.first.eachdo|key_value_pair|nextunless(key_value_pair.is_a?(Array))ordered_hash[key_value_pair[0]]=key_value_pair[1]endreturnordered_hashendunless(args.size%2==0)raiseArgumentError.new("odd number of arguments for Hash")endargs.each_with_indexdo|val,ind|nextif(ind%2!=0)ordered_hash[val]=args[ind+1]endordered_hashend