Possible Duplicate:
Is Ruby pass by reference or by value?可能重复:Ruby是通过引用还是按值传递?
Working with Ruby, when passing an object to a method, how is the memory of this object handled?
使用Ruby,在将对象传递给方法时,如何处理此对象的内存?
Coming from a C background, I can think of several things which may be happening:
来自C背景,我可以想到可能发生的一些事情:
-
A copy of the memory associated with the according object and is made available to the method being called. In which case the modification of the object would only be reflected in the context of method being called, and not the calling method.
与相应对象关联的内存副本,可供被调用方法使用。在这种情况下,对象的修改只会反映在被调用方法的上下文中,而不是调用方法。
-
A reference to the memory of the object is passed the method being called (essentially a pointer). Hence any changes made by the object by the method being called or the calling method would be reflected in both contexts. As well, should this program be multithreaded, some kind of mechanism (mutex, semaphore, etc.) must be used to ensure mutually exclusive access to that memory performing write operations.
对对象的内存的引用传递给被调用的方法(本质上是指针)。因此,被调用的方法或调用方法对对象所做的任何更改都将反映在两个上下文中。同样,如果该程序是多线程的,则必须使用某种机制(互斥,信号量等)来确保对执行写操作的内存的互斥访问。
-
Something else I am unable to think of... maybe a memory model similar to that of Go... Pipes... MessagePassing...?
我无法想到的其他东西......也许是一个类似于Go ...... Pipes ...... MessagePassing ......的记忆模型?
What is actually happening?
实际发生了什么?
2 个解决方案
#1
2
Ruby uses pass-by-value, or more precisely, a special case of pass-by-value where the value being passed is always a pointer. This special case is also sometimes known as call-by-sharing, call-by-object-sharing or call-by-object.
Ruby使用pass-by-value,或者更确切地说,是一个传递值的特殊情况,其中传递的值总是一个指针。这种特殊情况有时也称为按共享呼叫,按对象分配或按对象调用。
It's the same convention that is used by more or less every object-oriented language ever created.
它与所创建的每一种面向对象语言或多或少都使用的约定相同。
Note: on all existing Ruby implementations Symbol
s, Fixnum
s and Float
s are actually passed directly by value and not with an intermediary pointer. However, since those three are immutable, there is no observable behavioral difference between pass-by-value and call-by-object-sharing in this case, so you can greatly simplify your mental model by simply treating everything as call-by-object-sharing. Just interpret these three special cases as internal compiler optimizations that you don't need to worry about.
注意:在所有现有的Ruby实现上,Symbols,Fixnums和Floats实际上是按值直接传递的,而不是通过中间指针传递的。但是,由于这三个是不可变的,在这种情况下,传值和对象共享之间没有可观察到的行为差异,因此您可以通过简单地将所有内容视为逐个对象来大大简化您的心理模型-sharing。只需将这三种特殊情况解释为内部编译器优化,您无需担心。
Here's a simple example you can run to determine the argument passing convention of Ruby (or any other language, after you translate it):
这是一个简单的例子,您可以运行以确定传递Ruby(或任何其他语言,翻译后)的约定的参数:
def is_ruby_pass_by_value?(foo)
foo.replace('More precisely, it is call-by-object-sharing!')
foo = 'No, Ruby is pass-by-reference.'
return nil
end
bar = 'Yes, of course, Ruby *is* pass-by-value!'
is_ruby_pass_by_value?(bar)
p bar
# 'More precisely, it is call-by-object-sharing!'
In short: it's your option 2.
简而言之:这是你的选择2。
#2
2
Ruby uses your second option, passes the parameter by reference.
Ruby使用您的第二个选项,通过引用传递参数。
#1
2
Ruby uses pass-by-value, or more precisely, a special case of pass-by-value where the value being passed is always a pointer. This special case is also sometimes known as call-by-sharing, call-by-object-sharing or call-by-object.
Ruby使用pass-by-value,或者更确切地说,是一个传递值的特殊情况,其中传递的值总是一个指针。这种特殊情况有时也称为按共享呼叫,按对象分配或按对象调用。
It's the same convention that is used by more or less every object-oriented language ever created.
它与所创建的每一种面向对象语言或多或少都使用的约定相同。
Note: on all existing Ruby implementations Symbol
s, Fixnum
s and Float
s are actually passed directly by value and not with an intermediary pointer. However, since those three are immutable, there is no observable behavioral difference between pass-by-value and call-by-object-sharing in this case, so you can greatly simplify your mental model by simply treating everything as call-by-object-sharing. Just interpret these three special cases as internal compiler optimizations that you don't need to worry about.
注意:在所有现有的Ruby实现上,Symbols,Fixnums和Floats实际上是按值直接传递的,而不是通过中间指针传递的。但是,由于这三个是不可变的,在这种情况下,传值和对象共享之间没有可观察到的行为差异,因此您可以通过简单地将所有内容视为逐个对象来大大简化您的心理模型-sharing。只需将这三种特殊情况解释为内部编译器优化,您无需担心。
Here's a simple example you can run to determine the argument passing convention of Ruby (or any other language, after you translate it):
这是一个简单的例子,您可以运行以确定传递Ruby(或任何其他语言,翻译后)的约定的参数:
def is_ruby_pass_by_value?(foo)
foo.replace('More precisely, it is call-by-object-sharing!')
foo = 'No, Ruby is pass-by-reference.'
return nil
end
bar = 'Yes, of course, Ruby *is* pass-by-value!'
is_ruby_pass_by_value?(bar)
p bar
# 'More precisely, it is call-by-object-sharing!'
In short: it's your option 2.
简而言之:这是你的选择2。
#2
2
Ruby uses your second option, passes the parameter by reference.
Ruby使用您的第二个选项,通过引用传递参数。