Assignment
From RubySpec
Assignment is the process of assigning a value to a variable or constant. Ruby supports simple assignment (variable = expression), as well as more complex assignment operations such as parallel assignment and operator assignment.
Simple Assignment
Variables or constants may be assigned the value of any legal Ruby expression. Like every operation in Ruby, assignment also generates a result or return value, which is generally the value assigned to the left-hand side.
a = 7 # local variable @b = false # instance variable @@c = "A class variable" # class variable
foo = Foo.new # object assignment BAR = 42 # constant assignment
Parallel Assignment
Ruby also supports true parallel assignment, such as the following:
a,b = 2,5 # a = 2, b = 5 a,b = b,a # a = 5, b = 2
When more left-hand side assignees exist than right-hand side values, nil values are created to provide as many values as needed:
a,b,c = 1,2 # a = 1, b = 2, c = nil
When more right-hand side values exist than left-hand side assignees, the last assignee receives an array of the remaining values:
a,b = 1,2,3 # a = 1, b = [2,3]
When a parallel assignment is being performed but only a single item exists on the right-hand side (rhs), the method to_ary will be called on the object if it is defined. In this case, the resulting array is used in place of the object, and then the parallel assignment is performed as though the contents of the returned array were specified as the rhs arguments:
class X
def to_ary
[1,2,3]
end
end
x = X.new
a,b,c = x # a = 1, b = 2, c = 3
d,e,f = x,5 # d = x, e = 5, f = nil
Nested Parallel Assignment
Parallel assignments can be nested, via the use of parentheses. When nested parallel assignments occur, each nested group of arguments is treated as a single argument in the outer parallel asisgnment:
a,(b,c),d = 1,[2,3,4],5 # a = 1, b = 2, c = 3, d = 5
Order of Evaluation
While in theory, assignment happens in parallel, in practice, the assignments are performed in a particular order. This only has implications where there are interactions between lhs or rhs arguments.
Parallel assignment evaluates the right-hand side (rhs) expressions in left-to-right order (except on Rubinius, where evaluation is performed right-to-left):
x = 0 def inc x += 1 end a,b,c = inc, inc, inc # a = 1, b = 2, c = 3
When a left-hand-side (lhs) argument appears multiple times, e.g. as an argument to another lhs arg, the order of execution of the assignment is also left-to-right. Thus:
c = [4,5,6] a,b,c[a] = 1,2,3 # a = 1, b = 2, c[a] = c[1] = 3, so now c = [4,3,6] c[a],b,a = 7,8,9 # c[a] = c[1] = 7, b = 8, a = 9, so now c = [4,7,6]
Return Value
The return value of a parallel assignment is almost never seen or used, and has different semantics depending on the Ruby implementation:
def foo a,b = 1,2 end x = foo # x = [1,2] in MRI, x = true in Rubinius
Operator Assignment
Ruby supports "operator assignment" as a short-cut:
a += b # equivalent to a = a + b
Most operators are supported, including +, -, /, *, %, **, ||, &&, |, &, ^, ~, << and >>.
Operator assignment can also be used when the left-hand side is an object, rather than a variable. For example:
foo.bar += 3 # equivalent to foo.bar=(foo.bar + 3)
Operator assignment can also be used with indexed objects, accessing values to be assigned to via the [] method of the object. Note that multiple arguments to [] may be used/required (depending on the method definition for []) to return the value to be used in the operator assignment:
a = [1,2,3,4,5] a[0] += 5 # a[0] = 6, so a is now [6,2,3,4,5] a[1,2] += [3.5] # a[1,2] = [2,3,3.5], so a is now [6,2,3,3.5,4,5]

