Introduction
In Ruby, there are different forms of assignment expressions which are simple assignment, abbreviated assignment and parallel assignment, and concept related to assignment to variables, assignment to constants and assignment to attributes and array elements.
In fact, the Ruby shorthand for calling a method is assigning to attributes and array elements.
This blog will discuss the concept of assignment and how assigning to attributes and array elements in ruby is done.
So, let's learn about this idea of assigning to attributes and array elements.
Assignment in Ruby
The assignment or simple assignment in ruby is done using the assignment operator '='. It involves one ‘lvalue’, the ‘=’ operator, and one ‘rvalue’.
Lvalue and Rvalue
An ‘lvalue’ is something present on the left-hand side of an assignment operator.
Values on the right-hand side of an assignment operator are commonly referred to as ‘rvalues’.
Example
We will be going through how the assignment is done with examples:
s = 6 # Setting the ‘lvalue s’ to the value 6
An abbreviated assignment is an expression that modifies the value of a variable by performing another operation (like addition) on the variable's current value. Assignment operators like += and *=, which combine binary operators with an equal sign(=), are used in the abbreviated assignments:
For example:
s *= 6 # Setting the lvalue ‘s’ to the value ‘s*6’
Any assignment expression with more than one ‘rvalue’ or ‘lvalue’ is said to be performing a parallel assignment. Here's an example:
s, t, u, v = 2, 4, 5, 7 # Setting ‘s’ to 2, ‘t’ to 4, ‘u’ to 5 and ‘v’ to 7
These tasks are effectively completed in parallel, rather than sequentially. The following two lines, for example, are not the same:
s, t = t, s # Swaps the values of two variables in 'Parallel'
s = t; t = s # Both variables have the same value in 'Sequence'
Ruby makes an array to hold the ‘rvalues’ and assigns it to the ‘lvalue’ when there is only one ‘lvalue’ and more than one ‘rvalue’:
s = 2, 4, 5 # s = [2, 4, 5]
We can put an asterisk '*' before the ‘lvalue’ but the meaning or return value of this assignment will not change.
If we want to prevent multiple ‘rvalues’ from being combined into a single array, use a comma after the ‘lvalue’. Even if there is no ‘lvalue’ after that comma, Ruby behaves as if there are multiple ‘lvalues’:
s, = 2, 4, 5 # s = 2, here, all the other values are discarded
Ruby tries to expand the ‘rvalue’ into a list of values to assign when there are multiple ‘lvalues’ but only a single ‘rvalue’. When an array is used as the ‘rvalue’, Ruby expands the array so that each element has its own ‘rvalue’. Ruby calls the to_ary method if the ‘rvalue’ is not an array but instead implements one, then expands the array it returns:
s, t, u, v = [2, 4, 5, 7] # This is same as s, t, u, v = 2, 4, 5, 7
Splat Operator
Asterisks before ‘rvalues’ indicate that they are arrays (or objects that resemble arrays) and that the values of each of their elements should be ‘rvalues’. Assigning values proceeds as previously described, with the array elements replacing the array in the original rvalue list:
s, t, u, v = 2, 4, *[5, 7] # This is same as s, t, u, v = 2, 4, 5, 7
Splatted rvalues can be array, hash and range. Any ‘rvalue’ that defines the to_a method can be prefixed with a splat in general. Splattering can be applied to any enumerable object, including enumerators. When a splat is applied to an object that lacks the to_a method, no expansion occurs and the splat evaluates the object itself.
When a ‘lvalue’ is preceded by an asterisk, it indicates that any additional ‘rvalues’ should be placed in an array and assigned to this ‘lvalue’. The value associated with that ‘lvalue’ is always an array with zero, one, or more elements:
s, t, *u = 2, 4, 5, 7 # s = 2; t = 4; u = [5, 7]
s, t, *u = 2, 4, 5 # s = 2; t = 4; u = [5]
s, t, *u = 2, 4 # s = 2; t = 4; u = []
s, t, *u = 2 # s = 2; t = nil; u = []
*s, t, u = 2, 4, 5, 7 # s = [2, 4]; t = 5; u = 7
*s, t, u = 2, 4, 5 # s = [2]; t = 4; u = 5
*s, t, u = 2, 4 # s = []; t = 2; u = 4
*s, t, u = 2 # s = []; t = nil; u = 2
Splats can appear on both sides of the parallel assignment expression:
s, t, u, *v = 2, 4, [5, 7, 9] # s = 2; t = 4; u = 5; v = [7, 9]
In the circumstance that there are more ‘lvalues’ than ‘rvalues’ and there is no splat operator present, the first ‘rvalue’ is assigned to the first ‘lvalue’, the second ‘rvalue’ is assigned to the second ‘lvalue’, and so on until all of the ‘rvalues’ have been assigned.
The remaining ‘lvalues’ are then assigned ‘nil’, overwriting any existing value for that ‘lvalue’:
s, t, u, v = 2, 4, 5 # Setting ‘s’ to 2, ‘t’ to 4, ‘u’ to 5 and ‘v’ to ‘nil’
If there are more ‘rvalues’ than ‘lvalues’ and no splat operator is used, ‘rvalues’ are assigned to each ‘lvalue’ in order, and the remaining ‘rvalues’ are discarded:
s, t, u = 2, 4, 5, 7 # Setting ‘s’ to 2, ‘t’ to 4, ‘u’ to 5 and 7 is not assigned to any variable
Now that we know what assignment is let us learn about assigning to attributes and array elements.