Funny! I actually came across this article yesterday when trying to look into the current state of Ruby's global method cache. I had a question about the write-up, so maybe I can ask it here!
The page says:
> This is now no longer an issue, Ruby 2.1 uses a method cache based on the class hierarchy, invalidating the cache for only the class in question and any subclasses.
How does this work with an instance of a class? Does it invalidate only that instance's method cache, or all instances in that class? If I do DCI-style:
@user.extend(PasswordConfirmable)
Does that blow away the cache for all User instances, or just that one?
Your example isn't conclusive that what you're saying is true. Your example is just showing that the extended module only appears in the instance's singleton class, this has nothing to do with method cache. It's how the method cache is implemented that matters. The new method caching changes can't be seen from user code.
I'm curious as to whether the ruby-core team implemented method caches for every Object in the system (eg: every instance of User would then have its own method cache because they all would have their own singleton classes) or just objects that are instances of the Class class. I seem to doubt that the former happened because it seems like it would be expensive to maintain a cache for every object, that means a normal Rails request that generates 65,000 strings would all have their own method caches. Only instances of the Class class having their own method caches sounds more reasonable from a performance standpoint to me.
For what it's worth, I've been trying to get an answer about this from a ruby-core member, although my attempts have been quite lazy through Twitter.
> Your example is just showing that the extended module only appears in the instance's singleton class, this has nothing to do with method cache. It's how the method cache is implemented that matters.
The method cache works by invalidating anything below the class(es) whose methods have changed on the ancestor chain.
You're right that it can't be seen from user code, but if you take the implementers at their word about how it works, then this should be a perfectly convincing demonstration.
Or, if you don't believe me, read the source yourself:
There's some great little improvements in there, to_h, required keywords and being able to decorate methods are all things that I regularly want. They're really making good on their philosophy of optimising for developer happiness :)
The page says:
> This is now no longer an issue, Ruby 2.1 uses a method cache based on the class hierarchy, invalidating the cache for only the class in question and any subclasses.
How does this work with an instance of a class? Does it invalidate only that instance's method cache, or all instances in that class? If I do DCI-style:
Does that blow away the cache for all User instances, or just that one?