Ruby对象引用

时间:2015-01-04 00:28:19

标签: ruby-on-rails ruby string object reference

我在Ruby中遇到了一段令我困惑的代码片段。将其归结为下面的一个简单示例来说明。为什么sArr在第一种情况下没有自动更新其内容但在第二种情况下会发生?这些片段简短且不言自明,三种回归分隔了两种情景。

2.0.0-p598 :068 > str = "Hello"
=> "Hello" 
2.0.0-p598 :069 > sArr = [str]
=> ["Hello"] 
2.0.0-p598 :070 > str = str.upcase
=> "HELLO" 
2.0.0-p598 :071 > str
=> "HELLO" 
2.0.0-p598 :072 > sArr
=> ["Hello"]  # Why is this not ["HELLO"] like in the sequence below?
2.0.0-p598 :073 > 
2.0.0-p598 :074 >   
2.0.0-p598 :075 >   
2.0.0-p598 :076 >   str = "Hello"
=> "Hello" 
2.0.0-p598 :077 > sArr = [str]
=> ["Hello"] 
2.0.0-p598 :078 > str.upcase!
=> "HELLO" 
2.0.0-p598 :079 > sArr
=> ["HELLO"] 

1 个答案:

答案 0 :(得分:5)

这是因为使用str = str.upcase而不是str.upcase!创建了一个新的String实例:

str = "Hello"
str.object_id
# => 70132476337960 
str = str.upcase
# => "HELLO" 
str.object_id
# => 70132476374360 (new instance)

str = "Hello"
str.object_id
# => 70132476415240 
str.upcase!
# => "HELLO" 
str.object_id
# => 70132476415240 (same instance)

sArr不受str = str.upcase影响,因为它持有对初始"Hello"的引用。分配到str不会比sArr更改str = 'World'中的对象。相反,str.upcase!会修改sArr所持有的实例。