I am working on the act_as_taggable_on plugin, but there is something that I cannot understand (even if it's just a very simple line of code).
puts "before:" + cache.inspect
# cache.delete_if {| key, value | key.id == owner.id && key.class == owner.class} # original code line
cache.delete_if {| key, value | true} # my test code
puts "after:" + cache.inspect
# output
before: {#<TaggableUser id: 1, name: nil>=>["dog"]}
after: {# TaggableUser id: 1, name: nil>=>["dog"]}
My problem is that cache.delete_if doesn't delete anything, even if it always evaluates to true. I just don’t understand why ... and really tried a lot. This is just a hash cache issue. But I really could not find anything special in this particular hash.
The cache is created in this method:
def cached_owned_tag_list_on (context)
variable_name = "@owned _ # {context} _list"
cache = instance_variable_get (variable_name) || instance_variable_set (variable_name, {})
end
The full code can be viewed here (see line 60): http://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb#L60
One more step
When I make rehashup delete_if, it's working. What can “spoil” a hash in such a way that it is necessary to rephrase it before any deletions are performed?
source
share