title: Variable references in ruby
body: <p>So apparently it seems that ruby is a pass-by reference language:</p>
  
  <pre><code>$ irb --simple-prompt 
  &gt;&gt; @foo=1
  =&gt; 1
  &gt;&gt; @bar=2
  =&gt; 2
  &gt;&gt; @foo.object_id
  =&gt; 3
  &gt;&gt; @bar.object_id
  =&gt; 5
  &gt;&gt; [@foo,@bar].each {|e| puts e.object_id }
  3
  5
  =&gt; [1, 2]
  </code></pre>
  
  <p>I.e., both the constructed array and the block seem to deal with references to the original class instance variable objects.</p>
  
  <p>However, these references seem to create copies as soon as I try to write into them:</p>
  
  <pre><code>&gt;&gt; [@foo,@bar].each {|e| puts e+=1 }
  2
  3
  =&gt; [1, 2]
  &gt;&gt; @foo
  =&gt; 1
  &gt;&gt; @bar
  =&gt; 2
  &gt;&gt; [@foo,@bar].map! {|e| e+1 }
  =&gt; [2, 3]
  &gt;&gt; @foo
  =&gt; 1
  &gt;&gt; @bar
  =&gt; 2
  </code></pre>
  
  <p>I had a handful of class instance variable objects that I needed to transform via a function so I thought I'd save keystrokes by making use of the pass-by-reference thing and do something like:</p>
  
  <pre><code>[@var1, @var2, @var3].map! {|v| my_function(v) }
  </code></pre>
  
  <p>but it doesn't seem to work due to this copy-on-write thing that seems to be going on.</p>
  
  <p>Is there a way to turn it off?
  How would you accomplish my while keeping the code both DRY and efficient at the same time?</p>