Ruby - 参数是按引用还是按值? [英] Ruby - Parameters by reference or by value?
问题描述
我不明白为什么他们说 Ruby 通过value,同时以下代码证明相反:
I don't understand why they say Ruby passes all parameters by value and at the same time the following code proves the opposite:
class MyClass1
@var1 = 123
def get1
@var1
end
def set1=value
@var1 = value
end
end
c1 = MyClass1.new
c1.set1 = 444
p c1.get1 # 444
def test1 mc
mc.set1 = 999
end
test1 c1
p c1.get1 # 999
如果是按值,它会打印出444
,而不是999
.
If it were by value, it would print out 444
, not 999
.
推荐答案
这个问题让人困惑,因为有一种叫做引用类型的东西,也有一种叫做 pass-by-reference 的东西,但他们实际上并没有全部彼此有很大关系.
This question confuses people because there is a thing called a reference type and there is a thing called pass-by-reference, but they don't actually have all that much to do with each other.
在传递引用场景中,函数的参数是对传入函数的变量的引用,修改参数就是修改原始变量.Ruby 不是这样的.例如,让我们看看下面的代码:
In a pass-by-reference scenario, the parameters of a function are references to the variables that were passed into the function, and modifying the parameters modifies the original variables. This is not what Ruby is. For example, let's look at the following code:
def inc(val)
val += 1
end
a = 1
inc a
puts a
如果 Ruby 是一种传递引用语言,该程序将打印 2,因为 inc
中的 val += 1
会增加 的值一个
.但事实并非如此.变量 val
不是对变量 a
的引用——它是一个被赋予相同值的独立变量.
If Ruby were a pass-by-reference language, this program would print 2, because the val += 1
in inc
would increment the value of a
. But that isn't what happens. The variable val
is not a reference to the variable a
— it's an independent variable that is given the same value.
等等!"你说."如果我们正在处理对象呢?对象变量肯定是通过引用传递的,对吧?"
"But wait!" you say. "What if we were dealing with objects? Surely object variables are passed by reference, right?"
def change_string(str)
str << " I can insult you all you want"
str << " because you'll never see this"
str << " because I'm going to replace the whole string!"
str << " Haha you smell bad!"
str = "What? I didn't say anything." # I'm so sneaky
end
be_nice_to_me = "hello"
change_string(be_nice_to_me)
puts be_nice_to_me
如果 Ruby 是按引用传递的,您将永远不会看到 change_string
方法的意义,因为 str = 什么,我什么都没说.";
会将 be_nice_to_me
的值完全替换为字符串 "What?我什么也没说."
但实际上change_string
的罪孽是有目共睹的.如果 Ruby 不通过引用传递,这怎么可能?
If Ruby were pass-by-reference, you'd never see how mean the change_string
method is, because the str = "What, I didn't say anything."
would totally replace the value of be_nice_to_me
with the string "What? I didn't say anything."
But in fact change_string
's sins are laid bare for all to see. How is this possible if Ruby doesn't pass by reference?
好吧,还记得我之前谈到的那些引用类型吗?嗯,这就是 Ruby 中的对象.引用类型是其值是对其他事物的引用的类型.在这种情况下,变量的值是对字符串 hello"
的引用.当你传递字符串时,变量的值——它是一个引用——被复制到变量 str
中.所以现在它们都持有对同一个对象的引用,但是 str
不是对 be_nice_to_me
的引用.因此,当您修改对象时,这些更改会显示出来,因为它们都指向同一个对象.但是当您修改一个变量时,另一个变量看不到它,因为两个变量都不是对另一个变量的引用.
Well, remember those reference types I talked about earlier? Well, that's what objects are in Ruby. A reference type is a type whose value is a reference to something else. In this case, the variable's value is a reference to the string "hello"
. When you pass the string, the variable's value — which is a reference — is copied into the variable str
. So now they both hold references to the same object, but str
is not a reference to be_nice_to_me
. So when you modify the object, those changes show up because they're both referring to the same object. But when you modify one variable, the other doesn't see it because neither variable is a reference to the other.
Ruby 是按引用传递还是按值传递?它是按值传递的,但所有值都是引用.
So is Ruby pass-by-reference or pass-by-value? It's pass-by-value, but all the values are references.
这篇关于Ruby - 参数是按引用还是按值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!