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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:09:02+00:00 2026-05-16T16:09:02+00:00

I have discovered a flaw in my understanding of Ruby or programming theory or

  • 0

I have discovered a flaw in my understanding of Ruby or programming theory or both. Look at this Code:

#!/usr/bin/ruby -w
@instance_ar = [1,2,3,4]
local_ar = @instance_ar
local_ar_2 = local_ar
###
irrelevant_local_ar = [5,6,7,8]
###
for i in irrelevant_local_ar
    local_ar_2.push(i)
end
count = 0
for i in local_ar_2
    puts "local_ar_2 value: #{i} and local_ar value: #{local_ar[count]} and @instance_ar value: #{@instance_ar[count]}\n"
    count += 1
end

The output of that is

local_ar_2 value: 1 and local_ar value: 1 and @instance_ar value: 1
local_ar_2 value: 2 and local_ar value: 2 and @instance_ar value: 2
local_ar_2 value: 3 and local_ar value: 3 and @instance_ar value: 3
local_ar_2 value: 4 and local_ar value: 4 and @instance_ar value: 4
local_ar_2 value: 5 and local_ar value: 5 and @instance_ar value: 5
local_ar_2 value: 6 and local_ar value: 6 and @instance_ar value: 6
local_ar_2 value: 7 and local_ar value: 7 and @instance_ar value: 7
local_ar_2 value: 8 and local_ar value: 8 and @instance_ar value: 8

Question A: How does push to local_ar_2 change the two other arrays? My understanding of local variables was that once they were created, they should not affect any other variables, being that they were local.

Question B: How can I avoid things like this from happening?
Coming from C and Perl this is just blowing my mind.

  • 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-16T16:09:02+00:00Added an answer on May 16, 2026 at 4:09 pm

    Ruby works with references! Keep that in mind. If you want a copy you’d have to do it like:

    @instance_ar = [1,2,3,4]
    local_ar = @instance_ar.clone
    local_ar_2 = local_ar.clone
    

    Edit:

    Examples:

    a = ["a", "b", "c"]
    b = a[0]
    b = "d" # We assign a new object to b!
    

    a is:
    => [“a”, “b”, “c”]

    but:

    a = ["a", "b", "c"]
    b = a[0]
    b[0] = "d" # We are working with the reference!
    
    a is:
    => ["d", "b", "c"]
    
    a = "hello"
    b = a
    b += " world" 
    # Is the same as b = b + " world", we assign a new object!
    

    a is:
    => “hello”

    but:

    a = "hello"
    b = a
    b<<" world"
    # We are working with the reference!
    

    a is:
    => “hello world”

    a = "abc"
    b = a
    b[0] = "d" # we are working with the reference
    

    a is:
    => “dbc”

    You can read everything about it here: http://ruby-doc.org/docs/ProgrammingRuby/. Scroll down to “Variables” almost at the bottom of the page.

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

Sidebar

Related Questions

[EDIT] I have now discovered the flaw in this approach. It works well when
I have discovered Ember.js through this article . A cursory glance at the documentation
I have discovered that the following PHP code crashes when it runs on a
This is a followup/post question to another question; as I have discovered a solution
I have discovered recently that's possible to call anoymous blocks from jdbc like this:
I have discovered the following code appear in two identical .php files on more
I have discovered that if i run following lines of code. int i =
I have discovered Scintilla/Notepad++ API this week end. As there is a nice Template
I have discovered through trial and error that the MATLAB engine function is not
We have discovered that one of our auto generated assemblies is throwing a StackOverflowException

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.