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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:55:10+00:00 2026-06-01T21:55:10+00:00

I always see replace in the Array and Hash documentation and I always think

  • 0

I always see replace in the Array and Hash documentation and I always think that it’s odd.

I’m sure I’ve done something like this many times:

a = [:a, :b, :c, :d]

...

if some_condition
    a = [:e, :f]
end

But I never thought to use this instead:

a = [:a, :b, :c, :d]

...

if some_condition
    a.replace [:e, :f]
end

Which I assume is the intended use. Does this really save memory, or have some other benefit, or is it just a style thing?

  • 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-01T21:55:11+00:00Added an answer on June 1, 2026 at 9:55 pm

    I think the intended use is to modify an array in-place that has been passed to a method. For example:

    def m(a)
        a.replace(%w[a b])
    end
    
    a = %w[x y z]
    m(a)
    # a is now ['a', 'b']
    

    Without replace, you’d have to do something like this:

    def m(a)
        a.clear
        a << 'a' # or use .push of course
        a << 'b'
    end
    

    Using replace lets you do it all at once should bypass the auto-shrinking and auto-growing (which probably involves copying some memory) behavior that would be a side effect of replacing the array’s content (not the array itself!) element by element. The performance benefit (if any) is probably just an extra, the primary intent is probably to get pointer-to-pointer behavior without having to introduce pointers or wrap the array in an extra object.

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

Sidebar

Related Questions

I always see people mention that Python binding and C Sharp binding etc. when
In base class constructors I always see a parameterless constructor, like so: public abstract
This question is famous but everyone always says go to see this Notepad Tutorial
I have a query that looks like this: select id , int1 , int2
i have a string that looks like this: /dir/location/test-load-ABCD.p and i need to parse
In various C code, I see constants defined like this: #define T 100 Whereas
I always see the word node . As far as I know node means
I am trying to learn a dataset using Svmlight. I always see the following
When I see website starter code and examples, the CSS is always in a
Are boost ptime instances always UTC? I can't see any time zone info on

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.