# Copying a matrix

**URL:** <https://www.ruby-forum.com/t/copying-a-matrix/195664>\
**Category:** Ruby\
**Created:** [September 27, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664 "2010-09-27T21:47:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/1 "2010-09-27T21:47:37Z")

</div>

How can I create an array copy that  
does not reference the same object…

I have tried this without luck.

m2 = m.map { |e| e }  
m2[2][2]=2

p m  
p m2

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/2 "2010-09-27T22:16:32Z")

</div>

On Sep 27, 3:47 pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted viahttp://www.ruby-forum.com/.

m2 = m.dup  
0.upto(m2.size - 1) { |i| m2[i] = m[i].dup; 0.upto(m2[i].size - 1) { |  
j| m2[i][j] = m[i][j] } }  
m2[2][2] = 100  
p m  
p m2

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]  
[[1, 2, 3], [4, 5, 6], [7, 8, 100]]

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/3 "2010-09-27T23:17:25Z")

</div>

Perhaps

m2 = Marshal.load(Marshal.dump(m))

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 28, 2010, 12:53am UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/4 "2010-09-28T00:53:56Z")

</div>

Adam P. wrote:

> Perhaps
> 
> m2 = Marshal.load(Marshal.dump(m))

Thank you both. It’s nice to have a one line object copy… I never  
imagined it would be so … esoteric O\_o…

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 28, 2010, 8:05am UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/5 "2010-09-28T08:05:52Z")

</div>

John S. wrote:

> How can I create an array copy that  
> does not reference the same object…
> 
> I have tried this without luck.
> 
> m2 = m.map { |e| e }

Try:  
m2 = m.map { |e| e.dup }

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 28, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/copying-a-matrix/195664/6 "2010-09-28T17:16:20Z")

</div>

On Tue, Sep 28, 2010 at 7:05 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> John S. wrote:
> 
> > How can I create an array copy that  
> > does not reference the same object…  
> > I have tried this without luck.  
> > m2 = m.map { |e| e }
> 
> Try:  
> m2 = m.map { |e| e.dup }

or maybe use:  
m = [“a”, nil, true, false, 1, 2.0]  
m2 = m.map { |e| e.dup rescue e }

I’ve wondered in the past why there isn’t a dup or clone alternative  
which  
for nil, true, false, Integer, Floats (anything else?) just returns the  
object itself.

Is there any easy way of telling whether an object will respond to dup  
or  
clone?  
For all those objects listed above which won’t dup or clone  
obj.respond\_to?(:dup) #=\> true on my computer

If the OP needs more than 2 levels of dups, this may be a case for more  
general recursion, discussed here  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/360651](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/360651)  
prompted by this thread:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/360275](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/360275)

\*\*\* ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]

m = [“a”, nil, true, false, 1, 2.0]  
m2 = m.map { |e|  
begin  
e.dup  
rescue  
puts “#=\> dup of #{e.inspect} #=\> #{$!.inspect}”  
puts “#=\> #{e.respond\_to?(:dup)} #{e.respond\_to?(:clone)}”  
e  
end  
}

#=\> dup of nil #=\> #\<TypeError: can’t dup NilClass\>  
#=\> true true  
#=\> dup of true #=\> #\<TypeError: can’t dup TrueClass\>  
#=\> true true  
#=\> dup of false #=\> #\<TypeError: can’t dup FalseClass\>  
#=\> true true  
#=\> dup of 1 #=\> #\<TypeError: can’t dup Fixnum\>  
#=\> true true  
#=\> dup of 2.0 #=\> #\<TypeError: allocator undefined for Float\>  
#=\> true true
