Is it correct to assign variables like a = b = c = d = 5?

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 that there is no problem assigning such values. My question is to appoint as above, or how is it?

 a , b, c, d = 5, 5, 5, 5 
+44
ruby ruby-on-rails
May 28 '10 at 13:30
source share
6 answers

What you need to know that your case only works fine, because the numbers are immutable in Ruby. You don't want to do this with strings, arrays, hashes, or just about anything 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" 

While parallel form is safe for all types:

 a,b,c,d = "test","test","test","test" => ["test", "test", "test", "test"] b << "x" => "testx" a => "test" 
+86
May 28 '10 at 2:09
source share
β€” -

There is nothing wrong with assigning this path ( a = b = c = d = 5 ). I personally prefer it over several assignments if all the variables should have the same value.

Here's another way:

 a, b, c, d = [5] * 4 
+8
May 28, '10 at 13:35
source share

If he is doing well, do it.

Language allows this, as you have discovered, and behaves as you expected. I would suggest that the only question you should ask yourself is expressiveness: is this a code telling you what its purpose is?

Personally, I don’t particularly like using this construct for many other purposes besides initializing, by default, preferably zero. Ideally, variables that would be initialized would have a similar purpose, such as counters. But if I had more than a pair of identically assigned variables, I could very well consider declaring them a form of duplication in order to be reorganized, for example, in Hash.

+6
May 28 '10 at 13:44
source share

These two initializations express a different meaning. The value a = b = c = d = 5 means that all my variables must be initialized with the same value, and this value is 5 ". The other, a, b, c, d = 5, 5, 5, 5 , means" I have a list of variables and a corresponding list of init values. "

Is your logic such that all variables should always be the same? Then the first is better. If not, the second could be better. Another question: a complete list of 4 variables? Are you likely to add or remove another variable to this group? If so, I would suggest another option:

 a = 5 b = 5 c = 5 d = 5 
+4
May 28 '10 at
source share

I once bit this one. Today it can save you a few keystrokes, but then bite you. 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] 
+3
Aug 25 '12 at 14:00
source share

I don't use ruby ​​at all, so this 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 a lot nicer IMO.

0
May 28 '10 at 13:37
source share



All Articles