diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-08-13 21:30:46 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-08-13 21:30:46 -0500 |
commit | 8cb14ee1203c9ed380c4b192e8730757a52d43cb (patch) | |
tree | d69be4eac1b7ecd0457de284549e28d729f40914 /activesupport | |
parent | b8b30985d525fd15b6c16d29fc115e83e3ee5037 (diff) | |
download | rails-8cb14ee1203c9ed380c4b192e8730757a52d43cb.tar.gz rails-8cb14ee1203c9ed380c4b192e8730757a52d43cb.tar.bz2 rails-8cb14ee1203c9ed380c4b192e8730757a52d43cb.zip |
Ensure results returned by a memoized method are immutable
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/memoizable.rb | 4 | ||||
-rw-r--r-- | activesupport/test/memoizable_test.rb | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index 5aa73bce52..6506238ac0 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -51,7 +51,7 @@ module ActiveSupport if instance_method(:#{symbol}).arity == 0 def #{symbol}(reload = false) if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? - #{memoized_ivar} = [#{original_method}] + #{memoized_ivar} = [#{original_method}.freeze] end #{memoized_ivar}[0] end @@ -64,7 +64,7 @@ module ActiveSupport if !reload && #{memoized_ivar}.has_key?(args) #{memoized_ivar}[args] elsif #{memoized_ivar} - #{memoized_ivar}[args] = #{original_method}(*args) + #{memoized_ivar}[args] = #{original_method}(*args).freeze end else #{original_method}(*args) diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb index 99d9240ea1..135d56f14a 100644 --- a/activesupport/test/memoizable_test.rb +++ b/activesupport/test/memoizable_test.rb @@ -110,6 +110,11 @@ uses_mocha 'Memoizable' do assert_equal 1, @person.age_calls end + def test_memorized_results_are_immutable + assert_equal "Josh", @person.name + assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") } + end + def test_reloadable counter = @calculator.counter assert_equal 1, @calculator.counter |