Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

and

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.

add comment

2 Answers

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
add comment

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
add comment

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