Skip to content

Instantly share code, notes, and snippets.

@dinks
Created May 17, 2015 19:43
Show Gist options
  • Save dinks/6962dee675f54ebe7390 to your computer and use it in GitHub Desktop.
Save dinks/6962dee675f54ebe7390 to your computer and use it in GitHub Desktop.
Mutable Objects as Hash Keys
a = [1, 2] # [1, 2]
a.object_id # 70364895085700
a << 3 # [1, 2, 3]
a.object_id # 70364895085700
h = {} # {}
h[a] = true # true
h # {[1, 2, 3]=>true}
h[a] # true
a << 4 # [1, 2, 3, 4]
h # {[1, 2, 3, 4]=>true}
h[a] # nil
h[a] = false # false
h # {[1, 2, 3, 4]=>true, [1, 2, 3, 4]=>false}
h[[1,2,3,4]] # false
h[[1,2,3]] # nil
l = [1, 2, 3, 4] # [1, 2, 3, 4]
l.instance_eval { def hash; [1,2,3].hash; end }
h[l] # true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment