ruby tricks: simple caching of method results
This is a simple trick based on the idea that everything in ruby returns a result. By using the block syntax we can do a series of expensive operations and cache the result somewhere. Here I’ve shown the result cached in an instance variable but it may just as well be something more exotic.
@result || begin
# expensive operation
foo = expensive_operation_1
@result = expensive_operation_2 * foo
end
end










No Comments Yet