-
-
Save dinks/6962dee675f54ebe7390 to your computer and use it in GitHub Desktop.
Mutable Objects as Hash Keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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