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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:35:05+00:00 2026-06-16T12:35:05+00:00

I’m looking at some techniques to deep-copy objects in Ruby (MRI 1.9.3). I came

  • 0

I’m looking at some techniques to deep-copy objects in Ruby (MRI 1.9.3).
I came across the following example, but I’m not sure about the #dup method implementation.
I tested it and it does work, but I don’t understand the logical steps of the method, thus I’m not confortable using it in my own code.

Is the statement @name = @name.dup referring to the iVar inside the copy? How? I can’t see it.

Could anyone explain it, please?
Also, are there better ways?

class MyClass
  attr_accessor :name

  def initialize(arg_str)   # called on MyClass.new("string")
    @name = arg_str         # initialize an instance variable
  end

  def dup
    the_copy = super        # shallow copy calling Object.dup
    @name = @name.dup       # new copy of istance variable
    return the_copy         # return the copied object
  end
end
  • 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-16T12:35:06+00:00Added an answer on June 16, 2026 at 12:35 pm

    This is a really thin, very specific implementation of a “deep copy”. What it’s demonstrating is creating an independent @name instance variable in the clone so that modifying the name of one with an in-place operation won’t have the side-effect of changing the clone.

    Normally deep-copy operations are important for things like nested arrays or hashes, but they’re also applicable to any object with attributes that refer to things of that sort.

    In your case, to make an object with a more robust dup method, you should call dup on each of the attributes in question, but I think this example is a bit broken. What it does is replace the @name in the original with a copy, which may break any references you have.

    A better version is:

    def dup
      copy = super
      copy.make_independent!
      copy
    end
    
    def make_independent!
      instance_variables.each do |var|
        value = instance_variable_get(var)
    
        if (value.respond_to?(:dup))
          instance_variable_set(var, value.dup)
        end
      end
    end
    

    This should have the effect of duplicating any instance variables which support the dup method. This skips things like numbers, booleans, and nil which can’t be duplicated.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

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.