Created
June 18, 2013 06:44
-
-
Save jakeboxer/5803136 to your computer and use it in GitHub Desktop.
Memoizing methods with options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Eminem | |
| def spaghetti(options={}) | |
| @spaghetti ||= {} | |
| @spaghetti[options] ||= Spaghetti.new(options) | |
| end | |
| end | |
| e = Eminem.new | |
| e.spaghetti # Calculates | |
| e.spaghetti # Doesn't calculate | |
| e.spaghetti(:creator => 'mom') # Calculates | |
| e.spaghetti(:creator => 'mom') # Doesn't calculate |
Author
Didn't Ruby start at 1.8.7?
Easy to fix with http://www.ruby-doc.org/core-2.0/Hash.html#method-i-hash however, doesn't address key ordering and potential symbol/string misses
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might be kinda burnt in 1.8.6: http://stackoverflow.com/questions/5075584/ruby-hash-as-key-to-a-hash