Assignment

Assignment is done using the equals sign (=).

  1. # Assigns to a local variable
  2. local = 1
  3. # Assigns to an instance variable
  4. @instance = 2
  5. # Assigns to a class variable
  6. @@class = 3

Each of the above kinds of variables will be explained later on.

Some syntax sugar that contains the = character is available:

  1. local += 1 # same as: local = local + 1
  2. # The above is valid with these operators:
  3. # +, -, *, /, %, |, &, ^, **, <<, >>
  4. local ||= 1 # same as: local || (local = 1)
  5. local &&= 1 # same as: local && (local = 1)

A method invocation that ends with = has syntax sugar:

  1. # A setter
  2. person.name=("John")
  3. # The above can be written as:
  4. person.name = "John"
  5. # An indexed assignment
  6. objects.[]=(2, 3)
  7. # The above can be written as:
  8. objects[2] = 3
  9. # Not assignment-related, but also syntax sugar:
  10. objects.[](2, 3)
  11. # The above can be written as:
  12. objects[2, 3]

The = operator syntax sugar is also available to setters and indexers. Note that || and && use the []? method to check for key presence.

  1. person.age += 1 # same as: person.age = person.age + 1
  2. person.name ||= "John" # same as: person.name || (person.name = "John")
  3. person.name &&= "John" # same as: person.name && (person.name = "John")
  4. objects[1] += 2 # same as: objects[1] = objects[1] + 2
  5. objects[1] ||= 2 # same as: objects[1]? || (objects[1] = 2)
  6. objects[1] &&= 2 # same as: objects[1]? && (objects[1] = 2)

Chained assignment

You can assign the same value to multiple variables using chained assignment:

  1. a = b = c = 123
  2. # Now a, b and c have the same value:
  3. a # => 123
  4. b # => 123
  5. c # => 123

The chained assignment is not only available to local variables but also to instance variables, class variables and setter methods (methods that end with =).

Multiple assignment

You can declare/assign multiple variables at the same time by separating expressions with a comma (,):

  1. name, age = "Crystal", 1
  2. # The above is the same as this:
  3. temp1 = "Crystal"
  4. temp2 = 1
  5. name = temp1
  6. age = temp2

Note that because expressions are assigned to temporary variables it is possible to exchange variables’ contents in a single line:

  1. a = 1
  2. b = 2
  3. a, b = b, a
  4. a #=> 2
  5. b #=> 1

If the right-hand side contains just one expression, the type is indexed for each variable on the left-hand side like so:

  1. name, age, source = "Crystal, 123, GitHub".split(", ")
  2. # The above is the same as this:
  3. temp = "Crystal, 123, GitHub".split(", ")
  4. name = temp[0]
  5. age = temp[1]
  6. source = temp[2]

Multiple assignment is also available to methods that end with =:

  1. person.name, person.age = "John", 32
  2. # Same as:
  3. temp1 = "John"
  4. temp2 = 32
  5. person.name = temp1
  6. person.age = temp2

And it is also available to index assignments ([]=):

  1. objects[1], objects[2] = 3, 4
  2. # Same as:
  3. temp1 = 3
  4. temp2 = 4
  5. objects[1] = temp1
  6. objects[2] = temp2