How to create a deep copy of an object in Ruby?

ruby deep copy object
ruby deep clone hash
ruby deep copy array
ruby copy object not reference
create a copy of a hash ruby
rails copy attributes from one object to another
ruby copy array
ruby clone object

I did some searching found some different methods and posts about creating a deep copy operator.

Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or hashes.

Working in Ruby 1.9.2.

Deep copy isn't built into vanilla Ruby, but you can hack it by marshalling and unmarshalling the object:

Marshal.load(Marshal.dump(@object))

This isn't perfect though, and won't work for all objects. A more robust method:

class Object
  def deep_clone
    return @deep_cloning_obj if @deep_cloning
    @deep_cloning_obj = clone
    @deep_cloning_obj.instance_variables.each do |var|
      val = @deep_cloning_obj.instance_variable_get(var)
      begin
        @deep_cloning = true
        val = val.deep_clone
      rescue TypeError
        next
      ensure
        @deep_cloning = false
      end
      @deep_cloning_obj.instance_variable_set(var, val)
    end
    deep_cloning_obj = @deep_cloning_obj
    @deep_cloning_obj = nil
    deep_cloning_obj
  end
end

Source:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/43424

The Complete Guide to Create a Copy of an Object in Ruby: Part I , Deep copy. A deep copy works has a shallow copy. In addition, all the objects pointed by reference in the copied object will also be copied. There is a native implementation to perform deep clones of ruby objects: ruby_deep_clone. Install it with gem: gem install ruby_deep_clone Example usage: require "deep_clone" object = SomeComplexClass.new() cloned_object = DeepClone.clone(object) It's approximately 6 to 7 times faster than the Marshal approach and event works with frozen objects.

I've created a native implementation to perform deep clones of ruby objects.

It's approximately 6 to 7 times faster than the Marshal approach.

https://github.com/balmma/ruby-deepclone

Note that this project is not maintained anymore (last commit in 2017, there are reported issues)

How to create a deep copy of an object in Ruby?, Deep copy isn't built into vanilla Ruby, but you can hack it by marshalling and unmarshalling the object: Marshal.load(Marshal.dump(@object)). The Complete Guide to Create a Copy of an Object in Ruby: Part I. A deep copy occurs when an object is copied along with the objects to which it refers. dup and clone methods.

Rails has a recursive method named deep_dup that will return a deep copy of an object and, on the contrary of dup and clone, works even on composite objects (array/hash of arrays/hashes). It's as easy as:

def deep_dup
  map { |it| it.deep_dup }
end

How to Make Deep Copies in Ruby, Ruby does provide two methods for making copies of objects, including one that can be made to do deep copies. The Object#dup method will make a shallow copy of an object. To achieve this, the dup method will call the initialize_copy method of that class. What this does exactly is dependent on the class. Deep copy isn't built into vanilla Ruby, but you can hack it by marshalling and unmarshalling the object: Marshal.load(Marshal.dump(@object)) This isn't perfect though, and won't work for all objects.

There is a native implementation to perform deep clones of ruby objects: ruby_deep_clone

Install it with gem:

gem install ruby_deep_clone

Example usage:

require "deep_clone"
object = SomeComplexClass.new()
cloned_object = DeepClone.clone(object)

It's approximately 6 to 7 times faster than the Marshal approach and event works with frozen objects.

Note that this project is not maintained anymore (last commit in 2017, there are reported issues)

Dup vs Clone in Ruby: Understanding The Differences, Deep vs Shallow Copying. There is more to copying an object than meets the eye . When you make a copy, with either dup or clone� Shallow Copy in Ruby. I did a quick search for how to copy an array in Ruby and came up easily with the dup method. And it is important to note that making copies of simple objects is easy and is the intended purpose of the dup method. Take the following as an example: array_one = [1, 2, 3] array_two = array_one array_two << 4 array_one

You can use duplicate gem for this.

It's a small pure ruby gem that able to recursively duplicate object It will duplicate it's object references too to the new duplication.

require 'duplicate'
duplicate('target object')

https://rubygems.org/gems/duplicate

https://github.com/adamluzsi/duplicate.rb

Shallow Copy vs. Deep Copy in Ruby, And it is important to note that making copies of simple objects is easy and is the intended purpose of the dup method. Take the following as an� How to create a deep copy of an object in Ruby? (6) I did some searching found some different methods and posts about creating a deep copy operator. Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or hashes. Working in Ruby 1.9.2.

Ruby: the differences between dup & clone (Example), A protip by mattetti about ruby, dup, clone, and copy objects. They both create a shallow copy of an object (meaning that they don't copy the There are a few ways to do a deep copy of an object, but that's a different matter. Many objects in Ruby are mutable, you can change them. If you want to change an object but keep a copy of the original then you can clone it. For example. You may want an array with all the elements but the first one. One way to do this: a = [1,2,3,4,5] a [1..-1] # [2,3,4,5] a = [1,2,3,4,5] a[1..-1] # [2,3,4,5]

deep_dup (Hash), Returns a deep copy of hash. Rails 4 version does not have this issue because it is completely different implementation. This simplistic implementation (unlike Marshal.load(Marshal.dump(object)) doesn't handle cycles in objects. Below are the ways to implement a deep copy operation. Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. Look at the below program to understand the deep copy practically.

Copy Object in Ruby, To our relief, Ruby makes object copying easy. That's it! By using the clone() method provided, we make a deep copy, not just a shallow copy. By using the clone() method provided, we make a deep copy, not just a shallow copy. It works because we tell Ruby to clone the hot_goods attribute. Solution that uses the marshalling mechanism would look like this:

Comments
  • related: stackoverflow.com/questions/14104542/ruby-object-deep-copying
  • OK, thanks. I found a post about marshalling but didn't know if it was a reasonable solution. Marshal.load(Marshal.dump(@object)) worked great.
  • Yeah, it's only slow relatively speaking - you'd only notice if you were cloning lots of objects at a time
  • Obviously, it also assumes that the object is marshallable in the first place, which not all objects are.
  • How comes and there isn't any ruby built in method for that?
  • This project is not maintained any more and not production ready, according to the author.
  • Rails' Object#deep_dup is actually a shallow dup call. Besdie Hash and Array, you need to implement your own deep_dup for it to work.
  • Unfortunately, this gem does not handle custom Collection classes that inherit from Array and contain their own complex nested classes. Ruby_deep_clone turns those objects into Arrays and they lose their custom attributes.
  • This project is not maintained any more and not production ready, according to the author.
  • @bbozo It claims to support up to Ruby 2.4. Is there a source saying it is no longer maintained?
  • This answer is a duplicate of stackoverflow.com/questions/8206523/… that was posted earlier