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

  1. 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, and var4 and assigned them string values 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

  1. The variables on the left exceed the number of values on the right. In this case, extra variables on the right are assigned a Nil value.

var1, var2, var3, var4 = "educative" , "provides", "interactive"
puts var1, var2, var3 # read variables that got their values assigned
puts var4.nil? # is var4 Nil?

Explanation

  • Line 1: We initialized four variables with names var1, var2, var3, and var4 and assigned them string values 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 var4 is Nil or not.

Fewer variables than values

  1. 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, and var3 and assigned them string values 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,7
a, b, c, d, e = 1, *var3
puts a, b, c, d, e

Explanation

  • Line 1: We initialized an array with the name var3 and assigned int values of 1, 2, 3, 4, 5, 6, and 7.
  • Line 2: We did a parallel assignment and used the asterisk * to destructure the array.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved