title: Is there a simple way to duplicate a multi-dimensional array in Ruby?
body: <p>I have a 2-dimensional array in Ruby that I want to produce a working duplicate of. Obviously I can't do this;</p>
  
  <pre><code>array=[[3,4],[5,9],[10,2],[11,3]]
  temp_array=array
  </code></pre>
  
  <p>as any modifications I make to temp_array will also be made to array, as I have merely copied the object identifier. I thought I would be able to get around this by simply using;</p>
  
  <pre><code>temp_array=array.dup
  </code></pre>
  
  <p>but this doesn't work as temp_array is simply an array of object identifiers that get duplicated so I still end up modifying the initial array (if I understand what went wrong when I did this). The solution I found was to do the following;</p>
  
  <pre><code>temp_array=[]
  array.each{|sub| temp_array &lt;&lt; sub.dup}
  </code></pre>
  
  <p>This achieves what I want but seems to be an awkward way of solving my problem.</p>
  
  <p>I am concerned about how this would work if I didn't know what my array was going to be containing (e.g. if it was possible that some parts of the array had 3-dimensions). I would potentially have to test the class of each member of the array to see if it had to be iterated over in order to duplicate it. Not an impossible task at all, but it seems messy to me. Is this simply a consequence of Ruby lacking built-in support for multidimensional arrays or is there a simple built-in function to do this that I have missed? </p>