1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

ActiveSupport::OrderedHash[1,2,3,4] creates an OrderedHash instead of a Hash.

[#2615 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Douglas F Shearer 2009-05-07 23:58:07 +01:00 committed by Jeremy Kemper
parent d45d0a1864
commit e1854e0b19
2 changed files with 16 additions and 0 deletions

View file

@ -10,6 +10,16 @@ module ActiveSupport
@keys = []
end
def self.[](*args)
ordered_hash = new
args.each_with_index { |val,ind|
# Only every second value is a key.
next if ind % 2 != 0
ordered_hash[val] = args[ind + 1]
}
ordered_hash
end
def initialize_copy(other)
super
# make a deep copy of keys

View file

@ -162,4 +162,10 @@ class OrderedHashTest < Test::Unit::TestCase
def test_inspect
assert @ordered_hash.inspect.include?(@hash.inspect)
end
def test_alternate_initialization
alternate = ActiveSupport::OrderedHash[1,2,3,4]
assert_kind_of ActiveSupport::OrderedHash, alternate
assert_equal [1, 3], alternate.keys
end
end