This code in your example:
@store[:foo] ||= {bar: 'BAR'}
does not do what you may be thinking it does. HWIA overrides the `[]=` operator:
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/hash_with_indifferent_access.rb#L96
to convert incoming plain Hash objects into HWIA. So the object that eventually is stored in `@store[:foo]` is NOT the one that was passed to the assignment operator.
You can check this explicitly:
require 'active_support/all'
@store = ActiveSupport::HashWithIndifferentAccess.new
def my_hash
@store[:foo] ||= {bar: 'BAR'}
end
first_time = my_hash # => {:bar =>”BAR”}
second_time = my_hash # => {“bar”=>”BAR”}
Note that `first_time` and `second_time` don’t have matching `inspect` results.
Assignment operators *always* return the value passed on the right-hand side, regardless of what the underlying `[]=` method returns. This means that in cases where the object doesn’t require conversion (coalwater’s modification from your Gist) the object returned from `||=` DOES match, and Waldo is found.
Not sure if there’s a fix for this - the converting-on-[]= behavior is something many applications are likely to depend on, and the behavior of assignment operators is a core Ruby issue.
—Matt Jones