Instance variables are also copied
If there was such a class
class Test
def set_name(name)
@name = name
end
def get_name
@name
end
end
First, set the instance variable normally
origin = Test.new
=> #<Test:0x00007fcebd06e940>
origin.set_name("origin")
=> "origin"
origin.get_name
=> "origin"
When cloned, the entire instance variable is copied
copy = origin.clone
=> #<Test:0x00007fcebd05d7a8 @name="origin">
copy.get_name
=> "origin"
Rewriting the instance variable of copy does not affect the original
copy.set_name('copy')
=> "copy"
origin.get_name
=> "origin"
dup Same for dup
copy2 = origin.dup
=> #<Test:0x00007fcebd054040 @name="origin">
irb(main):030:0> copy2.get_name
=> "origin"
Recommended Posts