What happens to instance variables when copying an instance with ruby

Conclusion

Instance variables are also copied

Verification

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

What happens to instance variables when copying an instance with ruby
[Ruby] What is an instance?
What to do when you launch an application with rails
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
[Ruby] Does self refer to a class or an instance?
Initialization with an empty string to an instance of Java String type
How to specify db when creating an app with rails
How to handle an instance
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
[Ruby] This is the solution. When should I use instance variables?
I got an IllegalAccessError when trying to use PowerMock with JUnit
What I was addicted to when implementing google authentication with rails
Automatic file upload with old Ruby gem What to do with Watir
Studying with CodeWar (ruby) ④ case ~ when
What to do when javax.batch.operations.JobStartException occurs
What to do if zip dies if there is a pom when making an executable jar with gradle
[IOS] What to do when the image is filled with one color
What to do when CentOS cannot be started with VirtualBox on Catalina
What to do and how to install when an error occurs in DXRuby 1.4.7
What to do if an error occurs when nokogiri enters when bundle install
Notes on what to do when EC2 is set up with t2.micro
What to do if you get an error when you hit Heroku logs
How to crop an image with libGDX
Send an email from gmail with Ruby
Introduction to Ruby basic grammar with yakiniku
What to do when a javax.el.PropertyNotWritableException occurs
Copying an object with ARKit + CoreML + LiDAR
What to do when undefined method ʻuser_signed_in?'
Convert JSON to TSV and TSV to JSON with Ruby
I get an error when I try to use "^" or "$" in ruby ​​regular expression
How to check if an instance variable is defined in a Ruby class
[Error resolution] Occurs when trying to build an environment for spring with docker
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
What to do when is invalid because it does not start with a'-'
What to do if ffi installation fails when launching an application in Rails
WELD-001408: What to do when Unsatisfied dependencies for type Bean with qualifiers @Default
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field