What is a parallel assignment in Ruby?
What is a parallel assignment?
Parallel assignment in Ruby facilitates the initialization of variables using a single line of code. The variables to be initialized precede the = operator and are separated by commas. This = operator is then followed by the values to be assigned to the primary variables. As the name suggests, all the assignments are parallel. The assignments are done according to the order of variables on the left.
Syntax
Consider the generic syntax below:
var1, var2, var3 = val1 , val2, val3
The three variables on the left, var1, var2, and var3, were assigned val1, val2, and val3, respectively. The values on the right can be of any data type.
Cases
We have five use cases that showcase various forms of parallel assignment.
- Vars equal to vals
- Vars greater than vals
- Vars less than vals
- Assigning array as val
Note: Here, we referred variables to vars and values to vals.
Let’s discuss them in detail.
Variables equal to values
- The number of variables on the left and values on the right are equal.
var1, var2, var3, var4 = "educative" , "provides", "interactive", "learning"puts var1, var2, var3, var4
Explanation
- Line 1: We initialized four variables with names
var1,var2,var3, andvar4and assigned themstringvalues of"educative","provides","interactive", and"learning".
Note: Using an array of values on the right is also acceptable per ruby syntax. Wrap the above values on the right side of
=by[]and witness this yourself!
More variables than values
- The variables on the left exceed the number of values on the right. In this case, extra variables on the right are assigned a
Nilvalue.
var1, var2, var3, var4 = "educative" , "provides", "interactive"puts var1, var2, var3 # read variables that got their values assignedputs var4.nil? # is var4 Nil?
Explanation
- Line 1: We initialized four variables with names
var1,var2,var3, andvar4and assigned themstringvalues of"educative","provides", and"interactive"leaving the fourth variable with no value. - Line 2: We print the variable values to the console.
- Line 3: We print the bool value of whether
var4isNilor not.
Fewer variables than values
- The variables on the left are lesser than the number of values on the right. In this case, ruby assigns the available variables their due values and skips the other value assignments, as their corresponding variables are not present.
var1, var2, var3 = "educative" , "provides", "interactive", "learning"puts var1, var2, var3
Explanation
- Line 1: We initialized three variables with names
var1,var2, andvar3and assigned themstringvalues of"educative","provides","interactive", and"learning".
Assigning array as a value
Consider an array of int. We can destructure it using * and assign its values to other variables in the parallel assignment as follows:
var3 = 2,3,4,5,6,7a, b, c, d, e = 1, *var3puts a, b, c, d, e
Explanation
- Line 1: We initialized an array with the name
var3and assignedintvalues of1,2,3,4,5,6, and7. - Line 2: We did a parallel assignment and used the asterisk
*to destructure the array.
Free Resources