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

  • Home
  • SEARCH
  • 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 8384461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:24:42+00:00 2026-06-09T17:24:42+00:00

I have a Pointer class with a single attribute :contents , that points to

  • 0

I have a Pointer class with a single attribute :contents, that points to an object of class MyObject.

class MyObject
  def hello; "hello" end
end

class Pointer
  attr_reader :contents
  def initialize( cont ); @contents = cont end
  # perhaps define some more state
end

I want my Pointer to be able to make copies of itself. I know that #dup method is defined by default, while #clone method is expected to be overriden to be able to make deep copies. But here, the copies don’t have to be too deep. So, the first dilemma that I have is, should I override #dup method, because I don’t really want to copy the additional state of my Pointer, just make a new one pointing to the same MyObject instance? Or should I refrain from overridine #dup, because I am not “supposed to” and override #clone with a method making shallow copies?

I would welcome comments on the above, but let’s say that I will choose to override #dup. I could do just this:

class Pointer
  def dup; self.class.new( contents ) end
end

But online, I read something like “the dup method will call the initialize copy method”. Also, this guy writes about #initialize_clone, #initialize_dup and #initialize_copy in Ruby. That leaves me wondering, is the best practice perhaps like this?

class Pointer
  def initialize_copy
    # do I don't know what
  end
end

Or like this?

class Pointer
  def initialize_dup
    # do I don't know what
  end
end

Or should I just forget about online rants written to confuse beginners and go for overriding #dup without concerns?

Also, I do understand that I can just call #dup without defining any custom #dup, but what if I want to define #dup with different behavior?

Also, the same question apply to #clone – should I try to define #initialize_clone or just #clone?

  • 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-09T17:24:43+00:00Added an answer on June 9, 2026 at 5:24 pm

    From my experience, overloading #initialize_copy works just fine (never heard about initialize_dup and initialize_clone).

    The original initialize_copy (which initializes every instance variable with the values from the original object) is available through super, so I usually do:

    class MyClass
      def initialize_copy(orig)
        super
        # Do custom initialization for self
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pointer to a QScriptEngine that I'm passing through the overloaded class
I have a Snake class, which has a pointer to a Controller class object.
I have a C++ template class that contains a method pointer and a class
I have a URes class which contains a single pointer to <T> , with
I have an auto pointer class and in the constructor I am passing in
If I have set<Pointer*> , Pointer* is pointer to custom class, set is from
I have a class with a static factory constructor which returns a pointer to
I have a class with about 50 members. 1 of them is a pointer.
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have a class AudioDeviceManager that passes a std::vector by reference to an AudioAnalyzer

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.