Permalink
Browse files

Freeze memoized results when instance is frozen instead of immediatel…

…y so you can memoize mutable objects
  • Loading branch information...
jeremy committed Aug 8, 2008
1 parent a805766 commit 6c70c02c83b3a03c88df4286130be8882f6d201c
Showing with 3 additions and 3 deletions.
  1. +3 −3 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

This comment has been minimized.

Show comment Hide comment
@josh

josh Aug 14, 2008

Member

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.

Member

josh commented on 6c70c02 Aug 14, 2008

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.