Reputation: 168199
Are there any situations where I should chose between instance_varaible_set
/instance_variable_get
or attr_writer
/attr_reader
to access an instance variable of the object from outside of it? How are they different in terms of speed, or etc?
I had the impression that if you want to let accessing the instance variable an opened method to the end user, attr_writer
/attr_reader
should be used to make it easy, but if you want to use it privately, you would rather use instance_varaible_set
/instance_variable_get
. Is this understanding correct?
Upvotes: 0
Views: 640
Reputation: 230471
This is correct. You define attr_accessor
/attr_reader
/attr_writer
inside class definition to make them access (implicitly or explicitly created) instance variable.
If class wasn't written by you and no such accessors exist, you still can read/write private instance variables using instance_variable_get
/instance_variable_set
.
Upvotes: 1