title: Object assignment and pointers
body: <p>I am a little confused about object assignment and pointers in Ruby, and coded up this snippet to test my assumptions.</p>
  
  <pre><code>class Foo
      attr_accessor :one, :two
      def initialize(one, two)
          @one = one
          @two = two
      end
  
  end
  
  bar = Foo.new(1, 2)
  beans = bar
  
  puts bar
  puts beans
  
  beans.one = 2
  puts bar
  puts beans
  puts beans.one
  puts bar.one
  </code></pre>
  
  <p>I had assumed that when I assigned bar to beans, it would create a copy of the object, and modifying one would not affect the other. Alas, the output shows otherwise.</p>
  
  <pre><code>^_^[jergason:~]$ ruby test.rb 
  #&lt;Foo:0x100155c60&gt;
  #&lt;Foo:0x100155c60&gt;
  #&lt;Foo:0x100155c60&gt;
  #&lt;Foo:0x100155c60&gt;
  2
  2
  </code></pre>
  
  <p>I believe that the numbers have something to do with the address of the object, and they are the same for both beans and bar, and when I modify beans, bar gets changed as well, which is not what I had expected. It appears that I am only creating a pointer to the object, not a copy of it. What do I need to do to copy the object on assignment, instead of creating a pointer?</p>
  
  <p>Tests with the Array class shows some strange behavior as well. </p>
  
  <pre><code>foo = [0, 1, 2, 3, 4, 5]
  baz = foo
  puts "foo is #{foo}"
  puts "baz is #{baz}"
  foo.pop
  puts "foo is #{foo}"
  puts "baz is #{baz}"
  
  foo += ["a hill of beans is a wonderful thing"]
  puts "foo is #{foo}"
  puts "baz is #{baz}"
  </code></pre>
  
  <p>This produces the following wonky output:</p>
  
  <pre><code>foo is 012345
  baz is 012345
  foo is 01234
  baz is 01234
  foo is 01234a hill of beans is a wonderful thing
  baz is 01234
  </code></pre>
  
  <p>This blows my mind. Calling pop on foo affects baz as well, so it isn't a copy, but concatenating something onto foo only affects foo, and not baz. So when am I dealing with the original object, and when am I dealing with a copy? In my own classes, how can I make sure that assignment copies, and doesn't make pointers? Help this confused guy out.</p>