0

Where did I go wrong?

x = 5 => 5 a = 2 => 2 b = x + a => 7 a = 29 => 29 puts b 7 => nil puts a 29 => nil puts b 7 => nil Is b = x + a a snapshot in time that stays that way forever no matter what changes about x or a?

13th Jun 2017, 2:04 AM
AJ Sellarole
AJ Sellarole - avatar
4 Antworten
+ 3
Your variables are getting references to existing objects. Notice how the object_id is the same regardless of how I specify "7" (or change a variable that originally led to the object for 7). https://code.sololearn.com/cbYnENB5Gh3u/?ref=app If you want to look more at how object_id works (close to your example): https://launchschool.com/blog/references-and-mutability-in-ruby
13th Jun 2017, 4:59 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Your followup question...track changes to vars...perhaps by using "watches" (a debugging term): debugging: trace_var http://phrogz.net/ProgrammingRuby/trouble.html Pry...to get at the code state: http://pryrepl.org https://stackoverflow.com/questions/41811558/how-can-i-watch-a-variable-in-ruby-pry
13th Jun 2017, 5:00 AM
Kirk Schafer
Kirk Schafer - avatar
0
b stores the result of the addition x+a. That result is 7. It is not storing references to 'x' and 'a'.
13th Jun 2017, 2:20 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
is there anything like a variable that stores references to x and a and tracks the changes if them?
13th Jun 2017, 2:26 AM
AJ Sellarole
AJ Sellarole - avatar