Created
January 13, 2009 05:25
-
-
Save eltiare/46339 to your computer and use it in GitHub Desktop.
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
class Hash | |
alias :old_default :default | |
def default(*args) | |
ret = old_default(*args) | |
ignore_these = [NilClass, TrueClass, FalseClass, Float, Symbol, Rational, Integer] | |
case ret | |
when *ignore_these: ret | |
else ret.dup | |
end | |
end | |
end | |
# This is to prevent weird behavior such as this: | |
# h = Hash.new([]) | |
# h['a'].push(1) -> [1] | |
# h['a'].push(2) -> [1,2] | |
# h['b'].push(3) -> [1,2,3] | |
# h['a'] -> [1,2,3] | |
# h['b'] -> [1,2,3] | |
# | |
# Since no assignments are being made, the intuitive behavior would be thus: | |
# h = Hash.new([]) | |
# h['a'].push(1) -> [1] | |
# h['a'].push(2) -> [2] | |
# h['b'].push(3) -> [3] | |
# h['a'] -> [] | |
# h['b'] -> [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment