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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:30:37+00:00 2026-05-27T10:30:37+00:00

In many ruby projects and even ruby itself I’ve encountered the using of dup

  • 0

In many ruby projects and even ruby itself I’ve encountered the using of dup method. For example, in some project I’ve met this construction:

class Array
  def flush
    self.dup.tap { self.clear }
  end
end

The question is why we should use dup method i.e. create copy of object instead of just use the same object? Thanks

  • 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-27T10:30:37+00:00Added an answer on May 27, 2026 at 10:30 am

    Use dup or clone if part of the codebase is written by an idiot that doesn’t know you shouldn’t modify objects if you can help it (instead, you should create a new object):

    class Greeter
    
      def initialize
        @greeting_cache = {}
      end
    
      def expensive_greeting_calculation(formality)
        case formality
          when :casual then "Hi"
          when :formal then "Hello"
        end
      end
    
      def greeting(formality)
        unless @greeting_cache.has_key?(formality)
          @greeting_cache[formality] = expensive_greeting_calculation(formality)
        end
        @greeting_cache[formality]
      end
    
    end
    
    def memoization_mutator
      greeter = Greeter.new
      first_person = "Bob"
      # Mildly contrived in this case,
      # but you could encounter this in more complex scenarios
      puts(greeter.greeting(:casual) << " " << first_person) # => Hi Bob
      second_person = "Sue"
      puts(greeter.greeting(:casual) << " " << second_person) # => Hi Bob Sue
    end
    
    memoization_mutator
    

    In my case, the idiot is past-me.

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

Sidebar

Related Questions

Consider this simple model, where a Project has one ProjectType and, naturally many Projects
I have worked on two projects using Ajax and Ruby. In one project, I
I'm learning Ruby on Rails. I'm using some associations (has_many :through, ...) But i'm
In my Ruby project I am using a mess of things like moving and
I am trying to build a project on ruby, using Emacs for editor. I
I am considering ditching Ruby on Rails for my web-development pet-project and using a
I am using Cucumber for BDD development in my Ruby on Rails project and
As I'm riding the wave of resurgence of Smalltalk (especially because many Ruby-on-Rails people
In Ruby, like in many other OO programming languages, operators are overloadable. However, only
I read about many CSS related languages and tools which need ruby. What is

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.