Skip to Content
Course content

152: Rightward Assignment

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this mistake a hundred times, usually from developers coming from languages where you're taught to be very explicit about every single memory move. You're trying to swap two values, and you think you've got it handled, but your data just... disappears.

# We want to swap the priority of two tasks
high_priority = "Fix production bug"
low_priority = "Update documentation"

high_priority = low_priority
low_priority = high_priority

puts "High: #{high_priority}" # Output: Update documentation
puts "Low: #{low_priority}"   # Output: Update documentation

The Vanishing Variable

If you run that code, you'll notice that "Fix production bug" is gone entirely. Both variables now point to the documentation task. Why? Because Ruby executes these assignments sequentially. In the first line, you told Ruby to make high_priority point to the same object as low_priority. By the time you got to the second line, the original value of high_priority was already overwritten. It's gone.

In other languages, you'd have to create a temp variable to hold the value of high_priority while you move things around. It's clunky, it's three lines of code, and it's an easy place to make a typo.

Swapping in One Breath

Ruby gives us a much more elegant way to handle this called parallel assignment, or what some call rightward assignment. You can handle the swap in a single line:

high_priority = "Fix production bug"
low_priority = "Update documentation"

high_priority, low_priority = low_priority, high_priority

puts "High: #{high_priority}" # Output: Update documentation
puts "Low: #{low_priority}"   # Output: Fix production bug

I love this syntax because it reads like a mathematical statement. But it's worth knowing what's actually happening under the hood so you don't treat it like magic. Ruby isn't actually doing two assignments at the exact same nanosecond. Instead, it evaluates the right side of the equals sign first. It creates a temporary array ["Update documentation", "Fix production bug"] and then "unpacks" that array into the variables on the left.

Unpacking Method Results

Parallel assignment isn't just for swapping. It's incredibly powerful when a method returns multiple values (which, in Ruby, usually means it's returning an array). Instead of grabbing the array and accessing indices like result[0] and result[1], you can assign them directly.

Take a look at how I'd handle a coordinate system:

def get_player_position
  [10, 42] # Returns an array representing x and y
end

x, y = get_player_position

puts "Player is at X: #{x}, Y: #{y}"

It's cleaner, it's more descriptive, and it removes the cognitive load of remembering which index corresponds to which axis. Just be careful: if the method returns more values than you have variables, the extras are simply ignored. If it returns fewer, the remaining variables will be assigned nil.




📋 Practical Task

Refactoring the Game State Swap

You are working on a turn-based strategy game. Currently, the code for swapping the "Active Player" and the "Waiting Player" is using a clunky temporary variable. Your goal is to refactor this using parallel assignment to make the code more "Ruby-esque".

Your Task:
Modify the swap_turns method in the code below. Remove the temp_player variable and replace the three-line swap logic with a single line of parallel assignment.

class Game
  attr_accessor :active_player, :waiting_player

  def initialize
    @active_player = "Player 1"
    @waiting_player = "Player 2"
  end

  def swap_turns
    # REFACTOR THIS SECTION
    temp_player = @active_player
    @active_player = @waiting_player
    @waiting_player = temp_player
    # END REFACTOR
  end
end

game = Game.new
puts "Active: #{game.active_player}" # Player 1
game.swap_turns
puts "Active: #{game.active_player}" # Should be Player 2
Rating
0 0

There are no comments for now.

to be the first to leave a comment.