I got the idea of learning how referencing works in Ruby from this tutorial:
1.9.3-p194 :007 > person1 = "Tim"
=> "Tim"
1.9.3-p194 :008 > person1.class
=> String
1.9.3-p194 :009 > person1.object_id
=> 73847870
1.9.3-p194 :010 > person2 = person1
=> "Tim"
1.9.3-p194 :011 > person2.class
=> String
1.9.3-p194 :012 > person2.object_id
=> 73847870
1.9.3-p194 :013 > person1[0] = "J"
=> "J"
1.9.3-p194 :014 > person1
=> "Jim"
1.9.3-p194 :015 > person2
=> "Jim"
1.9.3-p194 :016 > person3 = person2.dup
=> "Jim"
1.9.3-p194 :017 > person3.object_id
=> 75146000
1.9.3-p194 :018 > person2 = "John"
=> "John"
1.9.3-p194 :019 > person1
=> "Jim"
1.9.3-p194 :020 > person1.object_id
=> 73847870
1.9.3-p194 :021 > person2.object_id
=> 75134230
When I attempted to assign person2 to carry a different string in IRB, it changed into a different object. Is this normal in terms of how Ruby works?
person1,person2andperson3aren’t objects — they’re variables. The strings themselves are objects. A variable is just a name that refers to an object. So when you assign a different object (in this case a string) to the variable, yes, the variable then refers to a different object.