Permalink
Browse files

ActiveSupport::OrderedHash[1,2,3,4] creates an OrderedHash instead of…

… a Hash.

[#2615 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent d45d0a1 commit e1854e0b199fba352ddcaa58a3af168e8cc70e3a @dougal dougal committed with jeremy May 7, 2009
Showing with 16 additions and 0 deletions.
  1. +10 −0 activesupport/lib/active_support/ordered_hash.rb
  2. +6 −0 activesupport/test/ordered_hash_test.rb
@@ -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
@@ -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

5 comments on commit e1854e0

I don't understand this commit. I just did:

alternate = ActiveSupport::OrderedHash[1,2,3,4]
=> #<OrderedHash {1=>2, 3=>4}>
alternate.class
=> ActiveSupport::OrderedHash
alternate.kind_of?(Hash)
=> true
alternate.kind_of?(ActiveSupport::OrderedHash)
=> true

inside of the console of a rails 2.3.2 project and inside an edge rails project console ... and they both returned exactly the same values.

Can someone help me understand what this change does?

Github ate my comment. Let's try this again:

I don't understand this commit. I just did:


>> alternate = ActiveSupport::OrderedHash[1,2,3,4]
=> #2, 3=>4}>
>> alternate.class
=> ActiveSupport::OrderedHash
>> alternate.kind_of?(Hash)
=> true
>> alternate.kind_of?(ActiveSupport::OrderedHash)
=> true

inside of the console of a rails 2.3.2 project and inside an edge rails project console ... and they both returned exactly the same values.

Can someone help me understand what this change does?

It ate it again... here's the comment inside a pastie: http://pastie.org/475402

I was just battling with this. Thanks.

http://pastie.org/475420

Gotcha!

Please sign in to comment.