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:
parent
d45d0a1864
commit
e1854e0b19
2 changed files with 16 additions and 0 deletions
|
@ -10,6 +10,16 @@ module ActiveSupport
|
||||||
@keys = []
|
@keys = []
|
||||||
end
|
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)
|
def initialize_copy(other)
|
||||||
super
|
super
|
||||||
# make a deep copy of keys
|
# make a deep copy of keys
|
||||||
|
|
|
@ -162,4 +162,10 @@ class OrderedHashTest < Test::Unit::TestCase
|
||||||
def test_inspect
|
def test_inspect
|
||||||
assert @ordered_hash.inspect.include?(@hash.inspect)
|
assert @ordered_hash.inspect.include?(@hash.inspect)
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue