Ruby-on-rails 像这样分配多个变量是否正确 a = b = c = d = 5?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2929356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it right to assign multiple variables like this a = b = c = d = 5?
提问by Salil
a = b = c = d = 5
puts (a) >> 5
puts (b) >> 5
puts (b) >> 5
puts (b) >> 5
a= a+1
puts (a) >> 6
puts (b) >> 5
I found there is no problem with the assigning of values like this. My question is should one assign like the one given above or like this?
我发现分配这样的值没有问题。我的问题是应该像上面给出的那样分配还是像这样分配?
a , b, c, d = 5, 5, 5, 5
回答by glenn mcdonald
The thing to be aware of here is that your case only works OK because numbers are immutable in Ruby. You don'twant to do this with strings, arrays, hashes or pretty much anything else other than numbers, because it would create multiple references to the same object, which is almost certainly not what you want:
这里要注意的是,您的情况只能正常工作,因为数字在 Ruby 中是不可变的。你不希望与字符串,数组,哈希或其他几乎任何数字以外的做到这一点,因为这会造成同一个对象,这是几乎可以肯定不是你想要的多次引用:
a = b = c = d = "test"
b << "x"
=> "testx"
a
=> "testx"
Whereas the parallel form is safe with all types:
而并行形式对所有类型都是安全的:
a,b,c,d = "test","test","test","test"
=> ["test", "test", "test", "test"]
b << "x"
=> "testx"
a
=> "test"
回答by Firas Assaad
There's nothing wrong with assigning it that way (a = b = c = d = 5). I personally prefer it over multiple assignment if all the variables need to have the same value.
以这种方式分配它没有错 ( a = b = c = d = 5)。如果所有变量都需要具有相同的值,我个人更喜欢它而不是多重赋值。
Here's another way:
这是另一种方式:
a, b, c, d = [5] * 4
回答by Mike Woodhouse
If it feels good, do it.
如果感觉良好,就去做。
The language allows it, as you discovered, and it behaves as you'd expect. I'd suggest that the only question you should ask yourself regards expressiveness: is the code telling you what its purpose is?
正如您发现的那样,该语言允许它,并且它的行为符合您的预期。我建议你应该问自己的唯一问题是关于表现力:代码是否告诉你它的目的是什么?
Personally, I don't particularly like using this construct for much other than initialisation to default values, preferably zero. Ideally the variables so initialised would all have a similar purpose as well, counters, for example. But if I had more than a couple of similarly-purposed variables I might very well consider declaring them to be a form of duplicate, to be refactored out into, for example, a Hash.
就我个人而言,除了初始化为默认值(最好为零)之外,我并不特别喜欢将此构造用于其他用途。理想情况下,如此初始化的变量也都有类似的目的,例如计数器。但是,如果我有多个用途相似的变量,我可能会考虑将它们声明为一种重复形式,将它们重构为例如哈希。
回答by Igor Krivokon
These two initializations express different meaning. The a = b = c = d = 5means "all my variables should be initialized to the same value, and this value is 5". The other one, a, b, c, d = 5, 5, 5, 5, means "I have a list of variables, and corresponding list of init values".
这两个初始化表达了不同的含义。的a = b = c = d = 5意思是“我所有的变量应该被初始化为相同的值,这个值是5”。另一个,a, b, c, d = 5, 5, 5, 5,表示“我有一个变量列表和相应的初始化值列表”。
Is your logic such that all the variables should always be the same? Then the first one is better. If not, the second one might be better. Another question: is your list of 4 variables comprehensive? is it likely that you will add or remove another variable to this group? If so, I'd suggest yet another variant instead:
您的逻辑是否使所有变量都应始终相同?那么第一个更好。如果没有,第二个可能更好。另一个问题:您的 4 个变量列表是否全面?您是否可能会向该组添加或删除另一个变量?如果是这样,我会建议另一种变体:
a = 5
b = 5
c = 5
d = 5
回答by Kibet Yegon
I once got bitten with that one. It may save you a few keystrokes today but come to bite you later. As @glenn mentioned, it creates multiple references to the same object.
我曾经被那个咬过。它今天可能会为您节省一些按键,但稍后会咬您。正如@glenn 提到的,它创建了对同一对象的多个引用。
Example: This applies to both ruby 1.8and 1.9
示例:这适用于 ruby1.8和1.9
> a = b = Array.new
=> []
> a.object_id == b.object_id
=> true
> a << 1
=> [1]
> b << 2
=> [1, 2]
回答by Josh Wright
I don't use ruby at all, so that might be an acceptable idiom, but a = b = c = d = 5looks pretty ugly to me. a , b, c, d = 5, 5, 5, 5looks much nicer, IMO.
我根本不使用 ruby,所以这可能是一个可以接受的习惯用法,但a = b = c = d = 5对我来说看起来很丑陋。a , b, c, d = 5, 5, 5, 5看起来好多了,海事组织。

