Skip to content

Instantly share code, notes, and snippets.

@jakeboxer
Created June 18, 2013 06:44
Show Gist options
  • Select an option

  • Save jakeboxer/5803136 to your computer and use it in GitHub Desktop.

Select an option

Save jakeboxer/5803136 to your computer and use it in GitHub Desktop.
Memoizing methods with options
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
@nakajima
Copy link

@jakeboxer
Copy link
Author

Didn't Ruby start at 1.8.7?

@jumph4x
Copy link

jumph4x commented Jun 18, 2013

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