Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 jeremy authored
Showing with 3 additions and 3 deletions.
  1. +3 −3 activesupport/lib/active_support/memoizable.rb
View
6 activesupport/lib/active_support/memoizable.rb
@@ -14,7 +14,7 @@ def freeze_with_memoizable
methods.each do |method|
if method.to_s =~ /^_unmemoized_(.*)/
begin
- __send__($1)
+ __send__($1).freeze
rescue ArgumentError
end
end
@@ -41,7 +41,7 @@ def #{symbol}(reload = false)
if !reload && defined? #{memoized_ivar}
#{memoized_ivar}
else
- #{memoized_ivar} = #{original_method}.freeze
+ #{memoized_ivar} = #{original_method}
end
end
else
@@ -52,7 +52,7 @@ def #{symbol}(*args)
if !reload && #{memoized_ivar} && #{memoized_ivar}.has_key?(args)
#{memoized_ivar}[args]
else
- #{memoized_ivar}[args] = #{original_method}(*args).freeze
+ #{memoized_ivar}[args] = #{original_method}(*args)
end
end
end

1 comment on commit 6c70c02

@josh
Owner

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.