Skip to content

Commit

Permalink
ActiveSupport::OrderedHash[1,2,3,4] creates an OrderedHash instead of…
Browse files Browse the repository at this point in the history
… a Hash.

[#2615 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
dougal authored and jeremy committed May 11, 2009
1 parent 4051dd3 commit 2bcb244
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Expand Up @@ -10,6 +10,16 @@ def initialize(*args, &block)
@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
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/ordered_hash_test.rb
Expand Up @@ -162,4 +162,10 @@ def test_keys
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

0 comments on commit 2bcb244

Please sign in to comment.