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.
Jean-sébastien J. (Guest)
on 2007-05-10 14: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?
Titou T. (Guest)
on 2007-05-10 14:51
What about this :

new_array = old_array


;)
unknown (Guest)
on 2007-05-10 15: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)
unknown (Guest)
on 2007-05-10 15:01
(Received via mailing list)
Hi --

On Thu, 10 May 2007, Titou T. 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)
Jean-sébastien J. (Guest)
on 2007-05-10 15: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.