开发者

Is it right to assign multiple variables like this a = b = c = d = 5?

开发者 https://www.devze.com 2023-01-01 04:07 出处:网络
a = b = c = d = 5 puts (a) >> 5 puts (b) >> 5 puts (b) >> 5 puts (b) >> 5 a= a+1 puts (a) >> 6
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 a开发者_JS百科bove or like this?

a , b, c, d = 5, 5, 5, 5


The thing to be aware of here is that your case only works OK because numbers are immutable in Ruby. You don't want 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:

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"


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.

Here's another way:

a, b, c, d = [5] * 4


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.


These two initializations express different meaning. The a = b = c = d = 5 means "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".

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:

a = 5
b = 5
c = 5
d = 5


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.

Example: This applies to both ruby 1.8 and 1.9

> a = b = Array.new
=> []
> a.object_id == b.object_id
=> true
> a << 1
=> [1]
> b << 2
=> [1, 2]


I don't use ruby at all, so that might be an acceptable idiom, but a = b = c = d = 5 looks pretty ugly to me. a , b, c, d = 5, 5, 5, 5 looks much nicer, IMO.

0

精彩评论

暂无评论...
验证码 换一张
取 消