Ruby attr_reader allows one to modify string variable if using <<
Ran into some weird behaviour and wondering if anyone else can confirm what I am seeing. Suppose you create a class with a member variable, and allow it to be read with attr_reader. class TestClass attr_reader :val def initialize(value) @val = value end end Now when I do the following, it seems to modify the value of @val, even though I have only granted it read privileges. test = TestClass.new('hello') puts test.val test.val << ' world' puts test.val This returns hello hello world This is just the result from some testing I did in irb so not sure if this is always the case You are not really