title: Ruby : how to prevent modification of an array instance variable through an attribute reader
body: <p>sorry for this noob question... let's say we have : </p>
  
  <pre><code>class TestMe
   attr_reader :array
  
   def initialize
     @array = (1..10).to_a
   end
  </code></pre>
  
  <p>end</p>
  
  <p>it is then possible to do :</p>
  
  <pre><code>&gt;&gt; a = TestMe.new
  =&gt; #&lt;TestMe:0x00000005567228 @x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&gt;
  &gt;&gt; a.array.map! &amp;:to_s
  =&gt; ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
  &gt;&gt; a.array
  =&gt; ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
  </code></pre>
  
  <ul>
  <li>this clearly goes against encapsulation, doesn'it ?  </li>
  <li>is there any way to quickly protect the array variable from being changed ? </li>
  <li>... or do i need to implement a deep-copy reader every time my instance variable has "destructive" methods ? </li>
  </ul>
  
  <p><strong>EDIT</strong> i read somewhere it is "bad OO" to expose an array instance variable. If it's true, why ?</p>