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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:02:56+00:00 2026-05-19T17:02:56+00:00

As a novice it’s probably something about Ruby I missed, but for the life

  • 0

As a novice it’s probably something about Ruby I missed, but for the life of me I don’t understand this result. So I have this simple function:

def crazyfunc(s)
  s.gsub!('a', 'b')
  #return has not purpose here
end

Now I have this simple few sets.

s1 = 'abc'
s2 = s1
s2 = crazyfunc(str2)

puts s1

=> bbc

Why in the world is s1 affected by crazyfunc? So doing this instead:

def crazyfunc(s)
  return s.gsub('a', 'b')
end

doesn’t change str1, so I figure it’s got to do with what the inplace gsub is doing. But I still don’t get the logic of why str1 would be changed.

  • 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-19T17:02:57+00:00Added an answer on May 19, 2026 at 5:02 pm

    String assignment in Ruby doesn’t implicitly copy the string. You are simply assigning another reference to it. If you want to copy the string, use clone.

    To demonstrate, you can check object IDs:

    ree-1.8.7-2010.02 > a = "foo"
     => "foo"
    ree-1.8.7-2010.02 > b = a
     => "foo"
    ree-1.8.7-2010.02 > a.object_id
     => 81728090
    ree-1.8.7-2010.02 > b.object_id
     => 81728090
    

    Since a and b have the same object ID, you know they’re the same object. If you want to modify b as a copy of a, you can either use methods which return a new string (like gsub rather than gsub!), or you can use b = a.clone, and then operate on b.

    ree-1.8.7-2010.02 > a = "foo"
     => "foo"
    ree-1.8.7-2010.02 > b = a.clone
     => "foo"
    ree-1.8.7-2010.02 > a.object_id
     => 81703030
    ree-1.8.7-2010.02 > b.object_id
     => 81696040
    ree-1.8.7-2010.02 > b.gsub! "f", "e"
     => "eoo"
    ree-1.8.7-2010.02 > a
     => "foo"
    ree-1.8.7-2010.02 > b
     => "eoo"
    

    Or more simply:

    ree-1.8.7-2010.02 > a = "foo"
     => "foo"
    ree-1.8.7-2010.02 > b = a.gsub("f", "e")
     => "eoo"
    ree-1.8.7-2010.02 > puts a, b
    foo
    eoo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Novice question, but I don't really understand why there are so many operations for
I am a Delphi novice, but I'm trying to understand the relationship between TApplication
This is almost certainly a very novice question, but being as I am a
I'm a very novice OCaml programmer so please forgive me if this is a
I am novice in sharepoint programming. I have a following code: SPWorkflowTask task =
I am a novice in the world of source/version control and I have been
complete novice at work (who is also ill and feeling particularly thick) I have
Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once db_login.php
Novice question I guess: The Scenario: I have an asp.net page that contains 6
I'm quite a novice with C# and a complete newbie regarding WPF. Probably it's

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.