RogerPodacter/Code-Samples — comment on commit 297ca275086ffd9bab7e38513cbe8e5ae3f1f375
File: whittle-inverse-r.rb line 55
By RogerPodacter:

this is a really good point and something i hadn't considered. this is indeed a case in which the recursive solution is actually faster

you can abstract the caching by using `ActiveSupport::Memoizable`:

extend ActiveSupport::Memoizable
def factorial n
n * factorial(n - 1)
end
memoize :factorial

also, i'd give this method a name that denotes the fact that it should only be used in a special case – the name `factorial` should be reserved for a more generic method that works well in all cases (like your `slow_factorial` method below, which i assume was named jokingly ;). i'd name this method `factorial_optimized_for_linear_search` or something (i have a high tolerance for long method names as long as they're descriptive)