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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:35:06+00:00 2026-05-13T14:35:06+00:00

I just stumbled upon something i don’t quite understand. I know that variables in

  • 0

I just stumbled upon something i don’t quite understand. I know that variables in ruby are references. So that awesome stuff is possible. But when i pass a variable to a method, it behaves strangely:

my_var_a = "nothing happend to me"
my_var_b = "nothing happend to me"

def parse_set(my_var_set)
  my_var_set = "my value changed"
end

def parse_sub(my_var_sub)
  my_var_sub.sub! /(.*)/, "my value changed"
end

parse_set(my_var_a)
parse_sub(my_var_b)

my_var_a # => "nothing happend to me"
my_var_b # => "my value changed"

Can you explain to me why it works with sub! and = leaves the object unchanged? How can I avoid to use sub! but having the same result?

  • 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-13T14:35:07+00:00Added an answer on May 13, 2026 at 2:35 pm

    my_var_a and my_var_set are different references, but they point at the same object. If you modify the object in my_var_set, the change shows up in my_var_a. However, if you repoint my_var_set at a new object, that doesn’t change what my_var_a points at.

    Edit: clarification…

    What Ruby does is called passing references by value. When you say

    my_var_a = "nothing happend to me"
    

    Ruby saves the string “nothing happend to me” in a memory location (let’s call it 1000), and saves the my_var_a reference in another memory location (let’s say 2000). When your code uses my_var_a, the interpreter looks at location 2000, see that it points to 1000, then gets the actual string value from 1000.

    When you call parse_set(my_var_a), Ruby actually creates a new reference named my_var_set and points it to the string that my_var_a was pointing at (memory location 1000). However, my_var_set is a copy of the my_var_a reference — let’s say my_var_set was created at memory location 3000. my_var_a and my_var_set are 2 completely different references in memory, they just happen to point at the same exact memory location which holds the string value.

    The statement my_var_set = "my value changed" in parse_set creates a new string in memory and points my_var_set at that new memory location. However, this doesn’t change what the original my_var_a reference points at! Now that my_var_set points at a different memory location, nothing that you do to that variable will affect my_var_a.

    The same reference copy happens for parse_sub as well. But the reason that parse_sub changes the string is because you’re calling a method directly on the my_var_sub reference. When you do this, the interpreter gets the object that my_var_sub is pointing at and then modifies it. So that change will show up in the my_var_a reference, because it still points at the same string.

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

Sidebar

Related Questions

I have just stumbled upon Bad Behavior - a plugin for PHP that promises
I just stumbled upon something I've never seen before. In the source of Backbone.js's
While doing a lot of benchmarking stuff these days, I stumbled upon something very
I've just stumbled upon this problem on the net. It kind of goes something
I just stumbled upon something in ORACLE SQL (not sure if it's in others),
I just stumbled upon a change that seems to have counterintuitive performance ramifications. Can
I just stumbled upon the package OneTuple on hackage. I want to know it's
I just stumbled upon MainSoft's Grasshopper , which claims to cross-compile .Net ILM to
I have just stumbled upon the bash syntax: foo=42 bar=$[foo+1] # evaluates an arithmetic
I just stumbled unto a weirdness in my JSP code. It seems that a

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.