Skip to content

Commit

Permalink
Freeze memoized results when instance is frozen instead of immediatel…
Browse files Browse the repository at this point in the history
…y so you can memoize mutable objects
  • Loading branch information
jeremy committed Aug 8, 2008
1 parent a805766 commit 6c70c02
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/memoizable.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

1 comment on commit 6c70c02

@josh
Copy link
Contributor

@josh josh commented on 6c70c02 Aug 14, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.