Is this the way that attr_reader is supposed to work with arrays ?
class User
def initialize
@interests = []
end
attr_reader :interests
end
u = User.new
=> #User:0x1d2cb60
u.interests << "music"
=> ["music"]
u.interests[1] = "travelling"
=> ["travelling"]
u.interests = nil
NoMethodError: undefined method `interests=' for u:User
I am just checking if my own explanation is correct, if not please correct me:
Is attr_reader not stopping me from assigning "@interests" values because you are not directly modifying the instance variable itself (which still holds a reference to the array object) but you are just operating on the values that that array object references ?
If that's correct, is there a quick and nice way to avoid attr_reader giving me write access to the array values, but letting me read them ?
Thanks