Forum: Ruby on Rails recursive deep copy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4b87f676cb6c4d648d71000681823693?d=identicon&s=25 Jean-sébastien Jney (jney)
on 2007-05-10 12:37
(Received via mailing list)
hello,

  It's a little bit hard question:
  i've a problem when working on arrays. i'm doing a deep copy of an
array (a find request result) by doing it :
@array = new.Array(@original_array)
it just copies the first level, under levels arrays and hashes are not
include in deep copy!

some can help?
E0cc1aaab18a0d2f82bcc52bdcb26cc5?d=identicon&s=25 Titou Titou (22marine)
on 2007-05-10 12:51
What about this :

new_array = old_array


;)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-10 13:00
(Received via mailing list)
Hi --

On Thu, 10 May 2007, Jean-Sébastien wrote:

> some can help?
The most common idiom for this is:

   copy = Marshal.load(Marshal.dump(@array))


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-10 13:01
(Received via mailing list)
Hi --

On Thu, 10 May 2007, Titou Titou wrote:

>
> What about this :
>
> new_array = old_array

He wants a deep copy, and that's neither deep nor a copy.  It's just a
new reference to the same array:

   array = [1,2,3]
   new_array = array
   new_array.push(4)
   p array              # [1, 2, 3, 4]


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
4b87f676cb6c4d648d71000681823693?d=identicon&s=25 Jean-sébastien Jney (jney)
on 2007-05-10 13:24
(Received via mailing list)
thank you guys,  "@array =
Marshal::load(Marshal.dump(@original_array))" works perfectly
This topic is locked and can not be replied to.