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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:51:24+00:00 2026-05-27T15:51:24+00:00

In Rails, variables seem to be linked, ie if if you make a =

  • 0

In Rails, variables seem to be linked, ie if if you make a = b, if you change b, you change a as well. Furthermore, if you change a, you also change b.

I come from a raw programing background (FORTRAN and C: languages aerospace is still tethered to), so this linking of variables is new to me. Furthermore, I it has given me some trouble, so I am looking for a way to control it.

I would like to be able to define a variable as a flashed constant value (or array of values), and until I redefine that one specifically, have it remain constant. More specifically, if I do:

a = b

And then I want to redefine

b = q

I would want a to retain the original b value (call it b’) while the new b has the value q.

a = b’

b = q

a = b’ != b = q

Look at the script below from rails console to further illustrate the aforementioned variable linking. You can tell that the two variables are interdependent: you cannot change one without changing the other. Any help controlling this phenomenon, or simply references to where I can learn more about it, would be more that appreciated.

system :004 >   b = []
 => [] 
system :005 > b = [123,456]
 => [123, 456] 
system :006 > a = b
 => [123, 456] 
system :007 > a
 => [123, 456] 
system :008 > b
 => [123, 456] 
system :009 > b[0]
 => 123 
system :010 > b[0]=789
 => 789 
system :011 > a
 => [789, 456] 
system :012 > b
 => [789, 456] 
system :013 > a[0] = 0
 => 0 
system :014 > a
 => [0, 456] 
system :015 > b
 => [0, 456] 
system :016 > 
  • 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-27T15:51:25+00:00Added an answer on May 27, 2026 at 3:51 pm

    The main problem you’re having is that once you’ve created an object (say a), other instance variables often create pointers to that same object, rather than create a new one.

    In this answer : Object assignment and pointers someone goes over how “assignment never makes a copy in ruby, but methods often return new objects instead of modifying existing objects.”

    Short answer: to ensure you’re creating an new object (ie not a new pointer to the same one) do this:

     b = a.dup
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from an ASP.NET MVC background and am currently going through the following
I need to set some server specific variables in a rails application that will
I have a question about ruby on rails and the process of assigning variables
I can't seem to grasp the Rails routing just yet. I want to be
Converting another app from Rails 2 to Rails 3 - in one of my
Why should we avoid using class variables @@ in rails? Is there any security
I was wondering how to add custom configuration variables to a Rails application and
I cannot seem to get a nested form to generate in a rails view
I need some clearance on scope of instance variables in rails application (not sure
I am suddenly completely lost with scope of variables in Rails with Mongoid. (Probably

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.