freeze and hash

S

Schüle Daniel

Hello

irb(main):069:0* a = Hash.new([])
=> {}
irb(main):070:0> a[1] << "X"
=> ["X"]
irb(main):071:0> a.freeze
=> {}
irb(main):072:0> a.frozen?
=> true
irb(main):073:0> b = a.dup
=> {}
irb(main):074:0> b.frozen?
=> false
irb(main):075:0> b.default
=> ["X"]
irb(main):076:0> b[11] << "Y"
=> ["X", "Y"]
irb(main):077:0> b.default
=> ["X", "Y"]
irb(main):078:0> a.default
=> ["X", "Y"]
irb(main):079:0> a.frozen?
=> true
irb(main):080:0> b.frozen?

as one can see a.default is a flat copy
so a is modified though it's frozen

what is the usual idom to get deep copy?

Regards, Daniel
 
C

Charles Mills

Schüle Daniel said:
Hello

irb(main):069:0* a = Hash.new([])
=> {}
irb(main):070:0> a[1] << "X"
=> ["X"]
irb(main):071:0> a.freeze
=> {}
irb(main):072:0> a.frozen?
=> true
irb(main):073:0> b = a.dup
=> {}
irb(main):074:0> b.frozen?
=> false
irb(main):075:0> b.default
=> ["X"]
irb(main):076:0> b[11] << "Y"
=> ["X", "Y"]
irb(main):077:0> b.default
=> ["X", "Y"]
irb(main):078:0> a.default
=> ["X", "Y"]
irb(main):079:0> a.frozen?
=> true
irb(main):080:0> b.frozen?

as one can see a.default is a flat copy
so a is modified though it's frozen

what is the usual idom to get deep copy?

Regards, Daniel

Try

a = Hash.new { [] }

When a default value is needed the proc is run and its return value is
used.
See ri Hash.new for more info.

-Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top