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 7891229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:33:55+00:00 2026-06-03T06:33:55+00:00

I have a KillerSudoku class in my application. Every instance has many cells in

  • 0

I have a KillerSudoku class in my application.

Every instance has many cells in @cells, many zones in @zones and so on.

Is there a way to easily copy an object (the sudoku) copying all it’s “sub-objects”, that’s to say, I want my copy to have cells and zones I can modify without modifying the previous sudoku?

  • 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-06-03T06:33:57+00:00Added an answer on June 3, 2026 at 6:33 am

    Assuming that all parts are serializable by Marshal and you want a completely deep clone:

    class Object
      # This doesn't have to be on Object; it could be on KillerSudoku instead
      def deep_clone
        Marshal.load(Marshal.dump(self))
      end
    end
    

    Seen in action:

    class KillerSudoku
      attr_accessor :cells
    end
    
    ks1 = KillerSudoku.new
    ks1.cells = ["one",2,3]
    
    ks2 = ks1.deep_clone
    ks2.cells.pop
    ks2.cells.first.reverse!
    
    p ks1.cells, ks2.cells
    #=> ["one", 2, 3]
    #=> ["eno", 2]           # New array with all instance values being uniq, too
    

    From the docs, the above won’t work if you have any of the following in your structures:

    • anonymous Class or Module.

    • objects which are related to the system (e.g. Dir, File::Stat, IO, File, Socket, etc.)

    • an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread, ThreadGroup, Continuation

    • objects which define singleton methods


    As seen above, this is a truly deep clone, such that even strings become new instances. If you want the arrays of cells and zones to be cloned, but have all values still referencing the same objects, then you want to customize what dup and clone do by using initialize_copy:

    class KillerSudoku
      attr_accessor :cells
      def initialize_copy(old)
        self.cells = old.cells.dup
      end
    end
    

    Seen in action:

    ks1 = KillerSudoku.new
    ks1.cells = ["one",2,3]
    
    ks2 = ks1.clone
    ks2.cells.pop
    ks2.cells.first.reverse!
    
    p ks1.cells, ks2.cells
    #=> ["eno", 2, 3]
    #=> ["eno", 2]           # New array that references all the same objects
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have done some working samples using Backbone Router, but is there a way to
Have a matrix report now that has Position, Hours and Wages for a location
Have you ever obfuscated your code before? Are there ever legitimate reasons to do
Have a really weird one here. The app has two targets, just to be
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a n-tire web application and search often times out after 30 secs. How
have different files with same name, in different directories. In these files there are
Have started playing with Xcode 4.2, and created a single page application using storyboard
have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,

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.