On 2004-11-01 12:58:27 +0900, John Carter wrote:
> Really an easy optimization to do.
Yep. You could also make it transparent for the clients of your class:
class BigExpensiveMadeOften
def initialize(unique_name)
# Big expensive computation
end
@@memo = Hash.new { |hash,key|
result = BigExpensiveMadeOften.allocate
result.__send__(:initialize, key)
hash[key] = result
}
def BigExpensiveMadeOften.new(unique_name)
@@memo[unique_name]
end
end
foo = BigExpensiveMadeOften.new('foo')
bar = BigExpensiveMadeOften.new('bar')
p foo
p bar
foo = BigExpensiveMadeOften.new('foo')
p foo
--
Florian Frank