Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit e1854e0b199fba352ddcaa58a3af168e8cc70e3a 1 parent d45d0a1
@dougal dougal authored jeremy committed
View
10 activesupport/lib/active_support/ordered_hash.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
View
6 activesupport/test/ordered_hash_test.rb
@@ -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

@melvinram

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?

@melvinram

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?

@melvinram

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

@runeb

I was just battling with this. Thanks.

http://pastie.org/475420

@melvinram

Gotcha!

Please sign in to comment.
Something went wrong with that request. Please try again.