From: Dominik Bathon Date: 2005-05-30T22:33:41+09:00 Subject: Re: Tiling Turmoil (#33) On Mon, 30 May 2005 13:30:21 +0200, atgeirr wrote: > As far as I can understand, dup would actually work here, since the > Array contains only numbers. Is that correct? Anyway, I did not know > about dup, so thanks! I do not really understand the difference between > dup and clone, though. #clone works like #dup, but it keeps more properties of the original object: #clone keeps a frozen object frozen, while #dup doesn't: irb(main):007:0> a="hallo" => "hallo" irb(main):008:0> a.freeze => "hallo" irb(main):009:0> a.frozen? => true irb(main):010:0> a.dup.frozen? => false irb(main):011:0> a.clone.frozen? => true and a cloned singleton class can not be instantieted, while a dupped singleton class can: irb(main):012:0> (class << a; self; end).new TypeError: can't create instance of virtual class from (irb):12:in `new' from (irb):12 from :0 irb(main):013:0> (class << a; self; end).clone.new TypeError: can't create instance of virtual class from (irb):13:in `new' from (irb):13 from :0 irb(main):014:0> (class << a; self; end).dup.new => "" For the full details, here is the source of both functions from ruby-1.8.2/object.c: VALUE rb_obj_clone(obj) VALUE obj; { VALUE clone; if (rb_special_const_p(obj)) { rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj)); } clone = rb_obj_alloc(rb_obj_class(obj)); RBASIC(clone)->klass = rb_singleton_class_clone(obj); RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT)) & ~(FL_FREEZE|FL_FINALIZE); init_copy(clone, obj); RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE; return clone; } VALUE rb_obj_dup(obj) VALUE obj; { VALUE dup; if (rb_special_const_p(obj)) { rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj)); } dup = rb_obj_alloc(rb_obj_class(obj)); init_copy(dup, obj); return dup; } Dominik