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

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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"

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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]

Solution 6 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSalilView Question on Stackoverflow
Solution 1 - Ruby on-Railsglenn mcdonaldView Answer on Stackoverflow
Solution 2 - Ruby on-RailsFiras AssaadView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMike WoodhouseView Answer on Stackoverflow
Solution 4 - Ruby on-RailsIgor KrivokonView Answer on Stackoverflow
Solution 5 - Ruby on-RailsKibet YegonView Answer on Stackoverflow
Solution 6 - Ruby on-RailsJosh WrightView Answer on Stackoverflow