aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-03-25 04:16:53 -0700
committerPiotr Sarnacki <drogus@gmail.com>2012-03-25 04:16:53 -0700
commita4bf3045cadcbdd599383f2116c1e6d259e176ce (patch)
treefa3a12b54f055d15866092783329599d94890001 /activesupport
parent64ccb59c9aba9e9f7186e2332927a911763869bf (diff)
parent2ee28b2bf8414cad3655dbe685ff0d27051395cd (diff)
downloadrails-a4bf3045cadcbdd599383f2116c1e6d259e176ce.tar.gz
rails-a4bf3045cadcbdd599383f2116c1e6d259e176ce.tar.bz2
rails-a4bf3045cadcbdd599383f2116c1e6d259e176ce.zip
Merge pull request #5566 from lest/patch-3
fix HashWithIndifferentAccess.[] method
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb4
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb9
2 files changed, 13 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 49aa012268..91459f3e5b 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -42,6 +42,10 @@ module ActiveSupport
end
end
+ def self.[](*args)
+ new.merge(Hash[*args])
+ end
+
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
alias_method :regular_update, :update unless method_defined?(:regular_update)
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 4544edf0dd..80b3c16328 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -388,6 +388,15 @@ class HashExtTest < ActiveSupport::TestCase
assert_equal expected, hash
end
+ def test_constructor_on_indifferent_access
+ hash = HashWithIndifferentAccess[:foo, 1]
+ assert_equal 1, hash[:foo]
+ assert_equal 1, hash['foo']
+ hash[:foo] = 3
+ assert_equal 3, hash[:foo]
+ assert_equal 3, hash['foo']
+ end
+
def test_reverse_merge
defaults = { :a => "x", :b => "y", :c => 10 }.freeze
options = { :a => 1, :b => 2 }