为什么`

时间:2021-09-09 12:23:23

This question already has an answer here:

这个问题在这里已有答案:

I'm working through a tutorial right now, and I'd like to understand why the following occurs:

我现在正在编写一个教程,我想了解为什么会发生以下情况:

original_string = "Hello, "
hi = original_string
there = "World"
hi += there
assert_equal "Hello, ", original_string

original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal "Hello, World", original_string

Why does += have no effect on original_string, and << does? I was absolutely certain that the second case would also equal "Hello, ", but that's not the case.

为什么+ =对original_string没有影响,而<

hi = original string in the first example appears to copy the value of original_string into hi, but hi = original string in the second example appears to set hi to point to the same string as original string. I would guess there is some kind of implicit decision behind the scenes as to whether to copy the value or copy the reference... or something.

hi =第一个示例中的原始字符串似乎将original_string的值复制为hi,但是hi =第二个示例中的原始字符串似乎设置为指向与原始字符串相同的字符串。我猜在幕后会有一些关于是复制值还是复制引用的隐含决定......或者其他什么。

3 个解决方案

#1


3  

<< on string mutates the original object while += creates a copy and returns that.

<< on string会改变原始对象,而+ =会创建一个副本并返回该副本。

See the object_id of the respective strings below:

请参阅下面各个字符串的object_id:

2.1.1 :029 > original_string = "Hello, "
 => "Hello, " 
2.1.1 :030 > hi = original_string
 => "Hello, " 
2.1.1 :031 > there = "World"
 => "World" 
2.1.1 :032 > original_string.object_id
 => 70242326713680 
2.1.1 :033 > hi.object_id
 => 70242326713680 

2.1.1 :034 > hi += there
 => "Hello, World" 
2.1.1 :035 > hi.object_id
 => 70242325614780 

Note the different object_id on hi on line 35. Once you reset hi to original_string in your example above, and use <<, it modifies the same object.

请注意第35行的hi上的不同object_id。在上面的示例中将hi重置为original_string,然后使用<<,它会修改相同的对象。

#2


3  

In both examples, hi = original_string copies the reference.

在两个示例中,hi = original_string复制引用。

With +=, however, you reassign hi to point to a new string, even though the variable name is the same.

但是,使用+ =,即使变量名称相同,也可以重新分配hi以指向新字符串。

This is because hi += there expands in the interpreter to the expression hi = hi + there.

这是因为hi + =在解释器中扩展到表达式hi = hi +那里。

Before this operation, hi and original string share a reference to the same string object. Upon the = operation in the expanded expression, hi now references the newly-created string result of hi + there.

在此操作之前,hi和原始字符串共享对同一字符串对象的引用。在扩展表达式中的=操作时,hi现在引用新创建的hi +字符串结果。

In the expression hi << there, nothing happens to change which object hi refers to. It refers to the same string as original_string, and therefore both hi and original_string reflect the change (which is of course due to the fact that they both reference the same string object).

在表达式hi < <中,没有任何改变可以改变hi指的是哪个对象。它引用与original_string相同的字符串,因此hi和original_string都反映了更改(当然这是因为它们都引用了相同的字符串对象)。< p>

#3


1  

You should check out the Ruby documentation for the String Class:

您应该查看String类的Ruby文档:

String#+ method

String#<< method

#1


3  

<< on string mutates the original object while += creates a copy and returns that.

<< on string会改变原始对象,而+ =会创建一个副本并返回该副本。

See the object_id of the respective strings below:

请参阅下面各个字符串的object_id:

2.1.1 :029 > original_string = "Hello, "
 => "Hello, " 
2.1.1 :030 > hi = original_string
 => "Hello, " 
2.1.1 :031 > there = "World"
 => "World" 
2.1.1 :032 > original_string.object_id
 => 70242326713680 
2.1.1 :033 > hi.object_id
 => 70242326713680 

2.1.1 :034 > hi += there
 => "Hello, World" 
2.1.1 :035 > hi.object_id
 => 70242325614780 

Note the different object_id on hi on line 35. Once you reset hi to original_string in your example above, and use <<, it modifies the same object.

请注意第35行的hi上的不同object_id。在上面的示例中将hi重置为original_string,然后使用<<,它会修改相同的对象。

#2


3  

In both examples, hi = original_string copies the reference.

在两个示例中,hi = original_string复制引用。

With +=, however, you reassign hi to point to a new string, even though the variable name is the same.

但是,使用+ =,即使变量名称相同,也可以重新分配hi以指向新字符串。

This is because hi += there expands in the interpreter to the expression hi = hi + there.

这是因为hi + =在解释器中扩展到表达式hi = hi +那里。

Before this operation, hi and original string share a reference to the same string object. Upon the = operation in the expanded expression, hi now references the newly-created string result of hi + there.

在此操作之前,hi和原始字符串共享对同一字符串对象的引用。在扩展表达式中的=操作时,hi现在引用新创建的hi +字符串结果。

In the expression hi << there, nothing happens to change which object hi refers to. It refers to the same string as original_string, and therefore both hi and original_string reflect the change (which is of course due to the fact that they both reference the same string object).

在表达式hi < <中,没有任何改变可以改变hi指的是哪个对象。它引用与original_string相同的字符串,因此hi和original_string都反映了更改(当然这是因为它们都引用了相同的字符串对象)。< p>

#3


1  

You should check out the Ruby documentation for the String Class:

您应该查看String类的Ruby文档:

String#+ method

String#<< method