eltiare (owner)

Revisions

gist: 46339 Download_button fork
public
Public Clone URL: git://gist.github.com/46339.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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'] -> []