[ create a new paste ] login | about

Link: http://codepad.org/OxXxqA8z    [ raw code | output | fork ]

Ruby, pasted on Jun 15:
1
2
3
4
5
6
7
pc = Struct.new(:price, :time)
h = Hash.new([pc.new]*5)  #making hash of items which store array of structs

h['hat'][0].price = '88s'
h['hat'][0].time = Time.now

p h['hat']   #now why does it fill ALL the structs with same info?


Output:
1
[#<struct #<Class:0x401bf928> price="88s", time=Fri Jun 15 00:17:05 +0000 2012>, #<struct #<Class:0x401bf928> price="88s", time=Fri Jun 15 00:17:05 +0000 2012>, #<struct #<Class:0x401bf928> price="88s", time=Fri Jun 15 00:17:05 +0000 2012>, #<struct #<Class:0x401bf928> price="88s", time=Fri Jun 15 00:17:05 +0000 2012>, #<struct #<Class:0x401bf928> price="88s", time=Fri Jun 15 00:17:05 +0000 2012>]


Create a new paste based on this one


Comments: