Method: Object#clone

Defined in:
object.c

#clone(freeze: true) ⇒ Object

Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. #clone copies the frozen (unless :freeze keyword argument is given with a false value) state of obj. See also the discussion under Object#dup.

class Klass
   attr_accessor :str
end
s1 = Klass.new      #=> #<Klass:0x401b3a38>
s1.str = "Hello"    #=> "Hello"
s2 = s1.clone       #=> #<Klass:0x401b3998 @str="Hello">
s2.str[1,4] = "i"   #=> "i"
s1.inspect          #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
s2.inspect          #=> "#<Klass:0x401b3998 @str=\"Hi\">"

This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy method of the class.

Returns:



404
405
406
407
408
409
410
411
# File 'object.c', line 404

static VALUE
rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
{
    int kwfreeze = freeze_opt(argc, argv);
    if (!special_object_p(obj))
	return mutable_obj_clone(obj, kwfreeze);
    return immutable_obj_clone(obj, kwfreeze);
}