Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:37:03+00:00 2026-05-26T23:37:03+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:37:04+00:00Added an answer on May 26, 2026 at 11:37 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My apologies if this has been answered before or is obvious...did some searching here
Did some searches here & on the 'net and haven't found a good answer
I did some HTTP monitoring with WireShark. Are there more tools like this that
I am searching for some cocoa code on AES encryption and I did some
I did do some googling and searching on this site but did not find
Disclaimer: I did try searching first, and even found this comment , but it
I did some searching to try and generate jpg files from an html page
Did some googling and couldn't find a clear answer on this. My assumption is
I did some tests a while ago and never figured out how to make
I did some googling to try to answer this question but even after that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.