Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am a RoR developer and want to clarify some doubt about ruby variable assignment.

In ruby we have two ways for variable assignment.

str, arr, num = "Hi", [1, 2], 3


str = "Hi"
arr = [1, 2]
num = 3

I know that the first way will go slower than second way but, I want to know that as per ruby language, which way is preferable and performance reliable?

share|improve this question

closed as off topic by gnat, Frank Shearar, Matthieu, Dynamic, Jim G. Sep 26 '12 at 0:40

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 4 down vote accepted

"Writing Efficient Ruby Code" by Dr. Stefan Kaes has this to say about parallel assignment (your first example):

Like any other Ruby expression, a value needs to be returned from a parallel assignment. The value returned by a parallel assignment is the value of the expression on the right-hand side of the assignment. This means Ruby needs to create an array if the right-hand side is a list of expressions:

$ irb
>> a,b = 1,2
=> [1, 2]

Of course, if the assignment isn’t the last statement of a method, the created array becomes garbage immediately. It’s therefore advisable to avoid the use of parallel assignment in performance-critical code sections.

Dr. Kaes goes on to speculate that Ruby 1.9 may change the semantics of parallel assignments to always return true for performance reasons.

To my eye, parallel assignment aids neither clarity nor maintainability, so, performance reasons aside, I would avoid it as a matter of course.

share|improve this answer
There are circumstances where parallel assignment aids clarity. One is swapping: a, b = b, a. Another is a function with multiple return values: a, b = somefunc (although multiple return values can frequently be better handled as an instance of a distinct class). – Wayne Conrad Sep 1 '12 at 21:59

Second option is preferable in most of the cases. However first case is more readable in some situations.

If you have common kind of variables or an array you can use first case. Example:

arr1, arr2, arr3 ... arr20 = 1, 2, 3 ... 20

In this case using 1 line of code is more preferable then using 20 different statements. Code is more readable.

If you are going to use parallel assignments you need to take care of assignment flow

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.