-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_default_value.rb
executable file
·46 lines (41 loc) · 1.29 KB
/
hash_default_value.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
#-*- coding: utf-8 -*-
h1, h2 = {}, Hash.new
puts "-" * 25
puts " {}[:a] => #{h1[:a].inspect}"
puts "Hash.new[:a] => #{h2[:a].inspect}"
puts
h3 = Hash.new 0
puts "-" * 25
puts " h = Hash.new 0"
puts " ... h[:a] => #{h3[:a].inspect}"
h3[:a] += 1
puts " h[:a] += 1"
puts " ... h[:a] => #{h3[:a].inspect}"
puts " ... h[:b] => #{h3[:b].inspect}"
puts
# No exactly what we wants
h4 = Hash.new []
puts "-" * 25
puts " h = Hash.new []"
puts " ... h[:a] => #{h4[:a].inspect}"
puts " ... h[:b] => #{h4[:b].inspect}"
h4[:a] <<= 'aa'
puts " h[:a] <<= 'aa'"
puts " ... h[:a] => #{h4[:a].inspect} (h[:a].object_id => #{h4[:a].object_id})"
puts " ... h[:b] => #{h4[:b].inspect} (h[:b].object_id => #{h4[:b].object_id})"
puts
h5 = Hash.new { [] }
puts "-" * 25
puts " h = Hash.new { [] }"
puts " ... h[:a] => #{h5[:a].inspect}"
puts " ... h[:b] => #{h5[:b].inspect}"
h5[:a] << 'aa'
puts " h[:a] << 'aa'"
puts " ... h[:a] => #{h5[:a].inspect} (h[:a].object_id => #{h5[:a].object_id})"
puts " ... h[:b] => #{h5[:b].inspect} (h[:b].object_id => #{h5[:b].object_id})"
h5[:a] <<= 'aa'
puts " h[:a] <<= 'aa'"
puts " ... h[:a] => #{h5[:a].inspect} (h[:a].object_id => #{h5[:a].object_id})"
puts " ... h[:b] => #{h5[:b].inspect} (h[:b].object_id => #{h5[:b].object_id})"
puts