更改 ruby 变量/引用的值 [英] Changing value of ruby variables/references
问题描述
我只是偶然发现了一些我不太明白的东西.我知道 ruby 中的变量是引用.所以那些很棒的东西是可能的.但是当我将一个变量传递给一个方法时,它的行为很奇怪:
I just stumbled upon something i don't quite understand. I know that variables in ruby are references. So that awesome stuff is possible. But when i pass a variable to a method, it behaves strangely:
my_var_a = "nothing happend to me"
my_var_b = "nothing happend to me"
def parse_set(my_var_set)
my_var_set = "my value changed"
end
def parse_sub(my_var_sub)
my_var_sub.sub! /(.*)/, "my value changed"
end
parse_set(my_var_a)
parse_sub(my_var_b)
my_var_a # => "nothing happend to me"
my_var_b # => "my value changed"
你能向我解释为什么它与 sub!
一起工作并且 =
保持对象不变?如何避免使用 sub!
但结果相同?
Can you explain to me why it works with sub!
and =
leaves the object unchanged? How can I avoid to use sub!
but having the same result?
推荐答案
my_var_a
和 my_var_set
是不同的引用,但它们指向同一个对象.如果您修改 my_var_set
中的对象,则更改会显示在 my_var_a
中.但是,如果您将 my_var_set
重新指向一个新对象,则不会改变 my_var_a
指向的内容.
my_var_a
and my_var_set
are different references, but they point at the same object. If you modify the object in my_var_set
, the change shows up in my_var_a
. However, if you repoint my_var_set
at a new object, that doesn't change what my_var_a
points at.
澄清...
Ruby 所做的称为按值传递引用.当你说
What Ruby does is called passing references by value. When you say
my_var_a = "nothing happend to me"
Ruby 在一个内存位置(我们称之为 1000)中保存字符串nothing had been at me",并将 my_var_a
引用保存在另一个内存位置(比如说 2000).当您的代码使用 my_var_a
时,解释器查看位置 2000,看到它指向 1000,然后从 1000 获取实际字符串值.
Ruby saves the string "nothing happend to me" in a memory location (let's call it 1000), and saves the my_var_a
reference in another memory location (let's say 2000). When your code uses my_var_a
, the interpreter looks at location 2000, see that it points to 1000, then gets the actual string value from 1000.
当您调用 parse_set(my_var_a)
时,Ruby 实际上会创建一个名为 my_var_set
的新引用,并将其指向 my_var_a
所指向的字符串在(内存位置 1000).但是,my_var_set
是 my_var_a
引用的副本——假设 my_var_set
是在内存位置 3000 处创建的.my_var_a
> 和 my_var_set
是内存中两个完全不同的引用,它们恰好指向保存字符串值的相同内存位置.
When you call parse_set(my_var_a)
, Ruby actually creates a new reference named my_var_set
and points it to the string that my_var_a
was pointing at (memory location 1000). However, my_var_set
is a copy of the my_var_a
reference -- let's say my_var_set
was created at memory location 3000. my_var_a
and my_var_set
are 2 completely different references in memory, they just happen to point at the same exact memory location which holds the string value.
parse_set
中的语句 my_var_set = "my value changed"
在内存中创建一个新字符串,并将 my_var_set
指向该新内存位置.但是,这不会改变原始 my_var_a
参考所指向的内容!现在 my_var_set
指向不同的内存位置,您对该变量所做的任何事情都不会影响 my_var_a
.
The statement my_var_set = "my value changed"
in parse_set
creates a new string in memory and points my_var_set
at that new memory location. However, this doesn't change what the original my_var_a
reference points at! Now that my_var_set
points at a different memory location, nothing that you do to that variable will affect my_var_a
.
parse_sub
也会发生相同的引用副本.但是 parse_sub
更改字符串的原因是因为您直接在 my_var_sub
引用上调用方法.执行此操作时,解释器会获取 my_var_sub
指向的对象,然后对其进行修改.因此该更改将显示在 my_var_a
引用中,因为它仍然指向相同的字符串.
The same reference copy happens for parse_sub
as well. But the reason that parse_sub
changes the string is because you're calling a method directly on the my_var_sub
reference. When you do this, the interpreter gets the object that my_var_sub
is pointing at and then modifies it. So that change will show up in the my_var_a
reference, because it still points at the same string.
这篇关于更改 ruby 变量/引用的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!