我从本教程中了解到如何在 Ruby 中进行引用:
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
当我试图分配 person2 在 IRB 中携带不同的字符串时,它变成了一个不同的对象。就 Ruby 的工作方式而言,这是否正常?