matthewrudy/memoist — pull request #14 by undefinedvalue (2014-04-16 02:44:55+00:00)
Title: Allow classes with memoized methods to be reloaded

When developing in Rails it is common to keep an irb console open and rely on autoloading to refresh files as you edit them. However, I found that classes with memoized methods couldn't be reloaded at all due to "already memoized" errors and required restarting irb to get the updates.

Here is what happens:

1. A file with a class is loaded
1. The class is defined
2. Memoist is extended
3. Memoized versions of methods are created
2. The same file is loaded again (e.g. via 'load' or some auto-reload mechanism as in Rails)
1. The class is defined (note: this is now modifying the originally loaded definition)
2. Memoist is extended
3. The first "memoize" raises an "already memoized" error because the original still exists.

This proposed fix is to detect when Memoist is extended and remove previously memoized methods defined in the current class. Then the class can continue loading as normal and re-memoize methods as desired.