Skip to content

Instantly share code, notes, and snippets.

@eltiare
Created January 13, 2009 05:25
Show Gist options
  • Save eltiare/46339 to your computer and use it in GitHub Desktop.
Save eltiare/46339 to your computer and use it in GitHub Desktop.
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