Hacker News new | past | comments | ask | show | jobs | submit login

lost your object? rehash to the rescue!

  jruby-1.6.8 :068 > h = {}
   => {} 
  jruby-1.6.8 :069 > l = [1, 2]
   => [1, 2] 
  jruby-1.6.8 :071 > h[l] = 1
   => 1 
  jruby-1.6.8 :072 > h[l]
   => 1 
  jruby-1.6.8 :073 > h[l] = 3
   => 3 
  jruby-1.6.8 :074 > h[l]
   => 3 
  jruby-1.6.8 :075 > l[1] = 3
   => 3 
  jruby-1.6.8 :076 > h[l]
   => nil 
  jruby-1.6.8 :077 > h.rehash
   => {[1, 3]=>3} 
  jruby-1.6.8 :078 > h[l]
   => 3
see http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-rehash :

  Rebuilds the hash based on the current hash values for each key. If values
  of key objects have changed since they were inserted, this method will 
  reindex hsh.
On one side I must admit that the behavior is a bit surprising I haven't ever encountered in the wild. It's not that easy to get hold of a hash key at a random point in the code where you don't know that it is a hash key.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: