1

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 > 
  • this seems to occur only with hashes.. ie if a=[0,1]; b=a then b => [0,1]... then if you change a[0] you will also change b[0]. but if you use variables such as a = "asdf"; b=a, a="fjf" then b will persist as "asdf". – jay Dec 23 '11 at 05:17
  • Interesting, though is there is there a way to make hashes constant then? – Caleb Balloch Dec 23 '11 at 06:18

1 Answers1

0

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
Community
  • 1
  • 1
jay
  • 12,066
  • 16
  • 64
  • 103
  • Your solution works. Particularly for the problem I had above. Unfortunately, however, I still have a problem. The problem above is a simplification of my issue. The issue that I have remaining is a link or pointer to a database, and regardless of creating a .dup or not, whenever you call a ".find" on the variable, it goes back into the database to find it, rather than the local variable. I will look around to see if there is any way to ".find" within a local variable without it linking back to the root db, but if you know of any, I would more than appreciate your thoughts. – Caleb Balloch Dec 23 '11 at 09:38