Skip to content
This repository
Browse code

Freeze memoized results when instance is frozen instead of immediatel…

…y so you can memoize mutable objects
  • Loading branch information...
commit 6c70c02c83b3a03c88df4286130be8882f6d201c 1 parent a805766
Jeremy Kemper jeremy authored

Showing 1 changed file with 3 additions and 3 deletions. Show diff stats Hide diff stats

  1. +3 3 activesupport/lib/active_support/memoizable.rb
6 activesupport/lib/active_support/memoizable.rb
@@ -14,7 +14,7 @@ def freeze_with_memoizable
14 14 methods.each do |method|
15 15 if method.to_s =~ /^_unmemoized_(.*)/
16 16 begin
17   - __send__($1)
  17 + __send__($1).freeze
18 18 rescue ArgumentError
19 19 end
20 20 end
@@ -41,7 +41,7 @@ def #{symbol}(reload = false)
41 41 if !reload && defined? #{memoized_ivar}
42 42 #{memoized_ivar}
43 43 else
44   - #{memoized_ivar} = #{original_method}.freeze
  44 + #{memoized_ivar} = #{original_method}
45 45 end
46 46 end
47 47 else
@@ -52,7 +52,7 @@ def #{symbol}(*args)
52 52 if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args)
53 53 #{memoized_ivar}[args]
54 54 else
55   - #{memoized_ivar}[args] = #{original_method}(*args).freeze
  55 + #{memoized_ivar}[args] = #{original_method}(*args)
56 56 end
57 57 end
58 58 end

1 comment on commit 6c70c02

Joshua Peek
Collaborator

What issues did you run into with freezing memoized returned values? I think it would be smart to stop others from modifying the cache.

def hello; “Hello”; end memoize :hello hello #=> “Hello” hello.gsub!(“.*”, “b00m”) hello #=> “b00m”

We could dup the returning value, but what a waste of time.

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