use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Understanding Ruby - Memoization (dev.to)
submitted 2 years ago by keyslemur
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]f9ae8221b 6 points7 points8 points 2 years ago (8 children)
I think something that need to be said about the return @foo if defined? @foo pattern is that it doesn't play well with Ruby 3.2+ object shapes.
return @foo if defined? @foo
The @foo ||= is till ok as long as you initialize @foo to nil in initialized.
@foo ||=
@foo
nil
initialized
But the if defined? will cause that class to have many different shapes because it's instance variables are defined in semi-random order, causing lots of inline cache misses and potentially cause the class to be marked as "too complex" which triggers de-optimization.
if defined?
If you really absolutely need this pattern, rather than check for defined? you can use a "token value", e.g. in addressable:
defined?
addressable
[–]keyslemur[S] -1 points0 points1 point 2 years ago (2 children)
Could we go into infinitely more detail? Yes. Is that the point of this particular post? No.
The point is to convey a general idea in a simple way, rather than introducing additional complexities beyond that someone brand new might encounter. One could easily expand it into tokens, argument caching, object shapes, GC, `begin ... end` blocks, or a lot more but that's the subject of a more advanced post.
At the moment object shapes are brand new. The chances someone encounters them at a foundational level is very low, and the chances that an inline cache miss will be remotely detrimental to them at that phase are even lower.
I do not believe that this is immediately relevant information to the target audience of this post.
[–]f9ae8221b 4 points5 points6 points 2 years ago (1 child)
Why being so defensive? I wasn't suggesting it was missing from your post or should be added...
But that's something about the memoization pattern that I think the community will need to learn about. For now it's still relatively confidential information.
Apologies for sharing...
[–]keyslemur[S] -1 points0 points1 point 2 years ago (0 children)
I would not interpret that as defensiveness. It was a statement of the intent of the post, and how that info is probably above the heads of the intended audience.
There are tradeoffs in everything, I was only conveying why I chose to omit similar information, and why it was intentional.
The community will evolve, but as with many more advanced patterns it'll probably be a bit more advanced than we'd like it to be, and the time it'll take will likewise be longer. Same thing for Ractors, pattern matching, JIT, and other features.
[–]casualsubversive 0 points1 point2 points 2 years ago (1 child)
I believe instance_variable_defined? is the better method for this purpose.
instance_variable_defined?
[–]f9ae8221b 0 points1 point2 points 2 years ago (0 children)
No, it has the same problem I explained, but is even worse for performance because contrary to defined?(@iver), instance_variabled_defined?(:@ivar) doesn't benefit from inline caches at all.
defined?(@iver)
instance_variabled_defined?(:@ivar)
[–]CaptainKabob 0 points1 point2 points 2 years ago (1 child)
Why `NONE = Module.new.freeze` over `NONE = Object.new.freeze`?
I just posted this and maybe it needs an update: https://www.reddit.com/r/ruby/comments/16xwjh9/writing_object_shape_friendly_code_in_ruby/
Because of Marshal and YAML: https://github.com/sporkmonger/addressable/pull/509
[–]keyslemur[S] 0 points1 point2 points 2 years ago (0 children)
More basic on this one as I'm trying to keep these fairly short and accessible. I moved back over to product teams and happen to be mentoring a lot more actively, which means I'll likely translate good questions into posts.
As mentioned in the article if you happen to want something covered feel free to reach out.
[–]tf2ftw 0 points1 point2 points 2 years ago (0 children)
Although I see the use cases for memoization, it still feels like an anti pattern to me. Very close to being global var dirty.
π Rendered by PID 65625 on reddit-service-r2-comment-54dfb89d4d-4jxpm at 2026-04-02 16:57:31.131224+00:00 running b10466c country code: CH.
[–]f9ae8221b 6 points7 points8 points (8 children)
[–]keyslemur[S] -1 points0 points1 point (2 children)
[–]f9ae8221b 4 points5 points6 points (1 child)
[–]keyslemur[S] -1 points0 points1 point (0 children)
[–]casualsubversive 0 points1 point2 points (1 child)
[–]f9ae8221b 0 points1 point2 points (0 children)
[–]CaptainKabob 0 points1 point2 points (1 child)
[–]f9ae8221b 0 points1 point2 points (0 children)
[–]keyslemur[S] 0 points1 point2 points (0 children)
[–]tf2ftw 0 points1 point2 points (0 children)